diff --git a/preload/apps/joust/__init__.py b/preload/apps/joust/__init__.py index dbda3be8e5d7a9485999d502b431f5cfecde5a4c..00b8e402a714fca767e56f1022f88e6ad78fc44f 100644 --- a/preload/apps/joust/__init__.py +++ b/preload/apps/joust/__init__.py @@ -10,19 +10,23 @@ import leds import vibra import bhi160 -COLOR_GREEN=(0,255,0) -COLOR_RED=(255,0,0) -COLOR_ORANGE=(255,125,0) -COLOR_WHITE=(255,255,255) +COLOR_GREEN = (0, 255, 0) +COLOR_RED = (255, 0, 0) +COLOR_ORANGE = (255, 125, 0) +COLOR_WHITE = (255, 255, 255) ACCELEROMETER = bhi160.BHI160Accelerometer() + + class JoustSensor: NUM_READINGS = 1 + def __init__(self): self.sensor = ACCELEROMETER self.last_accels = [] self.jerk = 0.0 self.threshold = 1.8 + def get_jerk(self): readings = list() while not readings: @@ -33,11 +37,13 @@ class JoustSensor: jerk_x = sum([a.x for a in self.last_accels]) / JoustSensor.NUM_READINGS jerk_y = sum([a.y for a in self.last_accels]) / JoustSensor.NUM_READINGS jerk_z = sum([a.z for a in self.last_accels]) / JoustSensor.NUM_READINGS - self.jerk = (jerk_x**2 + jerk_y**2 + jerk_z**2)**0.5 + self.jerk = (jerk_x ** 2 + jerk_y ** 2 + jerk_z ** 2) ** 0.5 return self.jerk + def too_fast(self): return self.get_jerk() > self.threshold + def wait_button(): utime.sleep_ms(1000) while True: @@ -56,54 +62,65 @@ def wait_button(): return "UL" return "??" + def joining_game(): set_display("JOIN ?", COLOR_WHITE) set_color(COLOR_WHITE) + def status_in_game(): set_display("IN GAME", COLOR_GREEN) set_color(COLOR_GREEN) + def status_out_game(): set_display("DROPPED", COLOR_RED) set_color(COLOR_RED) + def set_display(text, colr): with display.open() as disp: disp.clear() - disp.print(text, fg=colr, bg=(0,0,0), posx=30, posy=30) + disp.print(text, fg = colr, bg = (0, 0, 0), posx = 30, posy = 30) disp.update() + def set_color(colr): for i in range(15): leds.prep(i, colr) leds.update() + def countdown(n): colr = COLOR_WHITE - for i in range(n,0,-1): - if i == 3: colr = COLOR_RED - elif i == 2: colr = COLOR_ORANGE - elif i == 1: colr = COLOR_GREEN + for i in range(n, 0, -1): + if i == 3: + colr = COLOR_RED + elif i == 2: + colr = COLOR_ORANGE + elif i == 1: + colr = COLOR_GREEN set_display("START: %d" % i, colr) leds.clear() vibra.vibrate(300) utime.sleep_ms(1000) vibra.vibrate(1000) + def change_status(status): status() vibra.vibrate(1000) + def run(): print("Starting ...") acc = JoustSensor() - i=0 + i = 0 states = [joining_game, status_in_game, status_out_game] while True: - current_status = states[i%len(states)] + current_status = states[i % len(states)] change_status(current_status) - i+=1 + i += 1 if current_status == joining_game: wait_button() countdown(5) @@ -113,4 +130,5 @@ def run(): elif current_status == status_out_game: wait_button() -run() \ No newline at end of file + +run()