Skip to content

Commit 9cf9cd0

Browse files
committed
fw: enable HRM on Silk using flag
Signed-off-by: Joshua Jun <[email protected]>
1 parent db6558c commit 9cf9cd0

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

platform/platform_capabilities.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@
184184
{
185185
'HAS_ACCESSORY_CONNECTOR',
186186
'HAS_APP_GLANCES',
187-
# 'HAS_BUILTIN_HRM', -- TODO: disabled because driver was removed
187+
# 'HAS_BUILTIN_HRM', -- disabled by default
188188
'HAS_CORE_NAVIGATION4',
189189
'HAS_HEALTH_TRACKING',
190190
'HAS_JAVASCRIPT',
@@ -209,7 +209,7 @@
209209
'COMPOSITOR_USES_DMA',
210210
'HAS_ACCESSORY_CONNECTOR',
211211
'HAS_APP_GLANCES',
212-
# 'HAS_BUILTIN_HRM', -- TODO: disabled because driver was removed
212+
# 'HAS_BUILTIN_HRM', -- disabled by default
213213
'HAS_CORE_NAVIGATION4',
214214
'HAS_GLYPH_BITMAP_CACHING',
215215
'HAS_HEALTH_TRACKING',
@@ -323,6 +323,10 @@ def get_capability_dict(ctx, board):
323323
if ctx.env.NOJS:
324324
capabilities_of_board.discard('HAS_JAVASCRIPT')
325325

326+
if ctx.env.SILK_HR:
327+
# Enable builtin HRM for Silk
328+
capabilities_of_board.add('HAS_BUILTIN_HRM')
329+
326330
# End overrides section
327331

328332
false_capabilities = master_capability_set - capabilities_of_board

src/fw/drivers/wscript_build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ elif mcu_family == 'NRF52840':
15231523
],
15241524
)
15251525

1526-
if bld.get_hrm() == 'AS7000':
1526+
if bld.get_hrm() == 'AS7000' and bld.env.SILK_HR:
15271527
bld.objects(
15281528
name='driver_hrm',
15291529
source=[

src/fw/wscript

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ def _build_normal(bld):
581581

582582
excludes.extend(_get_launcher_globs_to_exclude(bld))
583583

584-
if not bld.capability('HAS_BUILTIN_HRM'):
584+
if not bld.capability('HAS_BUILTIN_HRM') or not bld.env.SILK_HR:
585585
excludes.append('popups/ble_hrm/**')
586586

587587
if bld.is_tintin():

wscript

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ def options(opt):
172172
opt.add_option('--reboot_on_bt_crash', action='store_true', help='Forces a BT '
173173
'chip crash to immediately force a system reboot instead of just cycling airplane mode. '
174174
'This makes it easier for us to actually get crash info')
175+
opt.add_option('--enable_silk_hr', action='store_true',
176+
help='Enable heart rate sensor on Silk, downloads the proprietary firmware from the server if available')
175177

176178

177179
def handle_configure_options(conf):
@@ -335,6 +337,89 @@ def handle_configure_options(conf):
335337

336338
if not conf.options.mfg and not conf.options.no_pulse_everywhere:
337339
conf.env.append_value('DEFINES', 'PULSE_EVERYWHERE=1')
340+
341+
if conf.options.enable_silk_hr and conf.is_silk():
342+
conf.env.SILK_HR = True
343+
print("Enabling Silk heart rate sensor")
344+
345+
import urllib.request
346+
import json
347+
348+
# Store the firmware in resources/normal/silk/blobs
349+
hr_fw_dir = conf.path.make_node('resources/normal/silk/blobs')
350+
if not os.path.exists(hr_fw_dir.abspath()):
351+
os.makedirs(hr_fw_dir.abspath())
352+
353+
# Path to save the firmware
354+
hr_fw_path = hr_fw_dir.make_node('as7000_fw_image.bin')
355+
conf.env.HR_FW_PATH = hr_fw_path.abspath()
356+
357+
# Download the firmware if it doesn't exist
358+
if not os.path.exists(hr_fw_path.abspath()):
359+
print("Downloading AS7000 heart rate sensor firmware...")
360+
hr_fw_url = "https://github.com/jplexer/AS7000_FW/raw/refs/heads/main/as7000_fw_image.bin"
361+
try:
362+
urllib.request.urlretrieve(hr_fw_url, hr_fw_path.abspath())
363+
print("AS7000 firmware downloaded successfully")
364+
except Exception as e:
365+
print(f"Error downloading AS7000 firmware: {e}")
366+
else:
367+
print(f"Using existing AS7000 firmware at {hr_fw_path.abspath()}")
368+
369+
# Add the firmware to the resource map manually to preserve formatting
370+
resource_map_path = conf.path.make_node('resources/normal/silk/resource_map.json')
371+
if os.path.exists(resource_map_path.abspath()):
372+
try:
373+
# Check if our entry is already there
374+
with open(resource_map_path.abspath(), 'r') as f:
375+
content = f.read()
376+
377+
if '"name": "AS7000_FW_IMAGE"' not in content:
378+
print("Adding AS7000 firmware to resource map")
379+
380+
# Find the position to insert our entry - right before the last entry
381+
insert_pos = content.rindex(' ]')
382+
383+
# Create our entry with the same formatting style as the rest of the file
384+
new_entry = ',\n {\n "type": "raw",\n "name": "AS7000_FW_IMAGE",\n "file": "normal/silk/blobs/as7000_fw_image.bin",\n "builtin": true\n }'
385+
386+
# Insert our entry
387+
new_content = content[:insert_pos] + new_entry + content[insert_pos:]
388+
389+
# Write back the modified content
390+
with open(resource_map_path.abspath(), 'w') as f:
391+
f.write(new_content)
392+
393+
print("AS7000 firmware added to resource map")
394+
else:
395+
print("AS7000 firmware already present in resource map")
396+
except Exception as e:
397+
print(f"Error modifying resource map: {e}")
398+
else:
399+
# If Silk HR is not enabled, remove the AS7000 firmware entry manually
400+
if conf.is_silk():
401+
resource_map_path = conf.path.make_node('resources/normal/silk/resource_map.json')
402+
if os.path.exists(resource_map_path.abspath()):
403+
try:
404+
with open(resource_map_path.abspath(), 'r') as f:
405+
content = f.read()
406+
407+
# Try to find and remove our entry
408+
import re
409+
pattern = r',?\s*\{\s*"type"\s*:\s*"raw"\s*,\s*"name"\s*:\s*"AS7000_FW_IMAGE"\s*,[^}]*\}'
410+
new_content = re.sub(pattern, '', content)
411+
412+
# Fix any double commas that might result
413+
new_content = new_content.replace(',\n ,', ',')
414+
415+
# Write back the modified content if it changed
416+
if new_content != content:
417+
print("Removing AS7000 firmware from resource map")
418+
with open(resource_map_path.abspath(), 'w') as f:
419+
f.write(new_content)
420+
421+
except Exception as e:
422+
print(f"Error modifying resource map: {e}")
338423

339424
def _create_cm0_env(conf):
340425
prev_env = conf.env

0 commit comments

Comments
 (0)