@@ -172,6 +172,8 @@ def options(opt):
172
172
opt .add_option ('--reboot_on_bt_crash' , action = 'store_true' , help = 'Forces a BT '
173
173
'chip crash to immediately force a system reboot instead of just cycling airplane mode. '
174
174
'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' )
175
177
176
178
177
179
def handle_configure_options (conf ):
@@ -335,6 +337,89 @@ def handle_configure_options(conf):
335
337
336
338
if not conf .options .mfg and not conf .options .no_pulse_everywhere :
337
339
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 } " )
338
423
339
424
def _create_cm0_env (conf ):
340
425
prev_env = conf .env
0 commit comments