|
| 1 | +""" |
| 2 | +Read the barometric reading in the air |
| 3 | +Visualize air reading changes over time as a color animation on a NeoPixel strip |
| 4 | +Display a "sinking" or "rising" graphic on the screen along with recent reading data |
| 5 | +
|
| 6 | +Code by Erin St Blaine for Adafruit Industries |
| 7 | +""" |
| 8 | + |
| 9 | +import board |
| 10 | +import neopixel |
| 11 | +from adafruit_clue import clue |
| 12 | +import adafruit_fancyled.adafruit_fancyled as fancy |
| 13 | +import displayio |
| 14 | +from adafruit_display_text import label |
| 15 | +from adafruit_bitmap_font import bitmap_font |
| 16 | + |
| 17 | +num_leds = 79 #number of LEDs in your strip |
| 18 | +timeToCheck = 100 # set the amount of time between sensor checks. 7800 is approx. 1 hour |
| 19 | + |
| 20 | +# Barometer or Thermometer? Uncomment the section you want to use |
| 21 | + |
| 22 | +# BAROMETER RANGES (hPa) |
| 23 | +# set desired reading range -- the NeoPixel palette choice will be determined by these thresholds |
| 24 | +deviceType = 0 |
| 25 | +min_reading = 960 |
| 26 | +med_reading = 965 |
| 27 | +high_reading= 970 |
| 28 | +max_reading = 975 |
| 29 | + |
| 30 | +""" |
| 31 | +# THERMOMETER RANGES (C) |
| 32 | +# set desired temperature range - NeoPixel palette choice determined by these thresholds |
| 33 | +deviceType = 1 |
| 34 | +min_reading = 25 |
| 35 | +med_reading = 27 |
| 36 | +high_reading= 31 |
| 37 | +max_reading = 33 |
| 38 | +""" |
| 39 | + |
| 40 | +# get an initial sensor reading |
| 41 | +if deviceType ==0: |
| 42 | + reading = clue.pressure |
| 43 | +else: |
| 44 | + reading = clue.temperature |
| 45 | + |
| 46 | +#set up variables for "remembering" past readings |
| 47 | +reading1 = reading |
| 48 | +reading2 = reading1 |
| 49 | +reading3 = reading2 |
| 50 | +counter = 0 |
| 51 | +toggle = 1 # for on/off switch on button A |
| 52 | +displayOn = 1 # to turn the display on and off with button B |
| 53 | + |
| 54 | +clue.display.brightness = 0.8 |
| 55 | +clue_display = displayio.Group(max_size=4) |
| 56 | + |
| 57 | +# draw the rising image |
| 58 | +rising_file = open("rising.bmp", "rb") |
| 59 | +rising_bmp = displayio.OnDiskBitmap(rising_file) |
| 60 | +rising_sprite = displayio.TileGrid(rising_bmp, pixel_shader=displayio.ColorConverter()) |
| 61 | +clue_display.append(rising_sprite) |
| 62 | + |
| 63 | +# draw the sinking image |
| 64 | +sinking_file = open("sinking.bmp", "rb") |
| 65 | +sinking_bmp = displayio.OnDiskBitmap(sinking_file) |
| 66 | +sinking_sprite = displayio.TileGrid(sinking_bmp, pixel_shader=displayio.ColorConverter()) |
| 67 | +clue_display.append(sinking_sprite) |
| 68 | + |
| 69 | +# Create text |
| 70 | +# first create the group |
| 71 | +text_group = displayio.Group(max_size=5, scale=1) |
| 72 | +# Make a label |
| 73 | +reading_font = bitmap_font.load_font("/font/RacingSansOne-Regular-29.bdf") |
| 74 | +reading_font.load_glyphs("0123456789ADSWabcdefghijklmnopqrstuvwxyz:!".encode('utf-8')) |
| 75 | +reading_label = label.Label(reading_font, color=0xffffff, max_glyphs=15) |
| 76 | +reading_label.x = 10 |
| 77 | +reading_label.y = 24 |
| 78 | +text_group.append(reading_label) |
| 79 | + |
| 80 | +reading2_label = label.Label(reading_font, color=0xdaf5f4, max_glyphs=15) |
| 81 | +reading2_label.x = 10 |
| 82 | +reading2_label.y = 54 |
| 83 | +text_group.append(reading2_label) |
| 84 | + |
| 85 | +reading3_label = label.Label(reading_font, color=0x4f3ab1, max_glyphs=15) |
| 86 | +reading3_label.x = 10 |
| 87 | +reading3_label.y = 84 |
| 88 | +text_group.append(reading3_label) |
| 89 | + |
| 90 | +timer_label = label.Label(reading_font, color=0x072170, max_glyphs=15) |
| 91 | +timer_label.x = 10 |
| 92 | +timer_label.y = 114 |
| 93 | +text_group.append(timer_label) |
| 94 | + |
| 95 | +clue_display.append(text_group) |
| 96 | +clue.display.show(clue_display) |
| 97 | + |
| 98 | +# Define color Palettes |
| 99 | +waterPalette = [0x00d9ff, 0x006f82, 0x43bfb9, 0x0066ff] |
| 100 | +icePalette = [0x8080FF, 0x8080FF, 0x8080FF, 0x0000FF, 0xC88AFF] |
| 101 | +sunPalette = [0xffaa00, 0xffdd00, 0x7d5b06, 0xfffca8] |
| 102 | +firePalette = [0xff0000, 0xff5500, 0x8a3104, 0xffaa00 ] |
| 103 | +forestPalette = [0xccffa8, 0x69f505, 0x05f551, 0x2c8247] |
| 104 | + |
| 105 | +# set up default initial palettes, just for startup |
| 106 | +palette = forestPalette |
| 107 | +palette2 = waterPalette |
| 108 | +palette3 = icePalette |
| 109 | + |
| 110 | +# Declare a NeoPixel object on pin A4 with num_leds pixels, no auto-write. |
| 111 | +# Set brightness to max because we'll be using FancyLED's brightness control. |
| 112 | +pixels = neopixel.NeoPixel(board.A4, num_leds, brightness=1.0, |
| 113 | + auto_write=False) |
| 114 | + |
| 115 | +offset = 0 # Positional offset into color palette to get it to 'spin' |
| 116 | + |
| 117 | +while True: |
| 118 | + # use button A to toggle the NeoPixels on or off by changing brightness |
| 119 | + if clue.button_a: |
| 120 | + if toggle == 1: |
| 121 | + toggle = 0 |
| 122 | + pixels.brightness = 0 |
| 123 | + clue.display.brightness = 0 |
| 124 | + elif toggle == 0: |
| 125 | + toggle = 1 |
| 126 | + pixels.brightness = 1.0 |
| 127 | + clue.display.brightness = 0.8 |
| 128 | + if clue.button_b: |
| 129 | + # Toggle only the display on and off |
| 130 | + if displayOn == 0: |
| 131 | + clue.display.brightness = 0.8 |
| 132 | + displayOn = 1 |
| 133 | + else: |
| 134 | + clue.display.brightness = 0 |
| 135 | + displayOn = 0 |
| 136 | + |
| 137 | + # assign color palette to NeoPixel section 1 based on the current reading reading |
| 138 | + if reading1 < min_reading: |
| 139 | + palette = firePalette |
| 140 | + elif reading1 > min_reading and reading1 < med_reading: |
| 141 | + palette = sunPalette |
| 142 | + elif reading1 > med_reading and reading1 < high_reading: |
| 143 | + palette = forestPalette |
| 144 | + elif reading1 > high_reading and reading1 < max_reading: |
| 145 | + palette = waterPalette |
| 146 | + else: |
| 147 | + palette = icePalette |
| 148 | + # Map colors to pixels. Adjust range numbers to light up specific pixels. This configuration |
| 149 | + # maps to a reflected gradient, with pixel 0 in the upper left corner |
| 150 | + # Load each pixel's color from the palette using an offset, run it |
| 151 | + # through the gamma function, pack RGB value and assign to pixel. |
| 152 | + for i in range(23, 31): #center right -- present moment |
| 153 | + color = fancy.palette_lookup(palette, offset + i / num_leds) |
| 154 | + color = fancy.gamma_adjust(color, brightness=0.25) |
| 155 | + pixels[i] = color.pack() |
| 156 | + |
| 157 | + for i in range(63, 71): #center left -- present moment |
| 158 | + color = fancy.palette_lookup(palette, offset + i / num_leds) |
| 159 | + color = fancy.gamma_adjust(color, brightness=0.25) |
| 160 | + pixels[i] = color.pack() |
| 161 | + |
| 162 | + for i in range(16, 23): #top mid right -- 1 cycle ago |
| 163 | + color = fancy.palette_lookup(palette2, offset + i / num_leds) |
| 164 | + color = fancy.gamma_adjust(color, brightness=0.25) |
| 165 | + pixels[i] = color.pack() |
| 166 | + |
| 167 | + for i in range(71, 78): #top mid left -- 1 cycle ago |
| 168 | + color = fancy.palette_lookup(palette2, offset + i / num_leds) |
| 169 | + color = fancy.gamma_adjust(color, brightness=0.25) |
| 170 | + pixels[i] = color.pack() |
| 171 | + |
| 172 | + for i in range(31, 38): #bottom mid right -- 1 cycle ago |
| 173 | + color = fancy.palette_lookup(palette2, offset + i / num_leds) |
| 174 | + color = fancy.gamma_adjust(color, brightness=0.25) |
| 175 | + pixels[i] = color.pack() |
| 176 | + |
| 177 | + for i in range(56, 63): #bottom mid left -- 1 cycle ago |
| 178 | + color = fancy.palette_lookup(palette2, offset + i / num_leds) |
| 179 | + color = fancy.gamma_adjust(color, brightness=0.25) |
| 180 | + pixels[i] = color.pack() |
| 181 | + |
| 182 | + for i in range(0, 16): #top right -- 2 cycles ago |
| 183 | + color = fancy.palette_lookup(palette3, offset + i / num_leds) |
| 184 | + color = fancy.gamma_adjust(color, brightness=0.25) |
| 185 | + pixels[i] = color.pack() |
| 186 | + |
| 187 | + for i in range(77, 79): #top left -- 2 cycles ago |
| 188 | + color = fancy.palette_lookup(palette3, offset + i / num_leds) |
| 189 | + color = fancy.gamma_adjust(color, brightness=0.25) |
| 190 | + pixels[i] = color.pack() |
| 191 | + |
| 192 | + for i in range(38, 56): #bottom -- 2 cycles ago |
| 193 | + color = fancy.palette_lookup(palette3, offset + i / num_leds) |
| 194 | + color = fancy.gamma_adjust(color, brightness=0.25) |
| 195 | + pixels[i] = color.pack() |
| 196 | + |
| 197 | + pixels.show() |
| 198 | + offset += 0.01 # Bigger number = faster spin |
| 199 | + |
| 200 | + reading_label.text = "Now {:.1f}".format(reading1) |
| 201 | + reading2_label.text = "Last {:.1f}".format(reading2) |
| 202 | + reading3_label.text = "Prev {:.1f}".format(reading3) |
| 203 | + timer_label.text = "{}".format(counter) |
| 204 | + clue.display.show(clue_display) |
| 205 | + |
| 206 | + # Is it time to update? |
| 207 | + if counter > timeToCheck: |
| 208 | + # This moves the current data to the "1 hour old" section of pixels and the "1 hour old" |
| 209 | + # data to the "2 hours old" section of pixels |
| 210 | + palette3 = palette2 |
| 211 | + palette2 = palette |
| 212 | + reading3 = reading2 |
| 213 | + reading2 = reading1 |
| 214 | + reading1 = reading |
| 215 | + # take a new sensor reading and reset the counter |
| 216 | + if deviceType == 0: |
| 217 | + reading = clue.pressure |
| 218 | + else: |
| 219 | + reading = clue.temperature |
| 220 | + counter = 0 |
| 221 | + # if reading is rising, show rising image and position text at the bottom |
| 222 | + if reading1 > reading2: |
| 223 | + sinking_sprite.x = 300 |
| 224 | + reading_label.y = 134 |
| 225 | + reading2_label.y = 164 |
| 226 | + reading3_label.y = 194 |
| 227 | + timer_label.y = 224 |
| 228 | + # if reading is falling, show sinking image and position text at the top |
| 229 | + elif reading2 < reading3: #reading is falling |
| 230 | + sinking_sprite.x = 0 |
| 231 | + reading_label.y = 24 |
| 232 | + reading2_label.y = 54 |
| 233 | + reading3_label.y = 84 |
| 234 | + timer_label.y = 114 |
| 235 | + # otherwise keep counting up |
| 236 | + else: |
| 237 | + counter = counter + 1 |
0 commit comments