8000 Merge pull request #85 from kitlab-io/jem2 · kitlab-io/micropython@1e75344 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1e75344

Browse files
Merge pull request #85 from kitlab-io/jem2
Update jem2 with new simple kit and add more fixes to demo kit
2 parents 5b95c26 + f46abe5 commit 1e75344

File tree

6 files changed

+532
-21
lines changed

6 files changed

+532
-21
lines changed

jem/kits/config.kit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"kit": {
3-
"name": "demo"
3+
"name": "simple"
44
}
55
}

jem/kits/demo/demo.py

Lines changed: 93 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
from jem import jemled, jembuzzer
1+
from jem import jemled, jembuzzer, jemrange, jemlight, jemimu
2+
from jem import jembarometer
23
from ble_uart_peripheral import JEMBLE
3-
import time
4+
import time, json
45
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)
610
ble = JEMBLE() # grab global BLE handler
711
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
921

1022
def led_red():
1123
led.set_color((100, 0, 0))
@@ -20,22 +32,91 @@ def led_blue():
2032
# in repl, you can set running to False to disable the run function below
2133
running = True
2234

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+
2381
def write(data):
2482
global ext_char
2583
ble.write(ext_char, data)
2684

2785
def run():
28-
global ext_char
2986
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()
3589
while running:
3690
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()
39120
except Exception as e:
40121
print("demo kit run failed: %s" % e)
41122
break # make sure to exit !

0 commit comments

Comments
 (0)
0