class DfrobotBoard(Board): """ Board class for drobot hardware. """ def __init__(self): super(DfrobotBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings(temp_pin=3, flame_pin=2, speaker_pin=15, reference_voltage=1.0) if HARDWARE_CONFIG.platform == KNOWN_PLATFORMS.firmata: addSubplatform(GENERIC_FIRMATA, "/dev/ttyACM0") self.pin_mappings += 512 self.pin_mappings.reference_voltage = 0.33 self.temp = Aio(self.pin_mappings.temp_pin) self.flame = Aio(self.pin_mappings.flame_pin) self.speaker = Gpio(self.pin_mappings.speaker_pin) self.speaker.dir(DIR_OUT) def update_hardware_state(self): """ Update hardware state. """ obj_temp = self.read_obj_temp() self.trigger_hardware_event(TEMP_READING, obj_temp) flame_detected = self.detect_flame() self.trigger_hardware_event(FLAME_DETECTED, flame_detected) # hardware functions def read_obj_temp(self): return ((500 * self.temp.read()) / 1024) * self.pin_mappings.reference_voltage def detect_flame(self): return (self.flame.read() * self.pin_mappings.reference_voltage >= 300) def play_beep(self): self.speaker.write(1) SCHEDULER.add_job(lambda: self.speaker.write(0), "date", run_date=datetime.utcnow() + timedelta(seconds=1)) def play_temp(self): self.play_beep() def play_flame(self): self.play_beep()
class DfrobotBoard(Board): """ Board class for drobot hardware. """ def __init__(self): super(DfrobotBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings( gas_pin=3, speaker_pin=16 ) if HARDWARE_CONFIG.platform == KNOWN_PLATFORMS.firmata: addSubplatform(GENERIC_FIRMATA, "/dev/ttyACM0") self.pin_mappings += 512 self.air = MQ2(self.pin_mappings.gas_pin) self.speaker = Gpio(self.pin_mappings.speaker_pin) self.speaker.dir(DIR_OUT) def update_hardware_state(self): """ Update hardware state. """ current_air_quality = self.sample_air() self.trigger_hardware_event(AIR_QUALITY, current_air_quality) # hardware functions def play_chime(self): """ Play chime. """ self.speaker.write(1) SCHEDULER.add_job( lambda: self.speaker.write(0), "date", run_date=datetime.utcnow() + timedelta(seconds=1) ) def sample_air(self): """ Sample air. """ return self.air.getSample()
class DfrobotBoard(Board): """ Board class for drobot hardware. """ def __init__(self): super(DfrobotBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings(gas_pin=3, speaker_pin=16) if HARDWARE_CONFIG.platform == KNOWN_PLATFORMS.firmata: addSubplatform(GENERIC_FIRMATA, "/dev/ttyACM0") self.pin_mappings += 512 self.air = MQ2(self.pin_mappings.gas_pin) self.speaker = Gpio(self.pin_mappings.speaker_pin) self.speaker.dir(DIR_OUT) def update_hardware_state(self): """ Update hardware state. """ current_air_quality = self.sample_air() self.trigger_hardware_event(AIR_QUALITY, current_air_quality) # hardware functions def play_chime(self): """ Play chime. """ self.speaker.write(1) SCHEDULER.add_job(lambda: self.speaker.write(0), "date", run_date=datetime.utcnow() + timedelta(seconds=1)) def sample_air(self): """ Sample air. """ return self.air.getSample()
class DfrobotBoard(Board): """ Board class for drobot hardware. """ def __init__(self): super(DfrobotBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings(moisture_pin=2, speaker_pin=15) if HARDWARE_CONFIG.platform == KNOWN_PLATFORMS.firmata: addSubplatform(GENERIC_FIRMATA, "/dev/ttyACM0") self.pin_mappings += 512 self.moisture = Moisture(self.pin_mappings.moisture_pin) self.speaker = Gpio(self.pin_mappings.speaker_pin) self.speaker.dir(DIR_OUT) def update_hardware_state(self): """ Update hardware state. """ moisture_reading = self.read_moisture() self.trigger_hardware_event(MOISTURE_SAMPLE, moisture_reading) # hardware functions def read_moisture(self): return self.moisture.value() def play_sound(self): self.speaker.write(1) SCHEDULER.add_job(lambda: self.speaker.write(0), "date", run_date=datetime.utcnow() + timedelta(seconds=1))
class GroveBoard(Board): """ Board class for Grove hardware. """ def __init__(self): super(GroveBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings(touch_pin=4, buzzer_pin=5, i2c_bus=6) if HARDWARE_CONFIG.platform == KNOWN_PLATFORMS.firmata: addSubplatform(GENERIC_FIRMATA, "/dev/ttyACM0") self.pin_mappings += 512 self.pin_mappings.i2c_bus = 512 self.screen = Jhd1313m1(self.pin_mappings.i2c_bus, 0x3E, 0x62) self.touch = TTP223(self.pin_mappings.touch_pin) self.buzzer = Gpio(self.pin_mappings.buzzer_pin) self.buzzer.dir(DIR_OUT) self.touch_state = False self.stop_buzzer() def update_hardware_state(self): """ Update hardware state. """ current_touch_state = self.detect_touch() if self.touch_state != current_touch_state: if current_touch_state: self.trigger_hardware_event(TOUCH_DOWN) else: self.trigger_hardware_event(TOUCH_UP) self.touch_state = current_touch_state # hardware functions def detect_touch(self): """ Detect touch state. """ return self.touch.isPressed() def start_buzzer(self): """ Start buzzer. """ self.buzzer.write(1) def stop_buzzer(self): """ Stop buzzer. """ self.buzzer.write(0) def write_message(self, message, line=0): """ Write message to LCD screen. """ message = message.ljust(16) self.screen.setCursor(line, 0) self.screen.write(message) print(message) def change_background(self, color): """ Change LCD screen background color. """ colors = { "red": lambda: self.screen.setColor(255, 0, 0), "purple": lambda: self.screen.setColor(255, 0, 255), "blue": lambda: self.screen.setColor(0, 0, 255), "green": lambda: self.screen.setColor(0, 255, 0), "yellow": lambda: self.screen.setColor(255, 255, 0), "white": lambda: self.screen.setColor(255, 255, 255) } colors.get(color, colors["white"])()
class GroveBoard(Board): """ Board class for Grove hardware. """ def __init__(self): super(GroveBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings(buzzer_pin=6, button_pin=5, rotary_pin=0, i2c_bus=6) if HARDWARE_CONFIG.platform == KNOWN_PLATFORMS.firmata: addSubplatform(GENERIC_FIRMATA, "/dev/ttyACM0") self.pin_mappings += 512 self.pin_mappings.i2c_bus = 512 self.screen = Jhd1313m1(self.pin_mappings.i2c_bus, 0x3E, 0x62) self.buzzer = Gpio(self.pin_mappings.buzzer_pin) self.rotary = Rotary(self.pin_mappings.rotary_pin) self.button = Button(self.pin_mappings.button_pin) self.buzzer.dir(DIR_OUT) self.rotary_value = 0 self.button_state = False def update_hardware_state(self): """ Update hardware state. """ current_rotary_value = self.read_rotary() if current_rotary_value != self.rotary_value: self.trigger_hardware_event(ROTARY, current_rotary_value) self.rotary_value = current_rotary_value current_button_state = self.button.value() if current_button_state != self.button_state: if not current_button_state: self.trigger_hardware_event(BUTTON_RELEASED) self.button_state = current_button_state # hardware functions def start_buzzer(self): """ Start the hardware buzzer. """ self.buzzer.write(1) def stop_buzzer(self): """ Stop the hardware buzzer. """ self.buzzer.write(0) def read_rotary(self): """ Read rotary value. """ return self.rotary.abs_value() def write_message(self, message, line=0): """ Write message to LCD screen. """ message = message.ljust(16) self.screen.setCursor(line, 0) self.screen.write(message) def change_background(self, color): """ Change LCD screen background color. """ colors = { "red": lambda: self.screen.setColor(255, 0, 0), "purple": lambda: self.screen.setColor(255, 0, 255), "blue": lambda: self.screen.setColor(0, 0, 255), "green": lambda: self.screen.setColor(0, 255, 0), "yellow": lambda: self.screen.setColor(255, 255, 0), "white": lambda: self.screen.setColor(255, 255, 255) } colors.get(color, colors["white"])() def change_brightness(self, value): """ Change LCD screen brightness. """ start = 0 end = 1020 value = int(floor((value - start) / end * 255)) value = 0 if value < 0 else 255 if value > 255 else value self.screen.setColor(value, value, value)
class DfrobotBoard(Board): """ Board class for drobot hardware. """ def __init__(self): super(DfrobotBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings(moisture_pin=3, pump_pin=15, screen_register_select_pin=8, screen_enable_pin=9, screen_data_0_pin=4, screen_data_1_pin=5, screen_data_2_pin=6, screen_data_3_pin=7, screen_analog_input_pin=0) if HARDWARE_CONFIG.platform == KNOWN_PLATFORMS.firmata: addSubplatform(GENERIC_FIRMATA, "/dev/ttyACM0") self.pin_mappings += 512 self.screen = LCDKS(self.pin_mappings.screen_register_select_pin, self.pin_mappings.screen_enable_pin, self.pin_mappings.screen_data_0_pin, self.pin_mappings.screen_data_1_pin, self.pin_mappings.screen_data_2_pin, self.pin_mappings.screen_data_3_pin, self.pin_mappings.screen_analog_input_pin) self.moisture = Moisture(self.pin_mappings.moisture_pin) self.pump = Gpio(self.pin_mappings.pump_pin) self.pump.dir(DIR_OUT) def update_hardware_state(self): """ Update hardware state. """ pass # hardware functions def pump_on(self): self.pump.write(1) def pump_off(self): self.pump.write(0) def detect_flow(self): return True if self.moisture.value() > 10 else False def detect_nonflow(self): return True def sample_moisture(self): return self.moisture.value() def write_message(self, message, line=0): """ Write message to LCD screen. """ message = message.ljust(16) self.screen.setCursor(line, 0) self.screen.write(message) def change_background(self, color): """ Change LCD screen background color. No effect on the dfrobot. """ pass
class GroveBoard(Board): """ Board class for Grove hardware. """ def __init__(self): super(GroveBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings( touch_pin=4, buzzer_pin=5, i2c_bus=6 ) if HARDWARE_CONFIG.platform == KNOWN_PLATFORMS.firmata: addSubplatform(GENERIC_FIRMATA, "/dev/ttyACM0") self.pin_mappings += 512 self.pin_mappings.i2c_bus = 512 self.screen = Jhd1313m1(self.pin_mappings.i2c_bus, 0x3E, 0x62) self.touch = TTP223(self.pin_mappings.touch_pin) self.buzzer = Gpio(self.pin_mappings.buzzer_pin) self.buzzer.dir(DIR_OUT) self.touch_state = False self.stop_buzzer() def update_hardware_state(self): """ Update hardware state. """ current_touch_state = self.detect_touch() if self.touch_state != current_touch_state: if current_touch_state: self.trigger_hardware_event(TOUCH_DOWN) else: self.trigger_hardware_event(TOUCH_UP) self.touch_state = current_touch_state # hardware functions def detect_touch(self): """ Detect touch state. """ return self.touch.isPressed() def start_buzzer(self): """ Start buzzer. """ self.buzzer.write(1) def stop_buzzer(self): """ Stop buzzer. """ self.buzzer.write(0) def write_message(self, message, line=0): """ Write message to LCD screen. """ message = message.ljust(16) self.screen.setCursor(line, 0) self.screen.write(message) print(message) def change_background(self, color): """ Change LCD screen background color. """ colors = { "red": lambda: self.screen.setColor(255, 0, 0), "purple": lambda: self.screen.setColor(255, 0, 255), "blue": lambda: self.screen.setColor(0, 0, 255), "green": lambda: self.screen.setColor(0, 255, 0), "yellow": lambda: self.screen.setColor(255, 255, 0), "white": lambda: self.screen.setColor(255, 255, 255) } colors.get(color, colors["white"])()
class DfrobotBoard(Board): """ Board class for drobot hardware. """ def __init__(self): super(DfrobotBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings( touch_pin=16, buzzer_pin=15, screen_register_select_pin=8, screen_enable_pin=9, screen_data_0_pin=4, screen_data_1_pin=5, screen_data_2_pin=6, screen_data_3_pin=7, screen_analog_input_pin=0 ) if HARDWARE_CONFIG.platform == KNOWN_PLATFORMS.firmata: addSubplatform(GENERIC_FIRMATA, "/dev/ttyACM0") self.pin_mappings += 512 self.screen = SAINSMARTKS( self.pin_mappings.screen_register_select_pin, self.pin_mappings.screen_enable_pin, self.pin_mappings.screen_data_0_pin, self.pin_mappings.screen_data_1_pin, self.pin_mappings.screen_data_2_pin, self.pin_mappings.screen_data_3_pin, self.pin_mappings.screen_analog_input_pin ) self.touch = Button(self.pin_mappings.touch_pin) self.buzzer = Gpio(self.pin_mappings.buzzer_pin) self.buzzer.dir(DIR_OUT) self.touch_state = False self.stop_buzzer() def update_hardware_state(self): """ Update hardware state. """ current_touch_state = self.detect_touch() if self.touch_state != current_touch_state: if current_touch_state: self.trigger_hardware_event(TOUCH_DOWN) else: self.trigger_hardware_event(TOUCH_UP) self.touch_state = current_touch_state # hardware functions def detect_touch(self): """ Detect touch state. """ return self.touch.value() def start_buzzer(self): """ Start buzzer. """ self.buzzer.write(1) def stop_buzzer(self): """ Stop buzzer. """ self.buzzer.write(0) def write_message(self, message, line=0): """ Write message to LCD screen. """ message = message.ljust(16) self.screen.setCursor(line, 0) self.screen.write(message) print(message) def change_background(self, color): """ Change LCD screen background color. No effect on the dfrobot. """ pass
def init_gpio(pin, dir=mraa.DIR_OUT_LOW, mode=mraa.MODE_STRONG): gpio = Gpio(pin) gpio.dir(dir) gpio.mode(mode) return gpio
class GroveBoard(Board): """ Board class for Grove hardware. """ def __init__(self): super(GroveBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings(buzzer_pin=6, button_pin=5, rotary_pin=0, i2c_bus=6) if HARDWARE_CONFIG.platform == KNOWN_PLATFORMS.firmata: addSubplatform(GENERIC_FIRMATA, "/dev/ttyACM0") self.pin_mappings += 512 self.pin_mappings.i2c_bus = 512 self.screen = Jhd1313m1(self.pin_mappings.i2c_bus, 0x3E, 0x62) self.buzzer = Gpio(self.pin_mappings.buzzer_pin) self.rotary = Rotary(self.pin_mappings.rotary_pin) self.button = Button(self.pin_mappings.button_pin) self.buzzer.dir(DIR_OUT) self.rotary_value = 0 self.button_state = False def update_hardware_state(self): """ Update hardware state. """ current_rotary_value = self.read_rotary() if current_rotary_value != self.rotary_value: self.trigger_hardware_event(ROTARY, current_rotary_value) self.rotary_value = current_rotary_value current_button_state = self.button.value() if current_button_state != self.button_state: if not current_button_state: self.trigger_hardware_event(BUTTON_RELEASED) self.button_state = current_button_state # hardware functions def start_buzzer(self): """ Start the hardware buzzer. """ self.buzzer.write(1) def stop_buzzer(self): """ Stop the hardware buzzer. """ self.buzzer.write(0) def read_rotary(self): """ Read rotary value. """ return self.rotary.abs_value() def write_message(self, message, line=0): """ Write message to LCD screen. """ message = message.ljust(16) self.screen.setCursor(line, 0) self.screen.write(message) def change_background(self, color): """ Change LCD screen background color. """ colors = { "red": lambda: self.screen.setColor(255, 0, 0), "purple": lambda: self.screen.setColor(255, 0, 255), "blue": lambda: self.screen.setColor(0, 0, 255), "green": lambda: self.screen.setColor(0, 255, 0), "yellow": lambda: self.screen.setColor(255, 255, 0), "white": lambda: self.screen.setColor(255, 255, 255), } colors.get(color, colors["white"])() def change_brightness(self, value): """ Change LCD screen brightness. """ start = 0 end = 1020 value = int(floor((value - start) / end * 255)) value = 0 if value < 0 else 255 if value > 255 else value self.screen.setColor(value, value, value)
class GroveBoard(Board): """ Board class for Grove hardware. """ def __init__(self): super(GroveBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings(temperature_pin=0, buzzer_pin=5, i2c_bus=6) self.voltage_adjust = 1.0 if HARDWARE_CONFIG.platform == KNOWN_PLATFORMS.firmata: addSubplatform(GENERIC_FIRMATA, "/dev/ttyACM0") self.pin_mappings += 512 self.pin_mappings.i2c_bus = 512 self.voltage_adjust = 0.66 self.screen = Jhd1313m1(self.pin_mappings.i2c_bus, 0x3E, 0x62) self.temperature = Temperature(self.pin_mappings.temperature_pin) self.buzzer = Gpio(self.pin_mappings.buzzer_pin) self.buzzer.dir(DIR_OUT) def update_hardware_state(self): """ Update hardware state. """ current_temp = self.read_temperature() self.trigger_hardware_event(TEMPRETURE_CHANGED, current_temp) # hardware functions def read_temperature(self): """ Read temperature value in Celcius. """ return self.temperature.value() * self.voltage_adjust def start_buzzer(self): """ Start buzzer. """ self.buzzer.write(1) def stop_buzzer(self): """ Stop buzzer. """ self.buzzer.write(0) def write_message(self, message, line=0): """ Write message to LCD screen. """ message = message.ljust(16) self.screen.setCursor(line, 0) self.screen.write(message) def change_background(self, color): """ Change LCD screen background color. """ colors = { "red": lambda: self.screen.setColor(255, 0, 0), "purple": lambda: self.screen.setColor(255, 0, 255), "blue": lambda: self.screen.setColor(0, 0, 255), "green": lambda: self.screen.setColor(0, 255, 0), "yellow": lambda: self.screen.setColor(255, 255, 0), "white": lambda: self.screen.setColor(255, 255, 255) } colors.get(color, colors["white"])()
class DfrobotBoard(Board): """ Board class for drobot hardware. """ def __init__(self): super(DfrobotBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings( temperature_pin=2, buzzer_pin=15, screen_register_select_pin=8, screen_enable_pin=9, screen_data_0_pin=4, screen_data_1_pin=5, screen_data_2_pin=6, screen_data_3_pin=7, screen_analog_input_pin=0 ) self.voltage_adjust = 1.0 if HARDWARE_CONFIG.platform == KNOWN_PLATFORMS.firmata: addSubplatform(GENERIC_FIRMATA, "/dev/ttyACM0") self.pin_mappings += 512 self.voltage_adjust = 0.66 self.screen = SAINSMARTKS( self.pin_mappings.screen_register_select_pin, self.pin_mappings.screen_enable_pin, self.pin_mappings.screen_data_0_pin, self.pin_mappings.screen_data_1_pin, self.pin_mappings.screen_data_2_pin, self.pin_mappings.screen_data_3_pin, self.pin_mappings.screen_analog_input_pin ) self.temperature = LM35(self.pin_mappings.temperature_pin) self.temperature.setScale(self.voltage_adjust) self.buzzer = Gpio(self.pin_mappings.buzzer_pin) self.buzzer.dir(DIR_OUT) def update_hardware_state(self): """ Update hardware state. """ current_temp = self.read_temperature() self.trigger_hardware_event(TEMPRETURE_CHANGED, current_temp) # hardware functions def read_temperature(self): """ Read temperature value in Celcius. """ return self.temperature.getTemperature() * self.voltage_adjust def start_buzzer(self): """ Start buzzer. """ self.buzzer.write(1) def stop_buzzer(self): """ Stop buzzer. """ self.buzzer.write(0) def write_message(self, message, line=0): """ Write message to LCD screen. """ message = message.ljust(16) self.screen.setCursor(line, 0) self.screen.write(message) def change_background(self, color): """ Change LCD screen background color. No effect on the dfrobot. """ pass
class DfrobotBoard(Board): """ Board class for drobot hardware. """ def __init__(self): super(DfrobotBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings(touch_pin=16, buzzer_pin=15, screen_register_select_pin=8, screen_enable_pin=9, screen_data_0_pin=4, screen_data_1_pin=5, screen_data_2_pin=6, screen_data_3_pin=7, screen_analog_input_pin=0) if HARDWARE_CONFIG.platform == KNOWN_PLATFORMS.firmata: addSubplatform(GENERIC_FIRMATA, "/dev/ttyACM0") self.pin_mappings += 512 self.screen = SAINSMARTKS(self.pin_mappings.screen_register_select_pin, self.pin_mappings.screen_enable_pin, self.pin_mappings.screen_data_0_pin, self.pin_mappings.screen_data_1_pin, self.pin_mappings.screen_data_2_pin, self.pin_mappings.screen_data_3_pin, self.pin_mappings.screen_analog_input_pin) self.touch = Button(self.pin_mappings.touch_pin) self.buzzer = Gpio(self.pin_mappings.buzzer_pin) self.buzzer.dir(DIR_OUT) self.touch_state = False self.stop_buzzer() def update_hardware_state(self): """ Update hardware state. """ current_touch_state = self.detect_touch() if self.touch_state != current_touch_state: if current_touch_state: self.trigger_hardware_event(TOUCH_DOWN) else: self.trigger_hardware_event(TOUCH_UP) self.touch_state = current_touch_state # hardware functions def detect_touch(self): """ Detect touch state. """ return self.touch.value() def start_buzzer(self): """ Start buzzer. """ self.buzzer.write(1) def stop_buzzer(self): """ Stop buzzer. """ self.buzzer.write(0) def write_message(self, message, line=0): """ Write message to LCD screen. """ message = message.ljust(16) self.screen.setCursor(line, 0) self.screen.write(message) print(message) def change_background(self, color): """ Change LCD screen background color. No effect on the dfrobot. """ pass
class GroveBoard(Board): """ Board class for Grove hardware. """ def __init__(self): super(GroveBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings( temperature_pin=0, buzzer_pin=5, i2c_bus=6 ) self.voltage_adjust = 1.0 if HARDWARE_CONFIG.platform == KNOWN_PLATFORMS.firmata: addSubplatform(GENERIC_FIRMATA, "/dev/ttyACM0") self.pin_mappings += 512 self.pin_mappings.i2c_bus = 512 self.voltage_adjust = 0.66 self.screen = Jhd1313m1(self.pin_mappings.i2c_bus, 0x3E, 0x62) self.temperature = Temperature(self.pin_mappings.temperature_pin) self.buzzer = Gpio(self.pin_mappings.buzzer_pin) self.buzzer.dir(DIR_OUT) def update_hardware_state(self): """ Update hardware state. """ current_temp = self.read_temperature() self.trigger_hardware_event(TEMPRETURE_CHANGED, current_temp) # hardware functions def read_temperature(self): """ Read temperature value in Celcius. """ return self.temperature.value() * self.voltage_adjust def start_buzzer(self): """ Start buzzer. """ self.buzzer.write(1) def stop_buzzer(self): """ Stop buzzer. """ self.buzzer.write(0) def write_message(self, message, line=0): """ Write message to LCD screen. """ message = message.ljust(16) self.screen.setCursor(line, 0) self.screen.write(message) def change_background(self, color): """ Change LCD screen background color. """ colors = { "red": lambda: self.screen.setColor(255, 0, 0), "purple": lambda: self.screen.setColor(255, 0, 255), "blue": lambda: self.screen.setColor(0, 0, 255), "green": lambda: self.screen.setColor(0, 255, 0), "yellow": lambda: self.screen.setColor(255, 255, 0), "white": lambda: self.screen.setColor(255, 255, 255) } colors.get(color, colors["white"])()
class Seg7(object): """ An object representation of a seven-segment which defaults to parameters used by the Calamari lure. This class assumes you control the seven-segment by bit-banging the data to a shift register. If you are not using a Calamari lure, then adjust the pin numbers, and possibly the char mapping. The critical time may not make a significant difference but it is included for completeness. """ # Default symbols translated to work on the Calamari lure. default_chars = {'1': 0b10000010, '2': 0b00011111, '3': 0b01011101, '4': 0b11010100, '5': 0b11001101, '6': 0b11001111, '7': 0b01011000, '8': 0b11011111, '9': 0b11011100, 'a': 0b11011110, 'b': 0b11000111, 'c': 0b00000111, 'd': 0b01010111, 'e': 0b10001111, 'f': 0b10001110, '0': 0b11011011, ' ': 0b00000000, '.': 0b00100000, '@': 0b11111111, } # The critical time for the sev-segment's shift-register to receive data. default_shift_ct = (10**-8) def __init__(self, delay=None, chars=None, shift_ct=None, clock_pin=25, latch_pin=18, data_pin=20, clear_pin=16): """ Initialize a Seg7 object that defaults to using the Calamari pins. delay [float] : The desired amount of time, in seconds, to sleep after a character is written. Defaults to : 1.0 chars [dict] : A dict mapping single character keys to byte items used by putc and enabling users to write raw data to the display in the form of human readable symbols. Keys [str] : A single character symbol. Items [int] : An int [0-255] of the translated symbol. Defaults to : self.default_chars (for a Calamari 7seg) shift_ct [float] : The amount of time to wait after shifting values to the device's register such that we are sure the correct bit was received. Defaults to: 10**(-8), for the Calamari Lure. *_pin [int] : The GPIO pins used to drive the 7seg hardware. Defaults to : Calamari pins: clock_pin = 25 latch_pin = 18 data_pin = 20 clear_pin = 16 """ from mraa import Gpio, DIR_OUT_LOW self.delay = 1.0 if delay is None else delay self.chars = self.default_chars if chars is None else chars self.sct = self.default_shift_ct if shift_ct is None else shift_ct self.clock_pin = Gpio(clock_pin) self.latch_pin = Gpio(latch_pin) self.data_pin = Gpio(data_pin) self.clear_pin = Gpio(clear_pin) self.clock_pin.dir(DIR_OUT_LOW) self.latch_pin.dir(DIR_OUT_LOW) self.data_pin.dir(DIR_OUT_LOW) self.clear_pin.dir(DIR_OUT_LOW) self.data_pin.write(0) self.clear_pin.write(0) self.clear() self.putc('.') def _tick(self): """ Drive the clock pin high and then low to allow the shift register to update its value. """ self.clock_pin.write(1) self.clock_pin.write(0) def _shift0(self): """ A zero bit is shifted into the seven-segment register. """ self.data_pin.write(1) self._tick() def _shift1(self): """ A one bit is shifted into the seven-segment register. """ self.data_pin.write(0) self._tick() def _latch(self): """ Update the segments with the current value in the register. """ self.latch_pin.write(0) self.latch_pin.write(1) self.latch_pin.write(0) def write(self, byte): """ Explicitly write any arbitrary byte to the seven-segment. byte [int] : A non-negative integer less than 256. """ from time import sleep if byte not in xrange(0, 256): raise ValueError("write's argument is not a byte [0-255]") byte_str = format(byte, "#010b")[2:] for bit in byte_str: if bit == '1': self._shift1() else: self._shift0() sleep(self.sct) # wait for the bit to be shifted over self._latch() def clear(self): """ Cycle the clear pin and then write all zero's to the segment. """ self.clear_pin.write(1) self.clear_pin.write(0) self.clear_pin.write(1) self.write(0) def putc(self, char, delay=None): """ Put a character symbol on the display, if it was defined. char [str] : A length 1 string corresponding to a key in self.chars delay [float] : The amount of time to sleep after displaying the char. Defaults to: 0.0 """ from time import sleep if delay is None: delay = 0.0 binary = self.chars.get(char.lower()) if binary is None: raise ValueError("Seg7 will only display hexadecimal symbols") self.write(binary) sleep(delay) def puts(self, string, delay=None): """ Write the contents of a string with a delay between each character. string [str] : the actual string to display delay [float] : the amount of time to sleep after showing each symbol """ if delay is None: delay = self.delay delay = self.delay if delay is None else delay for char in string: self.putc(char, delay) def blinkc(self, char, period=None, duty=None, cycles=None): """ Blink a character on a duty cycle over a number of periods. char [str] : The character displayed period [float] : The amount of time, in seconds, in a full period Defaults to: 0.25 duty [float] : Percentage of the period that the segment is lit. Should be between 0.0 and 1.0 Defaults to: 0.5 cycles [int] : The number of periods to display Defaults to: 5 """ from time import sleep if period is None: period = 0.25 elif period < 0.0: raise ValueError("period < 0.0") if duty is None: duty = 0.5 elif duty > 1.0: raise ValueError("duty > 1.0") elif duty < 0.0: raise ValueError("duty < 0.0") if cycles is None: cycles = 5 elif cycles < 0: raise ValueError("cycles < 0") elif not isinstance(cycles, int): raise TypeError("cycles must be int") for blink in range(cycles): self.putc(char, period * duty) self.clear() sleep(period * (1.0 - duty)) def blink(self, char, period=None, duty=None, elapse=None): """ Blink a character on a duty cycle for an elapsed time. char [char] : The character displayed period [float] : The amount of time, in seconds, of a full period Defaults to: 0.25 duty [float] : Percentage of the period that the segment is lit. Should be between 0.0 and 1.0 Defaults to: 0.5 elapse [float] : The amount of time, in seconds, to run this function. Defaults to: 5.0 """ from time import sleep, time if period is None: period = 0.25 elif period < 0.0: raise ValueError("period < 0.0") if duty is None: duty = 0.5 elif duty > 1.0: raise ValueError("duty > 1.0") elif duty < 0.0: raise ValueError("duty < 0.0") if elapse is None: elapse = 5.0 elif elapse < 0.0: raise ValueError("elapse < 0.0") start = time() finish = start while(finish - start < elapse): self.putc(char, period * duty) self.clear() sleep(period * (1.0 - duty)) finish = time()
class DfrobotBoard(Board): """ Board class for drobot hardware. """ def __init__(self): super(DfrobotBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings( buzzer_pin=15, button_pin=16, rotary_pin=3, screen_register_select_pin=8, screen_enable_pin=9, screen_data_0_pin=4, screen_data_1_pin=5, screen_data_2_pin=6, screen_data_3_pin=7, screen_analog_input_pin=0, ) if HARDWARE_CONFIG.platform == KNOWN_PLATFORMS.firmata: addSubplatform(GENERIC_FIRMATA, "/dev/ttyACM0") self.pin_mappings += 512 self.screen = SAINSMARTKS( self.pin_mappings.screen_register_select_pin, self.pin_mappings.screen_enable_pin, self.pin_mappings.screen_data_0_pin, self.pin_mappings.screen_data_1_pin, self.pin_mappings.screen_data_2_pin, self.pin_mappings.screen_data_3_pin, self.pin_mappings.screen_analog_input_pin, ) self.buzzer = Gpio(self.pin_mappings.buzzer_pin) self.rotary = Rotary(self.pin_mappings.rotary_pin) self.button = Button(self.pin_mappings.button_pin) self.buzzer.dir(DIR_OUT) self.rotary_value = 0 self.button_state = False def update_hardware_state(self): """ Update hardware state. """ current_rotary_value = self.read_rotary() if current_rotary_value != self.rotary_value: self.trigger_hardware_event(ROTARY, current_rotary_value) self.rotary_value = current_rotary_value current_button_state = self.button.value() if current_button_state != self.button_state: if not current_button_state: self.trigger_hardware_event(BUTTON_RELEASED) self.button_state = current_button_state # hardware functions def start_buzzer(self): """ Start the hardware buzzer. """ self.buzzer.write(1) def stop_buzzer(self): """ Stop the hardware buzzer. """ self.buzzer.write(0) def read_rotary(self): """ Read rotary value. """ return self.rotary.abs_value() def write_message(self, message, line=0): """ Write message to LCD screen. """ message = message.ljust(16) self.screen.setCursor(line, 0) self.screen.write(message) def change_background(self, color): """ Change LCD screen background color. No effect on the dfrobot. """ pass def change_brightness(self, value): """ Change LCD screen brightness. Only supports on (value < 512) and off (value >= 512) on the dfrobot. """ if value < 512: self.screen.displayOff() else: self.screen.displayOn()
class Seg7(object): """ An object representation of a seven-segment which defaults to parameters used by the Calamari lure. This class assumes you control the seven-segment by bit-banging the data to a shift register. If you are not using a Calamari lure, then adjust the pin numbers, and possibly the char mapping. The critical time may not make a significant difference but it is included for completeness. """ # Default symbols translated to work on the Calamari lure. default_chars = { '1': 0b10000010, '2': 0b00011111, '3': 0b01011101, '4': 0b11010100, '5': 0b11001101, '6': 0b11001111, '7': 0b01011000, '8': 0b11011111, '9': 0b11011100, 'a': 0b11011110, 'b': 0b11000111, 'c': 0b00000111, 'd': 0b01010111, 'e': 0b10001111, 'f': 0b10001110, '0': 0b11011011, ' ': 0b00000000, '.': 0b00100000, '@': 0b11111111, } # The critical time for the sev-segment's shift-register to receive data. default_shift_ct = (10**-8) def __init__(self, delay=None, chars=None, shift_ct=None, clock_pin=25, latch_pin=18, data_pin=20, clear_pin=16): """ Initialize a Seg7 object that defaults to using the Calamari pins. delay [float] : The desired amount of time, in seconds, to sleep after a character is written. Defaults to : 1.0 chars [dict] : A dict mapping single character keys to byte items used by putc and enabling users to write raw data to the display in the form of human readable symbols. Keys [str] : A single character symbol. Items [int] : An int [0-255] of the translated symbol. Defaults to : self.default_chars (for a Calamari 7seg) shift_ct [float] : The amount of time to wait after shifting values to the device's register such that we are sure the correct bit was received. Defaults to: 10**(-8), for the Calamari Lure. *_pin [int] : The GPIO pins used to drive the 7seg hardware. Defaults to : Calamari pins: clock_pin = 25 latch_pin = 18 data_pin = 20 clear_pin = 16 """ from mraa import Gpio, DIR_OUT_LOW self.delay = 1.0 if delay is None else delay self.chars = self.default_chars if chars is None else chars self.sct = self.default_shift_ct if shift_ct is None else shift_ct self.clock_pin = Gpio(clock_pin) self.latch_pin = Gpio(latch_pin) self.data_pin = Gpio(data_pin) self.clear_pin = Gpio(clear_pin) self.clock_pin.dir(DIR_OUT_LOW) self.latch_pin.dir(DIR_OUT_LOW) self.data_pin.dir(DIR_OUT_LOW) self.clear_pin.dir(DIR_OUT_LOW) self.data_pin.write(0) self.clear_pin.write(0) self.clear() self.putc('.') def _tick(self): """ Drive the clock pin high and then low to allow the shift register to update its value. """ self.clock_pin.write(1) self.clock_pin.write(0) def _shift0(self): """ A zero bit is shifted into the seven-segment register. """ self.data_pin.write(1) self._tick() def _shift1(self): """ A one bit is shifted into the seven-segment register. """ self.data_pin.write(0) self._tick() def _latch(self): """ Update the segments with the current value in the register. """ self.latch_pin.write(0) self.latch_pin.write(1) self.latch_pin.write(0) def write(self, byte): """ Explicitly write any arbitrary byte to the seven-segment. byte [int] : A non-negative integer less than 256. """ from time import sleep if byte not in xrange(0, 256): raise ValueError("write's argument is not a byte [0-255]") byte_str = format(byte, "#010b")[2:] for bit in byte_str: if bit == '1': self._shift1() else: self._shift0() sleep(self.sct) # wait for the bit to be shifted over self._latch() def clear(self): """ Cycle the clear pin and then write all zero's to the segment. """ self.clear_pin.write(1) self.clear_pin.write(0) self.clear_pin.write(1) self.write(0) def putc(self, char, delay=None): """ Put a character symbol on the display, if it was defined. char [str] : A length 1 string corresponding to a key in self.chars delay [float] : The amount of time to sleep after displaying the char. Defaults to: 0.0 """ from time import sleep if delay is None: delay = 0.0 binary = self.chars.get(char.lower()) if binary is None: raise ValueError("Seg7 will only display hexadecimal symbols") self.write(binary) sleep(delay) def puts(self, string, delay=None): """ Write the contents of a string with a delay between each character. string [str] : the actual string to display delay [float] : the amount of time to sleep after showing each symbol """ if delay is None: delay = self.delay delay = self.delay if delay is None else delay for char in string: self.putc(char, delay) def blinkc(self, char, period=None, duty=None, cycles=None): """ Blink a character on a duty cycle over a number of periods. char [str] : The character displayed period [float] : The amount of time, in seconds, in a full period Defaults to: 0.25 duty [float] : Percentage of the period that the segment is lit. Should be between 0.0 and 1.0 Defaults to: 0.5 cycles [int] : The number of periods to display Defaults to: 5 """ from time import sleep if period is None: period = 0.25 elif period < 0.0: raise ValueError("period < 0.0") if duty is None: duty = 0.5 elif duty > 1.0: raise ValueError("duty > 1.0") elif duty < 0.0: raise ValueError("duty < 0.0") if cycles is None: cycles = 5 elif cycles < 0: raise ValueError("cycles < 0") elif not isinstance(cycles, int): raise TypeError("cycles must be int") for blink in range(cycles): self.putc(char, period * duty) self.clear() sleep(period * (1.0 - duty)) def blink(self, char, period=None, duty=None, elapse=None): """ Blink a character on a duty cycle for an elapsed time. char [char] : The character displayed period [float] : The amount of time, in seconds, of a full period Defaults to: 0.25 duty [float] : Percentage of the period that the segment is lit. Should be between 0.0 and 1.0 Defaults to: 0.5 elapse [float] : The amount of time, in seconds, to run this function. Defaults to: 5.0 """ from time import sleep, time if period is None: period = 0.25 elif period < 0.0: raise ValueError("period < 0.0") if duty is None: duty = 0.5 elif duty > 1.0: raise ValueError("duty > 1.0") elif duty < 0.0: raise ValueError("duty < 0.0") if elapse is None: elapse = 5.0 elif elapse < 0.0: raise ValueError("elapse < 0.0") start = time() finish = start while (finish - start < elapse): self.putc(char, period * duty) self.clear() sleep(period * (1.0 - duty)) finish = time()
class DfrobotBoard(Board): """ Board class for drobot hardware. """ def __init__(self): super(DfrobotBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings(buzzer_pin=15, button_pin=16, rotary_pin=3, screen_register_select_pin=8, screen_enable_pin=9, screen_data_0_pin=4, screen_data_1_pin=5, screen_data_2_pin=6, screen_data_3_pin=7, screen_analog_input_pin=0) if HARDWARE_CONFIG.platform == KNOWN_PLATFORMS.firmata: addSubplatform(GENERIC_FIRMATA, "/dev/ttyACM0") self.pin_mappings += 512 self.screen = LCDKS(self.pin_mappings.screen_register_select_pin, self.pin_mappings.screen_enable_pin, self.pin_mappings.screen_data_0_pin, self.pin_mappings.screen_data_1_pin, self.pin_mappings.screen_data_2_pin, self.pin_mappings.screen_data_3_pin, self.pin_mappings.screen_analog_input_pin) self.buzzer = Gpio(self.pin_mappings.buzzer_pin) self.rotary = Rotary(self.pin_mappings.rotary_pin) self.button = Button(self.pin_mappings.button_pin) self.buzzer.dir(DIR_OUT) self.rotary_value = 0 self.button_state = False def update_hardware_state(self): """ Update hardware state. """ current_rotary_value = self.read_rotary() if current_rotary_value != self.rotary_value: self.trigger_hardware_event(ROTARY, current_rotary_value) self.rotary_value = current_rotary_value current_button_state = self.button.value() if current_button_state != self.button_state: if not current_button_state: self.trigger_hardware_event(BUTTON_RELEASED) self.button_state = current_button_state # hardware functions def start_buzzer(self): """ Start the hardware buzzer. """ self.buzzer.write(1) def stop_buzzer(self): """ Stop the hardware buzzer. """ self.buzzer.write(0) def read_rotary(self): """ Read rotary value. """ return self.rotary.abs_value() def write_message(self, message, line=0): """ Write message to LCD screen. """ message = message.ljust(16) self.screen.setCursor(line, 0) self.screen.write(message) def change_background(self, color): """ Change LCD screen background color. No effect on the dfrobot. """ pass def change_brightness(self, value): """ Change LCD screen brightness. Only supports on (value < 512) and off (value >= 512) on the dfrobot. """ if value < 512: self.screen.displayOff() else: self.screen.displayOn()
class DfrobotBoard(Board): """ Board class for drobot hardware. """ def __init__(self): super(DfrobotBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings(temperature_pin=2, buzzer_pin=15, screen_register_select_pin=8, screen_enable_pin=9, screen_data_0_pin=4, screen_data_1_pin=5, screen_data_2_pin=6, screen_data_3_pin=7, screen_analog_input_pin=0) self.voltage_adjust = 1.0 if HARDWARE_CONFIG.platform == KNOWN_PLATFORMS.firmata: addSubplatform(GENERIC_FIRMATA, "/dev/ttyACM0") self.pin_mappings += 512 self.voltage_adjust = 0.66 self.screen = LCDKS(self.pin_mappings.screen_register_select_pin, self.pin_mappings.screen_enable_pin, self.pin_mappings.screen_data_0_pin, self.pin_mappings.screen_data_1_pin, self.pin_mappings.screen_data_2_pin, self.pin_mappings.screen_data_3_pin, self.pin_mappings.screen_analog_input_pin) self.temperature = LM35(self.pin_mappings.temperature_pin) self.temperature.setScale(self.voltage_adjust) self.buzzer = Gpio(self.pin_mappings.buzzer_pin) self.buzzer.dir(DIR_OUT) def update_hardware_state(self): """ Update hardware state. """ current_temp = self.read_temperature() self.trigger_hardware_event(TEMPRETURE_CHANGED, current_temp) # hardware functions def read_temperature(self): """ Read temperature value in Celcius. """ return self.temperature.getTemperature() * self.voltage_adjust def start_buzzer(self): """ Start buzzer. """ self.buzzer.write(1) def stop_buzzer(self): """ Stop buzzer. """ self.buzzer.write(0) def write_message(self, message, line=0): """ Write message to LCD screen. """ message = message.ljust(16) self.screen.setCursor(line, 0) self.screen.write(message) def change_background(self, color): """ Change LCD screen background color. No effect on the dfrobot. """ pass