Issue
I am new to Python. I am trying to create a fake GPIO module for my Raspberry Pi that uses PWM motors so that my interpreter (using Visual Studio Code) can understand it and pass it without error.
This is what I wanted to achieve:
#Motor.py
import RPi.GPIO as GPIO
GPIO.PWM(16,100).start(0)
This is the fake module I have attempted to create after attempting to learn the basic way how python handles modules
#RPi/GPIO.py
#(RPi folder has an empty __init__.py file along with the GPIO.py file)
BOARD = 1
IN = 1
OUT = 1
def setmode(a):
print(a)
def setup(a):
print(a)
def output(a):
print(a)
def PWM(a, b):
print(a)
def start(c):
print(c)
The error I have gotten shows like this:
AttributeError: 'NoneType' object has no attribute 'start'
I do not know how to properly create modules where it could work with multiple periods. How should I fix it so that it would achieve what I want?
Solution
GPIO.PWM
is a class - and start
is a member function of that class.
So you'll need to look at how classes are constructed using the __init__()
method, and how member functions work.
Here is an example of the PWM class you'll need to create:
class PWM:
def __init__(self, channel, frequency):
print(f"PWM({channel},{frequency})");
def start(self, duty_cycle):
print(f"PWM.start({duty_cycle})")
So looking at GPIO.PWM(16,100).start(0)
- this would first construct a PWM object by calling the PWM __init__()
method - and then call the start()
method on that object.
You can also split this into two calls if you need the PWM object. i.e.
motor_pwm = GPIO.PWM(16.100)
motor_pwm.start(0)
If that makes more sense?
Answered By - cguk70 Answer Checked By - Cary Denson (WPSolving Admin)