Embedding Python in C++ using classes

Hope you guys can help. I searched and googled but could not find a solution. Firstly my code:

Main.cpp

#include <iostream>
#include <iomanip>
#include "ToggleLights.h"

using namespace std;
int main ()
{
bool lightsOn;
Lights lightsGo(50);
lightsOn=lightsGo.LightsChanged();

return 0;

}

ToggleLights.cpp

#include "ToggleLights.h"

using namespace std;
Lights::Lights(const int getBrightness)
{

}

bool Lights :: LightsChanged()
{

setenv("PYTHONPATH",".",1);
Py_Initialize();
state=PyGILState_Ensure();
//pName = PyString_FromString("Lights");
pModule = PyImport_ImportModule("Lights");
pArg = Py_BuildValue("(iii)", l_leftLightPin, l_rightLightPin,l_Brightness);
// pDict = PyModule_GetDict(pModule);
pFunc = PyObject_GetAttrString(pModule,"SetLights");
pRet = PyEval_CallObject(pFunc,pArg);

if (PyString_Check(pRet))
{ Py_DECREF(pValue);
Py_DECREF(pModule);
Py_DECREF(pName);
Py_Finalize();
return true;

} else
{
Py_DECREF(pValue);
Py_DECREF(pModule);
Py_DECREF(pName);
Py_Finalize();
return false;
}
}
void Lights::init_Lights(const int getBrightness)
{
//Setup * setup = Setup::get();
l_leftLightPin=20;
l_rightLightPin=21;
l_Brightness=getBrightness;

}

ToggleLights.h

#ifndef TOGGLELIGHTS_H_INCLUDED
#define TOGGLELIGHTS_H_INCLUDED
//# pragma once
#include "Python.h"
using namespace std;

//class Setup;
class Lights {
private:
int l_leftLightPin;
int l_rightLightPin;
int l_Brightness;

public:
PyObject *pName, *pModule, *pArg, *pFunc, *pRet, *pValue;
PyGILState_STATE state;
Lights(const int getBrightness = 50);
bool LightsChanged();
void init_Lights(const int getBrightness);

//Setup * setup ;
};
#endif // TOGGLELIGHTS_H_INCLUDED

Lights.py

#!/usr/bin/env python2.7
# script by Alex Eames http://RasPi.tv
#http://RasPi.tv/2013/how-to-use-soft-pwm-in-rpi-gpio-pt-2-led-dimming-and-motor-speed-control
# Using PWM with RPi.GPIO pt 2 - requires RPi.GPIO 0.5.2a or higher

def SetLights(PinL,PinR,Level):

import RPi.GPIO as GPIO # always needed with RPi.GPIO
from time import sleep # pull in the sleep function from time module

GPIO.setmode(GPIO.BCM) # choose BCM or BOARD numbering schemes. I use BCM

GPIO.setup(25, GPIO.OUT)# set GPIO 25 as output for white led
GPIO.setup(24, GPIO.OUT)# set GPIO 24 as output for red led

Left = GPIO.PWM(PinL, 100) # create object white for PWM on port 25 at 100 Hertz
Right = GPIO.PWM(PinR, 100) # create object red for PWM on port 24 at 100 Hertz

Left.start(Level) # start white led on 0 percent duty cycle (off)
Right.start(Level) # red fully on (100%)

# now the fun starts, we'll vary the duty cycle to
# dim/brighten the leds, so one is bright while the other is dim

#pause_time = 0.02 # you can change this to slow down/speed up

#try:
# while True:
# for i in range(0,101): # 101 because it stops when it finishes 100
# white.ChangeDutyCycle(i)
# red.ChangeDutyCycle(100 - i)
# sleep(pause_time)
# for i in range(100,-1,-1): # from 100 to zero in steps of -1
# white.ChangeDutyCycle(i)
# red.ChangeDutyCycle(100 - i)
# sleep(pause_time)

# except KeyboardInterrupt:
# white.stop() # stop the white PWM output
# red.stop() # stop the red PWM output
# GPIO.cleanup() # clean up GPIO on CTRL+C exit

As you can I still need finish up the python side but that is not where my problem is.

Everything builds perfectly in Code::Blocks (I code directly on my Pi) When I press F8 to step through the code that is where my hassles start.

Firsly I get this error and I did read that you get your compiler to ignore it but I don't know how in Code::Blocks. 1st prize would be to get rid of it completely.
Here is the error:

>Setting breakpoints
>Debugger name and version: GNU gdb (GDB) 7.4.1-debian
>Program received signal SIGILL, Illegal instruction.
>In ?? () (/usr/lib/arm-linux-gnueabihf/libcrypto.so.1.0.0)

I then use the step out button to continue debugging and as soon I reach LightsOn.cpp the fun starts. The execution of my code jumps to the next line and then back to the previous. See Below.

At /home/pi/PythonFiles/NewTest/NewTest/Main.cpp:11
At /home/pi/PythonFiles/NewTest/NewTest/Main.cpp:12
At /home/pi/PythonFiles/NewTest/NewTest/LightsOn.cpp:26
At /home/pi/PythonFiles/NewTest/NewTest/LightsOn.cpp:24
At /home/pi/PythonFiles/NewTest/NewTest/LightsOn.cpp:26
At /home/pi/PythonFiles/NewTest/NewTest/LightsOn.cpp:27
At /home/pi/PythonFiles/NewTest/NewTest/LightsOn.cpp:28
At /home/pi/PythonFiles/NewTest/NewTest/LightsOn.cpp:30
At /home/pi/PythonFiles/NewTest/NewTest/LightsOn.cpp:31
At /home/pi/PythonFiles/NewTest/NewTest/LightsOn.cpp:30
At /home/pi/PythonFiles/NewTest/NewTest/LightsOn.cpp:31
At /home/pi/PythonFiles/NewTest/NewTest/LightsOn.cpp:33
At /home/pi/PythonFiles/NewTest/NewTest/LightsOn.cpp:31
At /home/pi/PythonFiles/NewTest/NewTest/LightsOn.cpp:33
Program received signal SIGSEGV, Segmentation fault.
In PyObject_GetAttrString () (/usr/lib/libpython2.7.so.1.0)
At /home/pi/PythonFiles/NewTest/NewTest/LightsOn.cpp:33

I did put a watch on my variables and pModule (Line 30 of the execution) for example - it gets a value and is then set to null as soon as it executes the second time.

I did test the code a previous time but did not use classes and code ran fine.

Could somebody please explain and assist?
Topic archived. No new replies allowed.