Embedding Python in a C++ class -


hope guys can help. searched , googled not find solution. firstly 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 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 rpi.gpio pt 2 - requires rpi.gpio 0.5.2a or higher  def setlights(pinl,pinr,level):  import rpi.gpio gpio # needed rpi.gpio time import sleep  # pull in sleep function time module  gpio.setmode(gpio.bcm)  # choose bcm or board numbering schemes. use bcm  gpio.setup(25, gpio.out)# set gpio 25 output white led gpio.setup(24, gpio.out)# set gpio 24 output red led  left = gpio.pwm(pinl, 100)    # create object white pwm on port 25 @    100 hertz right = gpio.pwm(pinr, 100)      # create object red pwm on port 24 @ 100 hertz  left.start(level)              # start white led on 0 percent duty cycle (off) right.start(level)              # red on (100%)  # fun starts, we'll vary duty cycle  # dim/brighten leds, 1 bright while other dim  #pause_time = 0.02           # can change slow down/speed  #try: #    while true: #        in range(0,101):      # 101 because stops when   finishes 100 #            white.changedutycycle(i) #            red.changedutycycle(100 - i) #            sleep(pause_time) #        in range(100,-1,-1):      # 100 0 in steps of -1 #            white.changedutycycle(i) #            red.changedutycycle(100 - i) #            sleep(pause_time)  # except keyboardinterrupt: #    white.stop()            # stop white pwm output #    red.stop()              # stop red pwm output #    gpio.cleanup()          # clean gpio on ctrl+c exit 

as can still need finish python side not problem is.

everything builds in code::blocks (i code directly on pi) when press f8 step through code hassles start.

firsly error , did read compiler ignore don't know how in code::blocks. 1st prize rid of completely. here error:

>setting breakpoints >debugger name , 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 use step out button continue debugging , reach lightson.cpp fun starts. execution of code jumps next line , previous. see below.

at /home/pi/pythonfiles/newtest/newtest/main.cpp:11 @ /home/pi/pythonfiles/newtest/newtest/main.cpp:12 @ /home/pi/pythonfiles/newtest/newtest/lightson.cpp:26 @ /home/pi/pythonfiles/newtest/newtest/lightson.cpp:24 @ /home/pi/pythonfiles/newtest/newtest/lightson.cpp:26 @ /home/pi/pythonfiles/newtest/newtest/lightson.cpp:27 @ /home/pi/pythonfiles/newtest/newtest/lightson.cpp:28 @ /home/pi/pythonfiles/newtest/newtest/lightson.cpp:30 @ /home/pi/pythonfiles/newtest/newtest/lightson.cpp:31 @ /home/pi/pythonfiles/newtest/newtest/lightson.cpp:30 @ /home/pi/pythonfiles/newtest/newtest/lightson.cpp:31 @ /home/pi/pythonfiles/newtest/newtest/lightson.cpp:33 @ /home/pi/pythonfiles/newtest/newtest/lightson.cpp:31 @ /home/pi/pythonfiles/newtest/newtest/lightson.cpp:33 program received signal sigsegv, segmentation fault. in pyobject_getattrstring () (/usr/lib/libpython2.7.so.1.0)  @ /home/pi/pythonfiles/newtest/newtest/lightson.cpp:33 

i did put watch on variables , pmodule (line 30 of execution) example - gets value , set null executes second time.

i did test code previous time did not use classes , code ran fine.

could please explain , assist?


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -