Embedding Python in a C ++ class
Hope you guys can help. I have searched and searched google but could not find a solution. First, 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 far as I can, I still need to get done with python, but that's not where my problem is.
Everything works fine in Code :: Blocks (I code directly on my Pi) When I press F8 to go to the code where my problems start.
Initially I get this error and I read that you are forcing the compiler to ignore it, but I don't know how to do it in Code :: Blocks. 1st prize is 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)
Then I use the Exit button to continue debugging, and as soon as I get to LightsOn.cpp the fun begins. The execution of my code goes to the next line and then to the previous one. 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, for example, set the clock on my variables and pModule (line 30 of execution) - it gets a value and then set to zero as soon as it executes a second time.
I've tested the code in the previous time, but didn't use classes and the code worked fine.
Can anyone explain and help?
source to share
No one has answered this question yet
Check out similar questions: