Skip to content

Commit 2e3718b

Browse files
author
Kevin J Walters
committed
Implementing reset().
Adding optional feature to views to include light sensor and passing an APDS9960 object when display is created. Reducing audio samples from 35 to 21 as this sounds better on the CLUE speaker.
1 parent b6c86d9 commit 2e3718b

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

microbit.py

+35-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
### microbit.py v0.34
1+
### microbit.py v0.35
22
### A partial emulation of MicroPython micro:bit microbit library
33

44
### Tested with an Adafruit CLUE and CircuitPython and 5.3.1
@@ -34,6 +34,7 @@
3434

3535
### Need to avoid this style of importing as this may be used with "import *"
3636
##from displayio import Bitmap, Group, Palette, TileGrid
37+
import supervisor
3738
import displayio
3839
import terminalio
3940

@@ -51,20 +52,27 @@
5152
try:
5253
import adafruit_display_text.label
5354
except ImportError:
54-
print("No library: adafruit_display_text")
55+
print("No display text library: adafruit_display_text")
5556

5657
### For accelerometer
5758
try:
5859
import adafruit_lsm6ds.lsm6ds33
5960
except ImportError:
6061
print("No accelerometer library: adafruit_lsm6ds")
6162

62-
### For compass
63+
### For magnetometer / compass
6364
try:
6465
import adafruit_lis3mdl
6566
except ImportError:
6667
print("No magnetometer library: adafruit_lis3mdl")
6768

69+
### For light level
70+
try:
71+
import adafruit_apds9960.apds9960
72+
except ImportError:
73+
print("No light sensor library: adafruit_apds9960")
74+
75+
6876
### For MicroBitDisplayViewEnhanced
6977
try:
7078
import display_pin
@@ -103,7 +111,7 @@ class ClueSpeaker:
103111

104112
def __init__(self):
105113
self._audio = None
106-
self._sample_len = 35
114+
self._sample_len = 21
107115
sine_wave = array.array("H", _makeSample(self._sample_len))
108116
self._wave_sample = audiocore.RawSample(sine_wave)
109117

@@ -150,7 +158,7 @@ def panic(error_code):
150158

151159

152160
def reset():
153-
raise NotImplementedError
161+
supervisor.reload()
154162

155163

156164
### Some class to manage the calls to update()
@@ -341,6 +349,7 @@ def __init__(self, display=None, ### pylint: disable=redefined-outer-name
341349
led_cols=5,
342350
font=MicroBitFonts.STANDARD,
343351
font_widths=MicroBitFonts.STANDARD_WIDTHS,
352+
light_sensor=None,
344353
exception=False,
345354
display_show=True):
346355
"""disp active display
@@ -358,8 +367,9 @@ def __init__(self, display=None, ### pylint: disable=redefined-outer-name
358367
self._initView(display, mode,
359368
led_rows=led_rows, led_cols=led_cols)
360369

361-
###
362-
self.ligtsensorthingy = None ### TODO
370+
self._light_sensor = light_sensor
371+
if light_sensor:
372+
light_sensor.enable_color = True
363373
self._showing = None
364374
self._scrolling = None
365375
self._led_rows = led_rows
@@ -637,7 +647,11 @@ def _nopOrE(self):
637647
### Rather clever implementation on micro:bit although there is a visible flicker
638648
def read_light_level(self):
639649
""" TODO - this is 0-255, reads 30 on a micro:bit at my desk"""
640-
raise NotImplementedError
650+
if self._light_sensor:
651+
### r,g,b,clear comes back from the APDS9960
652+
return self._light_sensor.color_data[3] // 256
653+
else:
654+
raise RuntimeError("No light sensor configured - missing library?")
641655

642656

643657
@property
@@ -1010,6 +1024,7 @@ def updatePin(self, pin_name, pin_type, value):
10101024
##print("updatePin", pin_name, pin_type, value)
10111025

10121026
### Disable auto_refresh to reduce flicker and increase efficiency
1027+
### this reduces a pwm sweep of range(0, 1024, 4) from 40s to 19s
10131028
restore_refresh = None
10141029
if self._display:
10151030
restore_refresh = self._display.auto_refresh
@@ -1596,7 +1611,7 @@ def _analog(self, direction):
15961611
frequency=self._frequency,
15971612
duty_cycle=0,
15981613
variable_frequency=True)
1599-
### microbit behaviour is unused for write_analog(0)
1614+
### microbit mode is unused for write_analog(0)
16001615
### https://forum.micropython.org/viewtopic.php?t=8933&p=50377
16011616
self._mode = "write_analog" if direction == "out" else "music"
16021617
self._deinit = self._deinitAnalog
@@ -1785,7 +1800,7 @@ def _accelToPitchRoll(x, y, z):
17851800

17861801

17871802
### TODO - micro:bit calibrates if not calibrated when methods
1788-
### are called that return data -
1803+
### are called that return data
17891804
### ponder storage using nvm module for eeprom-like storage
17901805
### could use magic identifer number a la microbit and NaN for NA numbers
17911806
class MicroBitCompass:
@@ -2007,8 +2022,17 @@ def removeHookPins(cls, method_name, cb, cb_args):
20072022
button_a = MicroBitButton(pin5)
20082023
button_b = MicroBitButton(pin11)
20092024

2025+
2026+
try:
2027+
apds9660_sensor = adafruit_apds9960.apds9960.APDS9960(board.I2C())
2028+
except NameError:
2029+
apds9660_sensor = None
2030+
20102031
### This needs to be created after pins and PinManager setup and buttons
2011-
display = MicroBitDisplay(board.DISPLAY, "enhanced")
2032+
display = MicroBitDisplay(board.DISPLAY,
2033+
"enhanced",
2034+
light_sensor=apds9660_sensor)
2035+
del apds9660_sensor
20122036

20132037
### These have some lazy initialisation to stop the instantiation
20142038
### blowing up if the relevant CircuitPython libraries aren't present in /lib

0 commit comments

Comments
 (0)