Toggle Navigation
Hatchery
Eggs
rocket_status
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
import leds import light_sensor import utime import personal_state as ps import display import power import buttons import bme680 import bhi160 STATE_STATE = { 0: ps.CHAOS, 1: ps.COMMUNICATION, 2: ps.CAMP, 7: ps.NO_CONTACT, } STATE_TEXT = { 0: "chaos", 1: "commnctn", 2: "camp", 7: "nein danke", } b = bhi160.BHI160Orientation() class MyPersonalState: def __init__(self, initial_state=0): self.set(initial_state) def next(self): self.state += 1 if self.state >= 3: self.state = 0 self.set(self.state) def set(self, state): for rocket in range(0, 3): leds.set_rocket(rocket, 0) self.state = state ps.set(STATE_STATE[self.state], True) def tick(self): if self.state == 7: return base = max(2, light_sensor.get_reading() - 13) brightness = min(31, base * 3) leds.set_rocket(self.state, brightness) def text(self): return STATE_TEXT[self.state] class MyEnvironment: def __init__(self, initial_state=0): bme680.init() self.state = 0 self.tick() def next(self): self.state += 1 if self.state >= 4: self.state = 0 def tick(self): self.temperature, self.humidity, self.pressure, self.resistance = bme680.get_data() def text(self): if self.state == 0: return "T:" + str(self.temperature) if self.state == 1: return "H:" + str(self.humidity) if self.state == 2: return "P:" + str(self.pressure) if self.state == 3: return "R:" + str(self.resistance) personal_state = MyPersonalState(0) environment = MyEnvironment(0) nickname = "hacker" try: with open("nickname.txt", "r") as nf: nickname = nf.read() except: pass BUTTON_TIMER_POPPED = 892 def button_events(timeout=0): """Iterate over button presses (event-loop).""" yield 0 button_pressed = False count = 0 while True: v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT) if timeout > 0 and count > 0 and count % timeout == 0: yield BUTTON_TIMER_POPPED if timeout > 0: count += 1 if v == 0: button_pressed = False if not button_pressed and v & buttons.BOTTOM_LEFT != 0: button_pressed = True yield buttons.BOTTOM_LEFT if not button_pressed and v & buttons.BOTTOM_RIGHT != 0: button_pressed = True yield buttons.BOTTOM_RIGHT if not button_pressed and v & buttons.TOP_RIGHT != 0: button_pressed = True yield buttons.TOP_RIGHT utime.sleep_ms(10) with display.open() as disp: for event in button_events(5): if event == buttons.BOTTOM_LEFT: personal_state.next() if event == buttons.BOTTOM_RIGHT: personal_state.set(7) if event == buttons.TOP_RIGHT: environment.next() personal_state.tick() _ = disp.clear() _ = disp.print(nickname, posy=0) _ = disp.print(personal_state.text(), posy=20) _ = disp.print("V:" + str(power.read_battery_voltage()), posy=40) _ = disp.print(environment.text(), posy=60) orn = b.read() if len(orn) > 0: x = orn[0].x base = [[0,0,0]] * 11 base[int((x % 180.0)/180.0*11.0)] = [255,255,255] leds.set_all(base) disp.update() environment.tick()