|
| 1 | +import time |
| 2 | +import board |
| 3 | +import displayio |
| 4 | +from adafruit_clue import clue |
| 5 | +from simpleio import map_range |
| 6 | +from adafruit_bitmap_font import bitmap_font |
| 7 | +from adafruit_lsm6ds import LSM6DS33, Rate, AccelRange |
| 8 | +from adafruit_progressbar import ProgressBar |
| 9 | +from adafruit_display_text.label import Label |
| 10 | + |
| 11 | +# turns off onboard NeoPixel to conserve battery |
| 12 | +clue.pixel.brightness = (0.0) |
| 13 | + |
| 14 | +# accessing the Clue's accelerometer |
| 15 | +sensor = LSM6DS33(board.I2C()) |
| 16 | + |
| 17 | +# step goal |
| 18 | +step_goal = 10000 |
| 19 | + |
| 20 | +# onboard button states |
| 21 | +a_state = False |
| 22 | +b_state = False |
| 23 | + |
| 24 | +# array to adjust screen brightness |
| 25 | +bright_level = [0, 0.5, 1] |
| 26 | + |
| 27 | +countdown = 0 # variable for the step goal progress bar |
| 28 | +clock = 0 # variable used to keep track of time for the steps per hour counter |
| 29 | +clock_count = 0 # holds the number of hours that the step counter has been running |
| 30 | +clock_check = 0 # holds the result of the clock divided by 3600 seconds (1 hour) |
| 31 | +last_step = 0 # state used to properly counter steps |
| 32 | +mono = time.monotonic() # time.monotonic() device |
| 33 | +mode = 1 # state used to track screen brightness |
| 34 | +steps_log = 0 # holds total steps to check for steps per hour |
| 35 | +steps_remaining = 0 # holds the remaining steps needed to reach the step goal |
| 36 | +sph = 0 # holds steps per hour |
| 37 | + |
| 38 | +# variables to hold file locations for background and fonts |
| 39 | +clue_bgBMP = "/clue_bgBMP.bmp" |
| 40 | +small_font = "/fonts/Roboto-Medium-16.bdf" |
| 41 | +med_font = "/fonts/Roboto-Bold-24.bdf" |
| 42 | +big_font = "/fonts/Roboto-Black-48.bdf" |
| 43 | + |
| 44 | +# glyphs for fonts |
| 45 | +glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.: ' |
| 46 | + |
| 47 | +# loading bitmap fonts |
| 48 | +small_font = bitmap_font.load_font(small_font) |
| 49 | +small_font.load_glyphs(glyphs) |
| 50 | +med_font = bitmap_font.load_font(med_font) |
| 51 | +med_font.load_glyphs(glyphs) |
| 52 | +big_font = bitmap_font.load_font(big_font) |
| 53 | +big_font.load_glyphs(glyphs) |
| 54 | + |
| 55 | +# creating display and default brightness |
| 56 | +clue_display = board.DISPLAY |
| 57 | +clue_display.brightness = 0.5 |
| 58 | + |
| 59 | +# graphics group |
| 60 | +clueGroup = displayio.Group(max_size=20) |
| 61 | + |
| 62 | +# loading bitmap background |
| 63 | +clue_bg = displayio.OnDiskBitmap(open(clue_bgBMP, "rb")) |
| 64 | +clue_tilegrid = displayio.TileGrid(clue_bg, pixel_shader=displayio.ColorConverter()) |
| 65 | +clueGroup.append(clue_tilegrid) |
| 66 | + |
| 67 | +# creating the ProgressBar object |
| 68 | +bar_group = displayio.Group(max_size=20) |
| 69 | +prog_bar = ProgressBar(1, 1, 239, 25, bar_color=0x652f8f) |
| 70 | +bar_group.append(prog_bar) |
| 71 | + |
| 72 | +clueGroup.append(bar_group) |
| 73 | + |
| 74 | +# text for step goal |
| 75 | +steps_countdown = Label(small_font, text='%d Steps Remaining' % step_goal, color=clue.WHITE) |
| 76 | +steps_countdown.x = 55 |
| 77 | +steps_countdown.y = 12 |
| 78 | + |
| 79 | +# text for steps |
| 80 | +text_steps = Label(big_font, text="0 ", color=0xe90e8b) |
| 81 | +text_steps.x = 45 |
| 82 | +text_steps.y = 70 |
| 83 | + |
| 84 | +# text for steps per hour |
| 85 | +text_sph = Label(med_font, text=" -- ", color=0x29abe2) |
| 86 | +text_sph.x = 8 |
| 87 | +text_sph.y = 195 |
| 88 | + |
| 89 | +# adding all text to the display group |
| 90 | +clueGroup.append(text_sph) |
| 91 | +clueGroup.append(steps_countdown) |
| 92 | +clueGroup.append(text_steps) |
| 93 | + |
| 94 | +# sending display group to the display at startup |
| 95 | +clue_display.show(clueGroup) |
| 96 | + |
| 97 | +# setting up the accelerometer and pedometer |
| 98 | +sensor.accelerometer_range = AccelRange.RANGE_2G |
| 99 | +sensor.accelerometer_data_rate = Rate.RATE_26_HZ |
| 100 | +sensor.gyro_data_rate = Rate.RATE_SHUTDOWN |
| 101 | +sensor.pedometer_enable = True |
| 102 | + |
| 103 | +while True: |
| 104 | + |
| 105 | + # button debouncing |
| 106 | + if not clue.button_a and not a_state: |
| 107 | + a_state = True |
| 108 | + if not clue.button_b and not b_state: |
| 109 | + b_state = True |
| 110 | + |
| 111 | + # setting up steps to hold step count |
| 112 | + steps = sensor.pedometer_steps |
| 113 | + |
| 114 | + # creating the data for the ProgressBar |
| 115 | + countdown = map_range(steps, 0, step_goal, 0.0, 1.0) |
| 116 | + |
| 117 | + # actual counting of the steps |
| 118 | + # if a step is taken: |
| 119 | + if abs(steps-last_step) > 1: |
| 120 | + step_time = time.monotonic() |
| 121 | + # updates last_step |
| 122 | + last_step = steps |
| 123 | + # updates the display |
| 124 | + text_steps.text = '%d' % steps |
| 125 | + clock = step_time - mono |
| 126 | + |
| 127 | + # logging steps per hour |
| 128 | + if clock > 3600: |
| 129 | + # gets number of hours to add to total |
| 130 | + clock_check = clock / 3600 |
| 131 | + # logs the step count as of that hour |
| 132 | + steps_log = steps |
| 133 | + # adds the hours to get a new hours total |
| 134 | + clock_count += round(clock_check) |
| 135 | + # divides steps by hours to get steps per hour |
| 136 | + sph = steps_log / clock_count |
| 137 | + # adds the sph to the display |
| 138 | + text_sph.text = '%d' % sph |
| 139 | + # resets clock to count to the next hour again |
| 140 | + clock = 0 |
| 141 | + mono = time.monotonic() |
| 142 | + |
| 143 | + # adjusting countdown to step goal |
| 144 | + prog_bar.progress = float(countdown) |
| 145 | + |
| 146 | + # displaying countdown to step goal |
| 147 | + if step_goal - steps > 0: |
| 148 | + steps_remaining = step_goal - steps |
| 149 | + steps_countdown.text = '%d Steps Remaining' % steps_remaining |
| 150 | + else: |
| 151 | + steps_countdown.text = 'Steps Goal Met!' |
| 152 | + |
| 153 | + # adjusting screen brightness, a button decreases brightness |
| 154 | + if clue.button_a and a_state: |
| 155 | + mode -= 1 |
| 156 | + a_state = False |
| 157 | + if mode < 0: |
| 158 | + mode = 0 |
| 159 | + clue_display.brightness = bright_level[mode] |
| 160 | + else: |
| 161 | + clue_display.brightness = bright_level[mode] |
| 162 | + # b button increases brightness |
| 163 | + if clue.button_b and b_state: |
| 164 | + mode += 1 |
| 165 | + b_state = False |
| 166 | + if mode > 2: |
| 167 | + mode = 2 |
| 168 | + clue_display.brightness = bright_level[mode] |
| 169 | + else: |
| 170 | + clue_display.brightness = bright_level[mode] |
| 171 | + |
| 172 | + time.sleep(0.001) |
0 commit comments