1
- from jem import jemled , jembuzzer
1
+ from jem import jemled , jembuzzer , jemrange , jemlight , jemimu
2
+ from jem import jembarometer
2
3
from ble_uart_peripheral import JEMBLE
3
- import time
4
+ import time , json
4
5
import micropython
5
-
6
+ imu = jemimu .JemIMU () # orientation angles, accel, magnetometer, gyro ..etc
7
+ range = jemrange .JemRange () # make distance sensor object
8
+ light = jemlight .JemLight () # light sensor
9
+ atm = jembarometer .JemBarometer () # atospheric sensor (temp, press, humdity..etc)
6
10
ble = JEMBLE () # grab global BLE handler
7
11
led = jemled .JemLed ()
8
- ext_char = None
12
+ buzz = jembuzzer .JemBuzzer ()
13
+ period_ms = 50
14
+ # ext_char: a ble characteristic of the remote control ble service
15
+ # the ble service that your demo.vue can use to receive kit specific info from JEM
16
+ rc_uart = ble .get_service_by_name ('rc_uart' )
17
+ ext_char = rc_uart .get_char_by_name ('extra' )
18
+
19
+ color_mode = "none"
20
+ global_color = (0 ,0 ,0 ) # rgb
9
21
10
22
def led_red ():
11
23
led .set_color ((100 , 0 , 0 ))
@@ -20,22 +32,91 @@ def led_blue():
20
32
# in repl, you can set running to False to disable the run function below
21
33
running = True
22
34
35
+ def color_range (hue ):
36
+ if hue == 0 or hue > 360 :
37
+ return (0 ,0 ,0 )
38
+ # Ensure hue is in the range [0, 360)
39
+ hue = hue % 360
40
+
41
+ # Convert hue to the range [0, 1]
42
+ hue /= 360.0
43
+
44
+ # HSV to RGB conversion
45
+ c = 1.0
46
+ x = (1.0 - abs ((hue * 6.0 ) % 2 - 1.0 ))
47
+ m = 0.0
48
+
49
+ if 0 <= hue < 1 / 6 :
50
+ r , g , b = c , x , 0
51
+ elif 1 / 6 <= hue < 2 / 6 :
52
+ r , g , b = x , c , 0
53
+ elif 2 / 6 <= hue < 3 / 6 :
54
+ r , g , b = 0 , c , x
55
+ elif 3 / 6 <= hue < 4 / 6 :
56
+ r , g , b = 0 , x , c
57
+ elif 4 / 6 <= hue < 5 / 6 :
58
+ r , g , b = x , 0 , c
59
+ else :
60
+ r , g , b = c , 0 , x
61
+ scale = 75
62
+ r = int ((r + m ) * scale )
63
+ g = int ((g + m ) * scale )
64
+ b = int ((b + m ) * scale )
65
+
66
+ return (r , g , b )
67
+
68
+ def color_temperature (temp ):
69
+ # Map temperature to hue in the range [0, 360)
70
+ if 28 <= temp <= 31 :
71
+ hue = ((temp - 28 ) / 3 ) * 360
72
+ else :
73
+ hue = 1000 # Default value for other temperatures
74
+
75
+ # Pass the hue to the generate_rgb_color function
76
+ return color_range (hue ) #(r,g,b)
77
+
78
+ def color_imu (r , p , y ):
79
+ return (abs (int (r )), abs (int (p )), abs (int (y )))
80
+
23
81
def write (data ):
24
82
global ext_char
25
83
ble .write (ext_char , data )
26
84
27
85
def run ():
28
- global ext_char
29
86
print ("Demo Kit starting" )
30
- # remote controller ble service
31
- # the ble service that your demo.vue can use to receive kit specific info from JEM
32
- rc_uart = ble .get_service_by_name ('rc_uart' )
33
- # an extra ble characteristic used by kit to notify mobile app of useful info, like sensor data
34
- ext_char = rc_uart .get_char_by_name ('extra' )
87
+ atm_value = atm .read ()
88
+ start_ms = time .ticks_ms ()
35
89
while running :
36
90
try :
37
- time .sleep (5 )
38
- write (b'hello from demo kit!' )
91
+ update_color = True
92
+ time .sleep_ms (period_ms )
93
+ # angles, example {'roll': -1.75, 'yaw': 359.9375, 'pitch': -5.9375}
94
+ angles = imu .orientation
95
+ json_dict = {"range" : range .distance , "light" : light .intensity * 100 , "imu" : angles }
96
+ json_dict ["atm" ] = atm_value #atm.read()
97
+ json_str = json .dumps (json_dict )
98
+ write (bytes (json_str .encode ('utf-8' )))
99
+ d = range .distance
100
+ if d >= 250 :
101
+ d = 0
102
+ d = d * 360 / 250
103
+ if color_mode == "imu" :
104
+ c = color_imu (angles ['roll' ], angles ['pitch' ], angles ['yaw' ])
105
+ elif color_mode == "temp" :
106
+ c = color_temperature (atm .read ()['temp' ])
107
+ elif color_mode == "range" :
108
+ c = color_range (d )
109
+ elif color_mode == "none" and led .color != global_color :
110
+ c = global_color
111
+ else :
112
+ update_color = False
113
+
114
+ if update_color :
115
+ led .set_color (c )
116
+ elapsed_ms = time .ticks_ms () - start_ms
117
+ if elapsed_ms >= 1000 :
118
+ atm_value = atm .read ()
119
+ start_ms = time .ticks_ms ()
39
120
except Exception as e :
40
121
print ("demo kit run failed: %s" % e )
41
122
break # make sure to exit !
0 commit comments