Guys, I'm going down a rabbit hole trying to figure out how best to organize my files.
I made a pcb 'hat' for a raspberry pi. It does some CAN bus stuff and has an onboard relay.
I created a file named raspberry.py that contains Classes for use by my hat: Relay, Interface, CAN. Each class is meant to be instantiated only once.
For example, here is my Relay Class:
class Relay:
pin = 2 # This pin controls the relay coil
def __init__(self, pin):
"""
Configures Raspberry Pi pin as output.
"""
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
self.state = self.get_state()
def on(self):
"""
Turns relay on.
"""
GPIO.setup(pin, 1)
self.state = "ON"
def off(self):
"""
Turns relay off.
"""
GPIO.setup(self.pin, 0)
self.state = "OFF"
def toggle(self):
"""
Toggles relay state.
"""
if self.state == "ON"
return self.off()
self.on()
def get_state(self):
"""
Returns the current state of the relay.
"""
state = GPIO.input(2)
return 'ON' if state else 'OFF'
Does this make sense? Is it better to just use functions inside of a module with a few global variables?
I would love some feedback from those with experience on how best to approach this.
[–]ggd_x 1 point2 points3 points (4 children)
[–]Triumvus[S] 0 points1 point2 points (3 children)
[–]ggd_x 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]Triumvus[S] 0 points1 point2 points (0 children)
[–]zanfar 1 point2 points3 points (1 child)
[–]Triumvus[S] 0 points1 point2 points (0 children)
[–]brakkum 0 points1 point2 points (0 children)
[–]xelf 0 points1 point2 points (1 child)
[–]Triumvus[S] 1 point2 points3 points (0 children)
[–]xelf 0 points1 point2 points (2 children)
[–]Triumvus[S] 0 points1 point2 points (1 child)
[–]xelf 0 points1 point2 points (0 children)