def __init__(self, cs_pin, clock_pin, data_pin, units="c", board=GPIO.BCM): """Initialize Soft (Bitbang) SPI bus Parameters: - cs_pin: Chip Select (CS) / Slave Select (SS) pin (Any GPIO) - clock_pin: Clock (SCLK / SCK) pin (Any GPIO) - data_pin: Data input (SO / MOSI) pin (Any GPIO) - units: (optional) unit of measurement to return. ("c" (default) | "k" | "f") - board: (optional) pin numbering method as per RPi.GPIO library (GPIO.BCM (default) | GPIO.BOARD) """ self.cs_pin = cs_pin self.clock_pin = clock_pin self.data_pin = data_pin self.units = units self.data = None self.board = board # Initialize needed GPIO GPIO.setmode(self.board) GPIO.setup(self.cs_pin, GPIO.OUT) GPIO.setup(self.clock_pin, GPIO.OUT) GPIO.setup(self.data_pin, GPIO.IN) # Pull chip select high to make chip inactive GPIO.output(self.cs_pin, GPIO.HIGH)
def toggle_enable(self): """ Toggle 'Enable' pin """ time.sleep(0.0005) GPIO.output(self.E, True) time.sleep(0.0005) GPIO.output(self.E, False) time.sleep(0.0005)
def write(self, line_bits, command: bool): """ Write bits to LCD module """ GPIO.output(self.RS, command) for bit_map in [self.hi_bits, self.lo_bits]: self.null_bits() for bit in bit_map.keys(): if line_bits & bit == bit: GPIO.output(bit_map[bit], True) self.toggle_enable()
def read(self): """Reads 32 bits of the SPI bus & stores as an integer in self.data.""" bytesin = 0 # Select the chip GPIO.output(self.cs_pin, GPIO.LOW) # Read in 32 bits for i in range(32): GPIO.output(self.clock_pin, GPIO.LOW) bytesin = bytesin << 1 if GPIO.input(self.data_pin): bytesin = bytesin | 1 GPIO.output(self.clock_pin, GPIO.HIGH) # Unselect the chip GPIO.output(self.cs_pin, GPIO.HIGH) # Save data self.data = bytesin
def __init__( self, E: int, RS: int, D4: int, D5: int, D6: int, D7: int, command: bool, chars_per_line: int = 16, char_mode: bool = True, ): """ Initialize and clear display """ self.command = command self.E = E self.RS = RS self.D4 = D4 self.D5 = D5 self.D6 = D6 self.D7 = D7 self.hi_bits = {0x10: D4, 0x20: D5, 0x40: D6, 0x80: D7} self.lo_bits = {0x01: D4, 0x02: D5, 0x04: D6, 0x08: D7} self.chars_per_line = chars_per_line self.char_mode = char_mode GPIO.setwarnings(False) # Use BCM GPIO numbers GPIO.setmode(GPIO.BCM) # Set GPIO's to output mode for pin in [E, RS, D4, D5, D6, D7]: GPIO.setup(pin, GPIO.OUT) self.write(0x33, command) # Initialize self.write(0x32, command) # Set to 4-bit mode self.write(0x06, command) # Cursor move direction self.write(0x0C, command) # Turn cursor off self.write(0x28, command) # 2 line display self.write(0x01, command) # Clear display # Delay to allow commands to process time.sleep(0.0005)
def cleanup(self): """Selective GPIO cleanup""" GPIO.setup(self.cs_pin, GPIO.IN) GPIO.setup(self.clock_pin, GPIO.IN)
def set_heat(self, value): if value > 0: self.heat = 1.0 if gpio_available: if config.heater_invert: GPIO.output(config.gpio_heat, GPIO.LOW) time.sleep(self.time_step * value) GPIO.output(config.gpio_heat, GPIO.HIGH) else: GPIO.output(config.gpio_heat, GPIO.HIGH) time.sleep(self.time_step * value) GPIO.output(config.gpio_heat, GPIO.LOW) else: # for runs that are simulations time.sleep(self.time_step * value) else: self.heat = 0.0 if gpio_available: if config.heater_invert: GPIO.output(config.gpio_heat, GPIO.HIGH) else: GPIO.output(config.gpio_heat, GPIO.LOW)
raise Exception("gpio_heat pin %s collides with SPI pins %s" % (config.gpio_heat, spi_reserved_gpio)) if config.max6675: from kilner.max6675 import MAX6675, MAX6675Error log.info("import MAX6675") sensor_available = True except ImportError: log.exception( "Could not initialize temperature sensor, using dummy values!") sensor_available = False try: from kilner.config import GPIO GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(config.gpio_heat, GPIO.OUT) # GPIO.setup(config.gpio_cool, GPIO.OUT) # GPIO.setup(config.gpio_air, GPIO.OUT) # GPIO.setup(config.gpio_door, GPIO.IN, pull_up_down=GPIO.PUD_UP) gpio_available = True except ImportError: msg = "Could not initialize GPIOs, oven operation will only be simulated!" log.warning(msg) gpio_available = False class Oven(threading.Thread): STATE_IDLE = "IDLE"
def null_bits(self): for output in [self.D4, self.D5, self.D6, self.D7]: GPIO.output(output, False)
def __del__(self): GPIO.cleanup()