在C++类中嵌入Python

2024-09-28 23:29:24 发布

您现在位置:Python中文网/ 问答频道 /正文

希望你们能帮忙。我搜了又搜,但找不到解决办法。首先我的代码:

 Main.cpp

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

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

return 0;

}

你知道吗切换灯.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;

 }

切换灯.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

你知道吗灯光.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方面的工作,但这不是我的问题所在。你知道吗

当我按F8单步遍历代码时,一切都完美地构建在Code::Blocks中(我直接在Pi上编写代码)。你知道吗

首先,我得到这个错误,我确实读到你让你的编译器忽略它,但我不知道如何在代码::块。第一个奖品就是彻底摆脱它。 错误如下:

>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按钮继续调试,一旦我到达莱特森.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。你知道吗

我以前确实测试过代码,但没有使用类,代码运行良好。你知道吗

有人能解释一下吗?你知道吗


Tags: thepyhomegpiopicppatint