|
| 1 | +# A easy to use module for the basic components of the tildagon badge |
| 2 | + |
| 3 | +from tildagonos import tildagonos |
| 4 | +import imu as tilda_imu |
| 5 | +import math |
| 6 | +import time |
| 7 | + |
| 8 | + |
| 9 | +class led(): |
| 10 | + |
| 11 | + @staticmethod |
| 12 | + def _setup_leds(): |
| 13 | + tildagonos.set_led_power(True) |
| 14 | + |
| 15 | + @staticmethod |
| 16 | + def set(led_number, state): |
| 17 | + if not isinstance(led_number, int) or led_number < 1 or led_number > 12: |
| 18 | + raise ValueError("led_number must be an integer between 1 and 12") |
| 19 | + |
| 20 | + # TODO : Ideally shouldn't need to run _setup_leds each use of set_led |
| 21 | + led._setup_leds() |
| 22 | + |
| 23 | + tildagonos.leds[led_number] = state |
| 24 | + tildagonos.leds.write() |
| 25 | + |
| 26 | + |
| 27 | +class button(): |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + def get(button_letter): |
| 31 | + button_letter = button_letter.lower() |
| 32 | + button_letters = { |
| 33 | + "a": (0x5A, 0, (1 << 6)), |
| 34 | + "b": (0x5A, 0, (1 << 7)), |
| 35 | + "c": (0x59, 0, (1 << 0)), |
| 36 | + "d": (0x59, 0, (1 << 1)), |
| 37 | + "e": (0x59, 0, (1 << 2)), |
| 38 | + "f": (0x59, 0, (1 << 3)), |
| 39 | + } |
| 40 | + if button_letter in button_letters.keys(): |
| 41 | + # Note the button must be flipped, as will return True when not pressed |
| 42 | + return not tildagonos.check_egpio_state(button_letters[button_letter]) |
| 43 | + else: |
| 44 | + raise ValueError("button_letter must be a string of a single letter from a to f") |
| 45 | + |
| 46 | + |
| 47 | +class imu(): |
| 48 | + class ImuData(): |
| 49 | + def __init__(self, x, y, z): |
| 50 | + self.x = x |
| 51 | + self.y = y |
| 52 | + self.z = z |
| 53 | + |
| 54 | + def __getitem__(self, index): |
| 55 | + if index == 0: |
| 56 | + return self.x |
| 57 | + elif index == 1: |
| 58 | + return self.y |
| 59 | + elif index == 2: |
| 60 | + return self.z |
| 61 | + else: |
| 62 | + raise IndexError("Index out of range. Valid indices are 0, 1, and 2.") |
| 63 | + |
| 64 | + def __str__(self): |
| 65 | + return f"x: {self.x}, y: {self.y}, z: {self.z}" |
| 66 | + |
| 67 | + @staticmethod |
| 68 | + def _magnitude(acc_read): |
| 69 | + return math.sqrt(sum(i ** 2 for i in acc_read)) |
| 70 | + |
| 71 | + @staticmethod |
| 72 | + def is_tilted_forward(): |
| 73 | + acc_read = tilda_imu.acc_read() |
| 74 | + if acc_read[0] < -4: |
| 75 | + return True |
| 76 | + return False |
| 77 | + |
| 78 | + @staticmethod |
| 79 | + def is_tilted_back(): |
| 80 | + acc_read = tilda_imu.acc_read() |
| 81 | + if acc_read[0] > 4: |
| 82 | + return True |
| 83 | + return False |
| 84 | + |
| 85 | + @staticmethod |
| 86 | + def is_tilted_left(): |
| 87 | + acc_read = tilda_imu.acc_read() |
| 88 | + if acc_read[1] < -4: |
| 89 | + return True |
| 90 | + return False |
| 91 | + |
| 92 | + @staticmethod |
| 93 | + def is_tilted_right(): |
| 94 | + acc_read = tilda_imu.acc_read() |
| 95 | + if acc_read[1] > 4: |
| 96 | + return True |
| 97 | + return False |
| 98 | + |
| 99 | + @staticmethod |
| 100 | + def is_shaken(): |
| 101 | + acc_read1 = tilda_imu.acc_read() |
| 102 | + magnitude1 = imu._magnitude(acc_read1) |
| 103 | + |
| 104 | + # Wait for a short period of time before taking another reading |
| 105 | + time.sleep(0.1) |
| 106 | + |
| 107 | + acc_read2 = tilda_imu.acc_read() |
| 108 | + magnitude2 = imu._magnitude(acc_read2) |
| 109 | + |
| 110 | + # If the change in magnitude is above a certain threshold (4 for now), the IMU is being shaken |
| 111 | + if abs(magnitude1 - magnitude2) > 4: |
| 112 | + return True |
| 113 | + return False |
| 114 | + |
| 115 | + @staticmethod |
| 116 | + def get_acceleration(): |
| 117 | + raw_data = tilda_imu.acc_read() |
| 118 | + acc_object = imu.ImuData(raw_data[0], raw_data[1], raw_data[2]) |
| 119 | + return acc_object |
0 commit comments