def __init__(self): super(GroveBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings( sound_pin=0, vibration_pin=2, 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.sound = Microphone(self.pin_mappings.sound_pin) self.sound_ctx = thresholdContext() self.sound_ctx.averageReading = 0 self.sound_ctx.runningAverage = 0 self.sound_ctx.averagedOver = 2 self.vibration = LDT0028(self.pin_mappings.vibration_pin) self.sample_time = 2 self.sample_number = 128
def __init__(self): super(GroveBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings(mic_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.mic = Microphone(self.pin_mappings.mic_pin) self.screen = Jhd1313m1(self.pin_mappings.i2c_bus, 0x3E, 0x62) self.mic_ctx = micThresholdContext() self.mic_ctx.averageReading = 0 self.mic_ctx.runningAverage = 0 self.mic_ctx.averagedOver = 2
def __init__(self): super(DfrobotBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings( sound_pin=2, vibration_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.sound = Microphone(self.pin_mappings.sound_pin) self.sound_ctx = thresholdContext() self.sound_ctx.averageReading = 0 self.sound_ctx.runningAverage = 0 self.sound_ctx.averagedOver = 2 self.vibration = Button(self.pin_mappings.vibration_pin) self.sample_time = 2 self.sample_number = 128
def __init__(self): super(GroveBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings( mic_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.mic = Microphone(self.pin_mappings.mic_pin) self.screen = Jhd1313m1(self.pin_mappings.i2c_bus, 0x3E, 0x62) self.mic_ctx = micThresholdContext() self.mic_ctx.averageReading = 0 self.mic_ctx.runningAverage = 0 self.mic_ctx.averagedOver = 2
class GroveBoard(Board): """ Board class for Grove hardware. """ def __init__(self): super(GroveBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings( mic_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.mic = Microphone(self.pin_mappings.mic_pin) self.screen = Jhd1313m1(self.pin_mappings.i2c_bus, 0x3E, 0x62) self.mic_ctx = micThresholdContext() self.mic_ctx.averageReading = 0 self.mic_ctx.runningAverage = 0 self.mic_ctx.averagedOver = 2 def update_hardware_state(self): """ Update hardware state. """ self.trigger_hardware_event(SOUND_MEASUREMENT, self.measure_volume()) # hardware functions def measure_volume(self): """ Measure the average volume """ samples = uint16Array(128) length = self.mic.getSampledWindow(2, 128, samples) if not length: return 0 noise = self.mic.findThreshold(self.mic_ctx, 30, samples, length) average = noise / 100 return average 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 GroveBoard(Board): """ Board class for Grove hardware. """ def __init__(self): super(GroveBoard, self).__init__() # pin mappings self.pin_mappings = PinMappings( sound_pin=0, vibration_pin=2, 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.sound = Microphone(self.pin_mappings.sound_pin) self.sound_ctx = thresholdContext() self.sound_ctx.averageReading = 0 self.sound_ctx.runningAverage = 0 self.sound_ctx.averagedOver = 2 self.vibration = LDT0028(self.pin_mappings.vibration_pin) self.sample_time = 2 self.sample_number = 128 def update_hardware_state(self): """ Update hardware state. """ vibration_sample = self.measure_vibration() self.trigger_hardware_event(VIBRATION_SAMPLE, vibration_sample) noise_sample = self.measure_sound() self.trigger_hardware_event(NOISE_SAMPLE, noise_sample) # hardware functions def measure_vibration(self): """ Measure average vibration. """ avg_sample = 0 for _ in range(0, self.sample_number): avg_sample += self.vibration.getSample() sleep(self.sample_time * 0.000001) avg_sample /= self.sample_number return avg_sample def measure_sound(self): """ Measure average volume. """ samples = uint16Array(128) length = self.sound.getSampledWindow(self.sample_time, self.sample_number, samples) if not length: return 0 noise = self.sound.findThreshold(self.sound_ctx, 30, samples, length) average = noise / 100 return average 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 = { "clear": lambda: self.screen.setColor(0, 0, 0), "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( sound_pin=2, vibration_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.sound = Microphone(self.pin_mappings.sound_pin) self.sound_ctx = thresholdContext() self.sound_ctx.averageReading = 0 self.sound_ctx.runningAverage = 0 self.sound_ctx.averagedOver = 2 self.vibration = Button(self.pin_mappings.vibration_pin) self.sample_time = 2 self.sample_number = 128 def update_hardware_state(self): """ Update hardware state. """ vibration_sample = self.measure_vibration() self.trigger_hardware_event(VIBRATION_SAMPLE, vibration_sample) noise_sample = self.measure_sound() self.trigger_hardware_event(NOISE_SAMPLE, noise_sample) # hardware functions def measure_vibration(self): """ Measure average vibration. """ vibration = self.vibration.value() return 200 if vibration else 0 def measure_sound(self): """ Measure average volume. """ samples = uint16Array(128) length = self.sound.getSampledWindow(self.sample_time, self.sample_number, samples) if not length: return 0 noise = self.sound.findThreshold(self.sound_ctx, 30, samples, length) average = noise / 100 return average 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(mic_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.mic = Microphone(self.pin_mappings.mic_pin) self.screen = Jhd1313m1(self.pin_mappings.i2c_bus, 0x3E, 0x62) self.mic_ctx = micThresholdContext() self.mic_ctx.averageReading = 0 self.mic_ctx.runningAverage = 0 self.mic_ctx.averagedOver = 2 def update_hardware_state(self): """ Update hardware state. """ self.trigger_hardware_event(SOUND_MEASUREMENT, self.measure_volume()) # hardware functions def measure_volume(self): """ Measure the average volume """ samples = uint16Array(128) length = self.mic.getSampledWindow(2, 128, samples) if not length: return 0 noise = self.mic.findThreshold(self.mic_ctx, 30, samples, length) average = noise / 100 return average 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"])()