From a6f026ba811a6b439ed0b1ac4d95ce8f9b195c0c Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Sat, 1 Jan 2022 00:26:04 +0200 Subject: [PATCH] Move fresh filesystem contents to template files. --- src/omv/boards/OPENMVPT/main_py.h | 91 +++++++++++++++++++++ src/omv/ports/stm32/main.c | 111 ++------------------------ src/omv/ports/stm32/omv_portconfig.mk | 1 + src/omv/templates/main_py.h | 15 ++++ src/omv/templates/readme_txt.h | 15 ++++ src/omv/templates/selftest_py.h | 74 +++++++++++++++++ 6 files changed, 201 insertions(+), 106 deletions(-) create mode 100644 src/omv/boards/OPENMVPT/main_py.h create mode 100644 src/omv/templates/main_py.h create mode 100644 src/omv/templates/readme_txt.h create mode 100644 src/omv/templates/selftest_py.h diff --git a/src/omv/boards/OPENMVPT/main_py.h b/src/omv/boards/OPENMVPT/main_py.h new file mode 100644 index 000000000..0fe9407f8 --- /dev/null +++ b/src/omv/boards/OPENMVPT/main_py.h @@ -0,0 +1,91 @@ +static const char fresh_main_py[] = +"import sensor, image, time, lcd, fir, math, pyb\n" +"\n" +"from machine import I2C\n" +"from vl53l1x import VL53L1X\n" +"\n" +"# Color Tracking Thresholds (Grayscale Min, Grayscale Max)\n" +"threshold_list = [(200, 255)]\n" +"\n" +"def main():\n" +"\n" +" i2c = I2C(2)\n" +" distance = VL53L1X(i2c)\n" +"\n" +" sensor.reset()\n" +" sensor.set_pixformat(sensor.RGB565)\n" +" sensor.set_framesize(sensor.VGA)\n" +" time.sleep_ms(50)\n" +"\n" +" fir.init(fir.FIR_LEPTON)\n" +" fir_img = sensor.alloc_extra_fb(fir.width(), fir.height(), sensor.GRAYSCALE)\n" +" time.sleep_ms(50)\n" +"\n" +" lcd.init(lcd.LCD_DISPLAY_WITH_HDMI, framesize=lcd.FWVGA, refresh=60)\n" +"\n" +" alpha_pal = image.Image(256, 1, sensor.GRAYSCALE)\n" +" for i in range(256): alpha_pal[i] = int(math.pow((i / 255), 2) * 255)\n" +"\n" +" to_min = None\n" +" to_max = None\n" +"\n" +" def map_g_to_temp(g):\n" +" return ((g * (to_max - to_min)) / 255.0) + to_min\n" +"\n" +" while True:\n" +" img = sensor.snapshot()\n" +" # ta: Ambient temperature\n" +" # ir: Object temperatures (IR array)\n" +" # to_min: Minimum object temperature\n" +" # to_max: Maximum object temperature\n" +" ta, ir, to_min, to_max = fir.read_ir()\n" +"\n" +" fir.draw_ir(fir_img, ir, color_palette = None)\n" +" fir_img_size = fir_img.width() * fir_img.height()\n" +"\n" +" # Find IR Blobs\n" +" blobs = fir_img.find_blobs(threshold_list,\n" +" pixels_threshold = (fir_img_size // 100),\n" +" area_threshold = (fir_img_size // 100),\n" +" merge = True)\n" +"\n" +" # Collect stats into a list of tuples\n" +" blob_stats = []\n" +" for b in blobs:\n" +" blob_stats.append((b.rect(), map_g_to_temp(img.get_statistics(thresholds = threshold_list,\n" +" roi = b.rect()).mean())))\n" +" x_scale = img.width() / fir_img.width()\n" +" y_scale = img.height() / fir_img.height()\n" +" img.draw_image(fir_img, 0, 0, x_scale = x_scale, y_scale = y_scale,\n" +" color_palette = sensor.PALETTE_IRONBOW,\n" +" alpha_palette = alpha_pal,\n" +" hint = image.BICUBIC)\n" +"\n" +" # Draw stuff on the colored image\n" +" for b in blobs:\n" +" img.draw_rectangle(int(b.rect()[0] * x_scale), int(b.rect()[1] * y_scale),\n" +" int(b.rect()[2] * x_scale), int(b.rect()[3] * y_scale))\n" +" img.draw_cross(int(b.cx() * x_scale), int(b.cy() * y_scale))\n" +" for blob_stat in blob_stats:\n" +" img.draw_string(int((blob_stat[0][0] * x_scale) + 4), int((blob_stat[0][1] * y_scale) + 1),\n" +" '%.2f C' % blob_stat[1], mono_space = False, scale = 2)\n" +"\n" +" # Draw ambient, min and max temperatures.\n" +" img.draw_string(4, 0, 'Lepton Temp: %0.2f C' % ta, color = (255, 255, 255), mono_space = False, scale = 2)\n" +" img.draw_string(4, 18, 'Min Temp: %0.2f C' % to_min, color = (255, 255, 255), mono_space = False, scale = 2)\n" +" img.draw_string(4, 36, 'Max Temp: %0.2f C' % to_max, color = (255, 255, 255), mono_space = False, scale = 2)\n" +" img.draw_string(4, 54, 'Distance: %d mm' % distance.read(), color = (255, 255, 255), mono_space = False, scale = 2)\n" +"\n" +" lcd.display(img, x_size = lcd.width(), hint = image.BILINEAR)\n" +"\n" +"try:\n" +" main()\n" +"except OSError:\n" +"\n" +" # I2C Bus may be stuck\n" +" p = pyb.Pin('P4', pyb.Pin.OUT_OD)\n" +" for i in range(20000):\n" +" p.value(not p.value())\n" +"\n" +" pyb.hard_reset()\n" +; diff --git a/src/omv/ports/stm32/main.c b/src/omv/ports/stm32/main.c index 0f6f73118..c2b306e1a 100644 --- a/src/omv/ports/stm32/main.c +++ b/src/omv/ports/stm32/main.c @@ -98,113 +98,12 @@ static fs_user_mount_t *vfs_fat = (fs_user_mount_t *) &_vfs_buf; pyb_thread_t pyb_thread_main; #endif -static const char fresh_main_py[] = -"# main.py -- put your code here!\n" -"import pyb, time\n" -"led = pyb.LED(3)\n" -"usb = pyb.USB_VCP()\n" -"while (usb.isconnected()==False):\n" -" led.on()\n" -" time.sleep_ms(150)\n" -" led.off()\n" -" time.sleep_ms(100)\n" -" led.on()\n" -" time.sleep_ms(150)\n" -" led.off()\n" -" time.sleep_ms(600)\n" -; - -static const char fresh_readme_txt[] = -"Thank you for supporting the OpenMV project!\r\n" -"\r\n" -"To download the IDE, please visit:\r\n" -"https://openmv.io/pages/download\r\n" -"\r\n" -"For tutorials and documentation, please visit:\r\n" -"http://docs.openmv.io/\r\n" -"\r\n" -"For technical support and projects, please visit the forums:\r\n" -"http://forums.openmv.io/\r\n" -"\r\n" -"Please use github to report bugs and issues:\r\n" -"https://github.com/openmv/openmv\r\n" -; - +// Fresh filesystem templates. +// NOTE: use the same template name in board dir to override. +#include "main_py.h" +#include "readme_txt.h" #if (OMV_ENABLE_SELFTEST == 1) -static const char fresh_selftest_py[] = -"import sensor, time, pyb\n" -"\n" -"def test_int_adc():\n" -" adc = pyb.ADCAll(12)\n" -" # Test VBAT\n" -" vbat = adc.read_core_vbat()\n" -" vbat_diff = abs(vbat-"OMV_CORE_VBAT")\n" -" if (vbat_diff > 0.15):\n" -" raise Exception('INTERNAL ADC TEST FAILED VBAT=%fv'%vbat)\n" -"\n" -" # Test VREF\n" -" vref = adc.read_core_vref()\n" -" vref_diff = abs(vref-1.2)\n" -" if (vref_diff > 0.1):\n" -" raise Exception('INTERNAL ADC TEST FAILED VREF=%fv'%vref)\n" -" adc = None\n" -" print('INTERNAL ADC TEST PASSED...')\n" -"\n" -"def test_color_bars():\n" -" sensor.reset()\n" -" # Set sensor settings\n" -" sensor.set_brightness(0)\n" -" sensor.set_saturation(3)\n" -" sensor.set_gainceiling(8)\n" -" sensor.set_contrast(2)\n" -"\n" -" # Set sensor pixel format\n" -" sensor.set_framesize(sensor.QVGA)\n" -" sensor.set_pixformat(sensor.RGB565)\n" -"\n" -" # Enable colorbar test mode\n" -" sensor.set_colorbar(True)\n" -"\n" -" # Skip a few frames to allow the sensor settle down\n" -" for i in range(0, 100):\n" -" image = sensor.snapshot()\n" -"\n" -" #color bars thresholds\n" -" t = [lambda r, g, b: r < 70 and g < 70 and b < 70, # Black\n" -" lambda r, g, b: r < 70 and g < 70 and b > 200, # Blue\n" -" lambda r, g, b: r > 200 and g < 70 and b < 70, # Red\n" -" lambda r, g, b: r > 200 and g < 70 and b > 200, # Purple\n" -" lambda r, g, b: r < 70 and g > 200 and b < 70, # Green\n" -" lambda r, g, b: r < 70 and g > 200 and b > 200, # Aqua\n" -" lambda r, g, b: r > 200 and g > 200 and b < 70, # Yellow\n" -" lambda r, g, b: r > 200 and g > 200 and b > 200] # White\n" -"\n" -" # color bars are inverted for OV7725\n" -" if (sensor.get_id() == sensor.OV7725):\n" -" t = t[::-1]\n" -"\n" -" #320x240 image with 8 color bars each one is approx 40 pixels.\n" -" #we start from the center of the frame buffer, and average the\n" -" #values of 10 sample pixels from the center of each color bar.\n" -" for i in range(0, 8):\n" -" avg = (0, 0, 0)\n" -" idx = 40*i+20 #center of colorbars\n" -" for off in range(0, 10): #avg 10 pixels\n" -" rgb = image.get_pixel(idx+off, 120)\n" -" avg = tuple(map(sum, zip(avg, rgb)))\n" -"\n" -" if not t[i](avg[0]/10, avg[1]/10, avg[2]/10):\n" -" raise Exception('COLOR BARS TEST FAILED.'\n" -" 'BAR#(%d): RGB(%d,%d,%d)'%(i+1, avg[0]/10, avg[1]/10, avg[2]/10))\n" -"\n" -" print('COLOR BARS TEST PASSED...')\n" -"\n" -"if __name__ == '__main__':\n" -" print('')\n" -" test_int_adc()\n" -" if sensor.get_id() == sensor.OV7725: test_color_bars()\n" -"\n" -; +#include "selftest_py.h" #endif void flash_error(int n) { diff --git a/src/omv/ports/stm32/omv_portconfig.mk b/src/omv/ports/stm32/omv_portconfig.mk index c6861b4c9..591feecf3 100644 --- a/src/omv/ports/stm32/omv_portconfig.mk +++ b/src/omv/ports/stm32/omv_portconfig.mk @@ -34,6 +34,7 @@ OMV_CFLAGS += -I$(TOP_DIR)/$(OMV_DIR)/common/ OMV_CFLAGS += -I$(TOP_DIR)/$(OMV_DIR)/imlib/ OMV_CFLAGS += -I$(TOP_DIR)/$(OMV_DIR)/modules/ OMV_CFLAGS += -I$(TOP_DIR)/$(OMV_DIR)/sensors/ +OMV_CFLAGS += -I$(TOP_DIR)/$(OMV_DIR)/templates/ OMV_CFLAGS += -I$(TOP_DIR)/$(OMV_DIR)/ports/$(PORT)/ OMV_CFLAGS += -I$(TOP_DIR)/$(OMV_DIR)/ports/$(PORT)/modules/ diff --git a/src/omv/templates/main_py.h b/src/omv/templates/main_py.h new file mode 100644 index 000000000..3201886f1 --- /dev/null +++ b/src/omv/templates/main_py.h @@ -0,0 +1,15 @@ +static const char fresh_main_py[] = +"# main.py -- put your code here!\n" +"import pyb, time\n" +"led = pyb.LED(3)\n" +"usb = pyb.USB_VCP()\n" +"while (usb.isconnected()==False):\n" +" led.on()\n" +" time.sleep_ms(150)\n" +" led.off()\n" +" time.sleep_ms(100)\n" +" led.on()\n" +" time.sleep_ms(150)\n" +" led.off()\n" +" time.sleep_ms(600)\n" +; diff --git a/src/omv/templates/readme_txt.h b/src/omv/templates/readme_txt.h new file mode 100644 index 000000000..d3fe78c67 --- /dev/null +++ b/src/omv/templates/readme_txt.h @@ -0,0 +1,15 @@ +static const char fresh_readme_txt[] = +"Thank you for supporting the OpenMV project!\r\n" +"\r\n" +"To download the IDE, please visit:\r\n" +"https://openmv.io/pages/download\r\n" +"\r\n" +"For tutorials and documentation, please visit:\r\n" +"http://docs.openmv.io/\r\n" +"\r\n" +"For technical support and projects, please visit the forums:\r\n" +"http://forums.openmv.io/\r\n" +"\r\n" +"Please use github to report bugs and issues:\r\n" +"https://github.com/openmv/openmv\r\n" +; diff --git a/src/omv/templates/selftest_py.h b/src/omv/templates/selftest_py.h new file mode 100644 index 000000000..a181678f4 --- /dev/null +++ b/src/omv/templates/selftest_py.h @@ -0,0 +1,74 @@ +static const char fresh_selftest_py[] = +"import sensor, time, pyb\n" +"\n" +"def test_int_adc():\n" +" adc = pyb.ADCAll(12)\n" +" # Test VBAT\n" +" vbat = adc.read_core_vbat()\n" +" vbat_diff = abs(vbat-"OMV_CORE_VBAT")\n" +" if (vbat_diff > 0.15):\n" +" raise Exception('INTERNAL ADC TEST FAILED VBAT=%fv'%vbat)\n" +"\n" +" # Test VREF\n" +" vref = adc.read_core_vref()\n" +" vref_diff = abs(vref-1.2)\n" +" if (vref_diff > 0.1):\n" +" raise Exception('INTERNAL ADC TEST FAILED VREF=%fv'%vref)\n" +" adc = None\n" +" print('INTERNAL ADC TEST PASSED...')\n" +"\n" +"def test_color_bars():\n" +" sensor.reset()\n" +" # Set sensor settings\n" +" sensor.set_brightness(0)\n" +" sensor.set_saturation(3)\n" +" sensor.set_gainceiling(8)\n" +" sensor.set_contrast(2)\n" +"\n" +" # Set sensor pixel format\n" +" sensor.set_framesize(sensor.QVGA)\n" +" sensor.set_pixformat(sensor.RGB565)\n" +"\n" +" # Enable colorbar test mode\n" +" sensor.set_colorbar(True)\n" +"\n" +" # Skip a few frames to allow the sensor settle down\n" +" for i in range(0, 100):\n" +" image = sensor.snapshot()\n" +"\n" +" #color bars thresholds\n" +" t = [lambda r, g, b: r < 70 and g < 70 and b < 70, # Black\n" +" lambda r, g, b: r < 70 and g < 70 and b > 200, # Blue\n" +" lambda r, g, b: r > 200 and g < 70 and b < 70, # Red\n" +" lambda r, g, b: r > 200 and g < 70 and b > 200, # Purple\n" +" lambda r, g, b: r < 70 and g > 200 and b < 70, # Green\n" +" lambda r, g, b: r < 70 and g > 200 and b > 200, # Aqua\n" +" lambda r, g, b: r > 200 and g > 200 and b < 70, # Yellow\n" +" lambda r, g, b: r > 200 and g > 200 and b > 200] # White\n" +"\n" +" # color bars are inverted for OV7725\n" +" if (sensor.get_id() == sensor.OV7725):\n" +" t = t[::-1]\n" +"\n" +" #320x240 image with 8 color bars each one is approx 40 pixels.\n" +" #we start from the center of the frame buffer, and average the\n" +" #values of 10 sample pixels from the center of each color bar.\n" +" for i in range(0, 8):\n" +" avg = (0, 0, 0)\n" +" idx = 40*i+20 #center of colorbars\n" +" for off in range(0, 10): #avg 10 pixels\n" +" rgb = image.get_pixel(idx+off, 120)\n" +" avg = tuple(map(sum, zip(avg, rgb)))\n" +"\n" +" if not t[i](avg[0]/10, avg[1]/10, avg[2]/10):\n" +" raise Exception('COLOR BARS TEST FAILED.'\n" +" 'BAR#(%d): RGB(%d,%d,%d)'%(i+1, avg[0]/10, avg[1]/10, avg[2]/10))\n" +"\n" +" print('COLOR BARS TEST PASSED...')\n" +"\n" +"if __name__ == '__main__':\n" +" print('')\n" +" test_int_adc()\n" +" if sensor.get_id() == sensor.OV7725: test_color_bars()\n" +"\n" +;