1
- ### microbit.py v0.34
1
+ ### microbit.py v0.35
2
2
### A partial emulation of MicroPython micro:bit microbit library
3
3
4
4
### Tested with an Adafruit CLUE and CircuitPython and 5.3.1
34
34
35
35
### Need to avoid this style of importing as this may be used with "import *"
36
36
##from displayio import Bitmap, Group, Palette, TileGrid
37
+ import supervisor
37
38
import displayio
38
39
import terminalio
39
40
51
52
try :
52
53
import adafruit_display_text .label
53
54
except ImportError :
54
- print ("No library: adafruit_display_text" )
55
+ print ("No display text library: adafruit_display_text" )
55
56
56
57
### For accelerometer
57
58
try :
58
59
import adafruit_lsm6ds .lsm6ds33
59
60
except ImportError :
60
61
print ("No accelerometer library: adafruit_lsm6ds" )
61
62
62
- ### For compass
63
+ ### For magnetometer / compass
63
64
try :
64
65
import adafruit_lis3mdl
65
66
except ImportError :
66
67
print ("No magnetometer library: adafruit_lis3mdl" )
67
68
69
+ ### For light level
70
+ try :
71
+ import adafruit_apds9960 .apds9960
72
+ except ImportError :
73
+ print ("No light sensor library: adafruit_apds9960" )
74
+
75
+
68
76
### For MicroBitDisplayViewEnhanced
69
77
try :
70
78
import display_pin
@@ -103,7 +111,7 @@ class ClueSpeaker:
103
111
104
112
def __init__ (self ):
105
113
self ._audio = None
106
- self ._sample_len = 35
114
+ self ._sample_len = 21
107
115
sine_wave = array .array ("H" , _makeSample (self ._sample_len ))
108
116
self ._wave_sample = audiocore .RawSample (sine_wave )
109
117
@@ -150,7 +158,7 @@ def panic(error_code):
150
158
151
159
152
160
def reset ():
153
- raise NotImplementedError
161
+ supervisor . reload ()
154
162
155
163
156
164
### Some class to manage the calls to update()
@@ -341,6 +349,7 @@ def __init__(self, display=None, ### pylint: disable=redefined-outer-name
341
349
led_cols = 5 ,
342
350
font = MicroBitFonts .STANDARD ,
343
351
font_widths = MicroBitFonts .STANDARD_WIDTHS ,
352
+ light_sensor = None ,
344
353
exception = False ,
345
354
display_show = True ):
346
355
"""disp active display
@@ -358,8 +367,9 @@ def __init__(self, display=None, ### pylint: disable=redefined-outer-name
358
367
self ._initView (display , mode ,
359
368
led_rows = led_rows , led_cols = led_cols )
360
369
361
- ###
362
- self .ligtsensorthingy = None ### TODO
370
+ self ._light_sensor = light_sensor
371
+ if light_sensor :
372
+ light_sensor .enable_color = True
363
373
self ._showing = None
364
374
self ._scrolling = None
365
375
self ._led_rows = led_rows
@@ -637,7 +647,11 @@ def _nopOrE(self):
637
647
### Rather clever implementation on micro:bit although there is a visible flicker
638
648
def read_light_level (self ):
639
649
""" 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?" )
641
655
642
656
643
657
@property
@@ -1010,6 +1024,7 @@ def updatePin(self, pin_name, pin_type, value):
1010
1024
##print("updatePin", pin_name, pin_type, value)
1011
1025
1012
1026
### Disable auto_refresh to reduce flicker and increase efficiency
1027
+ ### this reduces a pwm sweep of range(0, 1024, 4) from 40s to 19s
1013
1028
restore_refresh = None
1014
1029
if self ._display :
1015
1030
restore_refresh = self ._display .auto_refresh
@@ -1596,7 +1611,7 @@ def _analog(self, direction):
1596
1611
frequency = self ._frequency ,
1597
1612
duty_cycle = 0 ,
1598
1613
variable_frequency = True )
1599
- ### microbit behaviour is unused for write_analog(0)
1614
+ ### microbit mode is unused for write_analog(0)
1600
1615
### https://forum.micropython.org/viewtopic.php?t=8933&p=50377
1601
1616
self ._mode = "write_analog" if direction == "out" else "music"
1602
1617
self ._deinit = self ._deinitAnalog
@@ -1785,7 +1800,7 @@ def _accelToPitchRoll(x, y, z):
1785
1800
1786
1801
1787
1802
### TODO - micro:bit calibrates if not calibrated when methods
1788
- ### are called that return data -
1803
+ ### are called that return data
1789
1804
### ponder storage using nvm module for eeprom-like storage
1790
1805
### could use magic identifer number a la microbit and NaN for NA numbers
1791
1806
class MicroBitCompass :
@@ -2007,8 +2022,17 @@ def removeHookPins(cls, method_name, cb, cb_args):
2007
2022
button_a = MicroBitButton (pin5 )
2008
2023
button_b = MicroBitButton (pin11 )
2009
2024
2025
+
2026
+ try :
2027
+ apds9660_sensor = adafruit_apds9960 .apds9960 .APDS9960 (board .I2C ())
2028
+ except NameError :
2029
+ apds9660_sensor = None
2030
+
2010
2031
### 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
2012
2036
2013
2037
### These have some lazy initialisation to stop the instantiation
2014
2038
### blowing up if the relevant CircuitPython libraries aren't present in /lib
0 commit comments