Skip to content

Commit 8876427

Browse files
authored
Merge pull request #1953 from CedarGroveStudios/main
Update Improved Thermal Camera code.py for CircuitPython v7.x compatibility
2 parents 49e0fce + ba2d205 commit 8876427

1 file changed

Lines changed: 29 additions & 29 deletions

File tree

  • PyGamer_Improved_Thermal_Camera

PyGamer_Improved_Thermal_Camera/code.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# SPDX-FileCopyrightText: 2021 Jan Goolsbey for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
# Thermal_Cam_v60_PyBadge_code.py
5-
# 2021-06-07 v6.0
4+
# Thermal_Cam_v70_PyBadge_code.py
5+
# 2021-12-21 v7.0 # CircuitPython v7.x compatible
66

77
import time
88
import board
99
import busio
10+
import gc
1011
import ulab
1112
import displayio
1213
import neopixel
@@ -61,28 +62,18 @@
6162
i2c = busio.I2C(board.SCL, board.SDA, frequency=400000)
6263
amg8833 = adafruit_amg88xx.AMG88XX(i2c)
6364

64-
# CircuitPython 6 & 7 compatible
6565
# Display splash graphics
66-
with open("/thermal_cam_splash.bmp", "rb") as bitmap_file:
67-
bitmap = displayio.OnDiskBitmap(bitmap_file)
68-
splash = displayio.Group(scale=display.width // 160)
69-
splash.append(displayio.TileGrid(bitmap, pixel_shader=getattr(bitmap, 'pixel_shader', displayio.ColorConverter())))
70-
display.show(splash)
71-
time.sleep(0.1) # Give the splash graphic some time to display
72-
73-
# # CircuitPython 7+ compatible
74-
# Display splash graphics
75-
# splash = displayio.Group(scale=display.width // 160)
76-
# bitmap = displayio.OnDiskBitmap("/thermal_cam_splash.bmp")
77-
# splash.append(displayio.TileGrid(bitmap, pixel_shader=bitmap.pixel_shader))
78-
# board.DISPLAY.show(splash)
79-
# time.sleep(0.1) # Allow the splash to display
66+
splash = displayio.Group(scale=display.width // 160)
67+
bitmap = displayio.OnDiskBitmap("/thermal_cam_splash.bmp")
68+
splash.append(displayio.TileGrid(bitmap, pixel_shader=bitmap.pixel_shader))
69+
board.DISPLAY.show(splash)
70+
time.sleep(0.1) # Allow the splash to display
8071

8172
# Set up ulab arrays
8273
n = 8 # Thermal sensor grid axis size; AMG8833 sensor is 8x8
83-
sensor_data = ulab.array(range(n * n)).reshape((n, n)) # Color index narray
84-
grid_data = ulab.zeros(((2 * n) - 1, (2 * n) - 1)) # 15x15 color index narray
85-
histogram = ulab.zeros((2 * n) - 1) # Histogram accumulation narray
74+
sensor_data = ulab.numpy.array(range(n * n)).reshape((n, n)) # Color index narray
75+
grid_data = ulab.numpy.zeros(((2 * n) - 1, (2 * n) - 1)) # 15x15 color index narray
76+
histogram = ulab.numpy.zeros((2 * n) - 1) # Histogram accumulation narray
8677

8778
# Convert default alarm and min/max range values from config file
8879
ALARM_C = fahrenheit_to_celsius(ALARM_F)
@@ -151,13 +142,13 @@ def update_histo_frame(): # Calculate and display histogram
151142
min_histo.text = str(MIN_RANGE_F) # Display histogram legend
152143
max_histo.text = str(MAX_RANGE_F)
153144

154-
histogram = ulab.zeros(GRID_AXIS) # Clear histogram accumulation array
145+
histogram = ulab.numpy.zeros(GRID_AXIS) # Clear histogram accumulation array
155146
for row in range(0, GRID_AXIS): # Collect camera data and calculate histo
156147
for col in range(0, GRID_AXIS):
157148
histo_index = int(map_range(grid_data[col, row], 0, 1, 0, GRID_AXIS - 1))
158149
histogram[histo_index] = histogram[histo_index] + 1
159150

160-
histo_scale = ulab.numerical.max(histogram) / (GRID_AXIS - 1)
151+
histo_scale = ulab.numpy.max(histogram) / (GRID_AXIS - 1)
161152
if histo_scale <= 0:
162153
histo_scale = 1
163154

@@ -373,6 +364,7 @@ def move_buttons(joystick=False): # Read position buttons and joystick
373364

374365
# ###--- PRIMARY PROCESS SETUP ---###
375366
t1 = time.monotonic() # Time marker: Primary Process Setup
367+
fm1 = gc.mem_free() # Monitor free memory
376368
display_image = True # Image display mode; False for histogram
377369
display_hold = False # Active display mode; True to hold display
378370
display_focus = False # Standard display range; True to focus display range
@@ -393,7 +385,7 @@ def move_buttons(joystick=False): # Read position buttons and joystick
393385
flash_status("-HOLD-", 0.25)
394386
else:
395387
sensor = amg8833.pixels # Get sensor_data data
396-
sensor_data = ulab.array(sensor) # Copy to narray
388+
sensor_data = ulab.numpy.array(sensor) # Copy to narray
397389

398390
t3 = time.monotonic() # Time marker: Constrain Sensor Values
399391
for row in range(0, 8):
@@ -402,9 +394,9 @@ def move_buttons(joystick=False): # Read position buttons and joystick
402394

403395
# Update and display alarm setting and max, min, and ave stats
404396
t4 = time.monotonic() # Time marker: Display Statistics
405-
v_max = ulab.numerical.max(sensor_data)
406-
v_min = ulab.numerical.min(sensor_data)
407-
v_ave = ulab.numerical.mean(sensor_data)
397+
v_max = ulab.numpy.max(sensor_data)
398+
v_min = ulab.numpy.min(sensor_data)
399+
v_ave = ulab.numpy.mean(sensor_data)
408400

409401
alarm_value.text = str(ALARM_F)
410402
max_value.text = str(celsius_to_fahrenheit(v_max))
@@ -495,13 +487,21 @@ def move_buttons(joystick=False): # Read position buttons and joystick
495487
MAX_RANGE_C = fahrenheit_to_celsius(MAX_RANGE_F)
496488

497489
t7 = time.monotonic() # Time marker: End of Primary Process
490+
gc.collect()
491+
fm7 = gc.mem_free()
498492
print("*** PyBadge/Gamer Performance Stats ***")
499-
print(f" define displayio: {(t1 - t0):6.3f}")
493+
print(f" define displayio: {(t1 - t0):6.3f} sec")
494+
print(f" startup free memory: {fm1/1000:6.3} Kb")
500495
print("")
501-
print(f" 1) data acquisition: {(t4 - t2):6.3f} rate: {(1 / (t4 - t2)):5.1f}")
496+
print(
497+
f" 1) data acquisition: {(t4 - t2):6.3f} rate: {(1 / (t4 - t2)):5.1f} /sec"
498+
)
502499
print(f" 2) display stats: {(t5 - t4):6.3f}")
503500
print(f" 3) interpolate: {(t6 - t5):6.3f}")
504501
print(f" 4) display image: {(t7 - t6):6.3f}")
505502
print(f" =======")
506-
print(f"total frame: {(t7 - t2):6.3f} rate: {(1 / (t7 - t2)):5.1f}")
503+
print(
504+
f"total frame: {(t7 - t2):6.3f} sec rate: {(1 / (t7 - t2)):5.1f} /sec"
505+
)
506+
print(f" free memory: {fm7/1000:6.3} Kb")
507507
print("")

0 commit comments

Comments
 (0)