在Python中嵌入Python

时间:2015-04-22 16:59:47

标签: python c++ linux

希望你们能提供帮助。我搜索和谷歌搜索,但找不到解决方案。首先是我的代码:

 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

我仍然需要完成python方面,但这不是我的问题所在。

一切都在Code :: Blocks中完美构建(我直接在我的Pi上编码)当我按F8来逐步完成我的麻烦开始的代码。

首先我得到这个错误,我确实读到你让你的编译器忽略它但我不知道Code :: Blocks中是怎么做的。一等奖将完全摆脱它。 这是错误:

>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)

然后我使用step out按钮继续调试,一旦我到达LightsOn.cpp,乐趣就开始了。我的代码的执行跳转到下一行,然后返回到前一行。见下文。

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

我确实对我的变量和pModule(执行的第30行)进行了监视 - 它获取了一个值,然后在第二次执行时立即设置为null。

我之前测试过代码,但没有使用类和代码运行正常。

有人可以解释和协助吗?

0 个答案:

没有答案