Toggle Navigation
Hatchery
Eggs
Sensors
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
""" Sensors data viewer Press left/right button to switch view Data is refreshed at 10Hz """ import utime import display import buttons import light_sensor import color import bme680 import os def limit_color_val(val): if val > 255: val = 255 if val < 0: val = 0 return val def mod_brightness(rgb, factor): r = int(rgb[0]*factor) r = limit_color_val(r) g = int(rgb[1]*factor) g = limit_color_val(g) b = int(rgb[2]*factor) b = limit_color_val(b) return [r,g,b] def display_data(title, data): with display.open() as disp: disp.rect(0, 0, 160, 80, col=color.BLACK, filled=True) disp.print(title, fg=color.WHITE, bg=color.BLACK, posx=80 - round(len(title) / 2 * 14), posy=24) disp.print(data, fg=mod_brightness(color.WHITE, 0.7), bg=color.BLACK, posx=80 - round(len(data) / 2 * 14), posy=45) disp.update() disp.close() def main(): sensors = [ 'RTC', 'LIGHT', 'HUMID.', 'PRESSURE', 'TEMP.', 'AIR QUAL.', 'BATTERY' ] view = 0 bme680.init() light_sensor.start() button_released = 1 while True: # Check if user want to change view pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT) if pressed == 0: button_released = 1 if button_released and (pressed & buttons.BOTTOM_LEFT != 0): view = (view - 1) % len(sensors) button_released = 0 elif button_released and (pressed & buttons.BOTTOM_RIGHT != 0): view = (view + 1) % len(sensors) button_released = 0 if sensors[view] == 'RTC': year, month, mday, hour, min, sec, wday, yday = utime.localtime(utime.time()) display_data(sensors[view], '{:02d}:{:02d}:{:02d}'.format(hour, min, sec)) elif sensors[view] == 'LIGHT': light = light_sensor.get_reading() display_data(sensors[view], '{} unit'.format(light)) elif sensors[view] == 'HUMID.': temperature, humidity, pressure, resistance = bme680.get_data() display_data(sensors[view], '{:.2f} %'.format(humidity)) elif sensors[view] == 'PRESSURE': temperature, humidity, pressure, resistance = bme680.get_data() display_data(sensors[view], '{:.2f} hPa'.format(pressure)) elif sensors[view] == 'TEMP.': temperature, humidity, pressure, resistance = bme680.get_data() display_data(sensors[view], '{:.2f} degC'.format(temperature)) elif sensors[view] == 'AIR QUAL.': temperature, humidity, pressure, resistance = bme680.get_data() display_data(sensors[view], '{:.0f} unit'.format(resistance)) elif sensors[view] == 'BATTERY': batt_lvl = os.read_battery() display_data(sensors[view], '{:.2f} V'.format(batt_lvl)) else: display_data('ERROR', 'Unknw {}'.format(view)) utime.sleep(0.1) bme680.deinit() if __name__ == "__main__": main()