From 822308812163c1d169f8ee76e5b0fd2488ecb39c Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 20 May 2021 02:34:36 +0700 Subject: [PATCH 1/4] Add Repbit32 work --- ports/esp32/boards/Rapbit32/manifest.py | 1 - ports/esp32/boards/Rapbit32/modules/_board_setup.py | 2 ++ ports/esp32/boards/Rapbit32/mpconfigboard.cmake | 8 ++++++++ ports/esp32/boards/Rapbit32/mpconfigboard.h | 3 +-- ports/esp32/boards/Rapbit32/mpconfigboard.mk | 3 --- ports/esp32/main.c | 13 ++----------- ports/esp32/main/CMakeLists.txt | 3 +++ 7 files changed, 16 insertions(+), 17 deletions(-) create mode 100644 ports/esp32/boards/Rapbit32/mpconfigboard.cmake delete mode 100644 ports/esp32/boards/Rapbit32/mpconfigboard.mk diff --git a/ports/esp32/boards/Rapbit32/manifest.py b/ports/esp32/boards/Rapbit32/manifest.py index 901752c25774c..7ae2ed15d9169 100644 --- a/ports/esp32/boards/Rapbit32/manifest.py +++ b/ports/esp32/boards/Rapbit32/manifest.py @@ -1,3 +1,2 @@ include("$(PORT_DIR)/boards/manifest.py") freeze("modules") - diff --git a/ports/esp32/boards/Rapbit32/modules/_board_setup.py b/ports/esp32/boards/Rapbit32/modules/_board_setup.py index d8d40fbc78e81..9337457072022 100644 --- a/ports/esp32/boards/Rapbit32/modules/_board_setup.py +++ b/ports/esp32/boards/Rapbit32/modules/_board_setup.py @@ -1,3 +1,4 @@ +""" from machine import Pin import display import buzzer @@ -10,3 +11,4 @@ display.clear() buzzer.off() usb.off() +""" diff --git a/ports/esp32/boards/Rapbit32/mpconfigboard.cmake b/ports/esp32/boards/Rapbit32/mpconfigboard.cmake new file mode 100644 index 0000000000000..92eef2478d592 --- /dev/null +++ b/ports/esp32/boards/Rapbit32/mpconfigboard.cmake @@ -0,0 +1,8 @@ +set(SDKCONFIG_DEFAULTS + boards/sdkconfig.base + boards/sdkconfig.ble +) + +if(NOT MICROPY_FROZEN_MANIFEST) + set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest.py) +endif() diff --git a/ports/esp32/boards/Rapbit32/mpconfigboard.h b/ports/esp32/boards/Rapbit32/mpconfigboard.h index 274fb682976e4..67d1bc35dce2e 100644 --- a/ports/esp32/boards/Rapbit32/mpconfigboard.h +++ b/ports/esp32/boards/Rapbit32/mpconfigboard.h @@ -1,3 +1,2 @@ -#define MICROPY_HW_BOARD_NAME "KidBright32" +#define MICROPY_HW_BOARD_NAME "Rapbit32" #define MICROPY_HW_MCU_NAME "ESP32" -#define MICROPY_HW_BOARD_KIDBRIGHT32 diff --git a/ports/esp32/boards/Rapbit32/mpconfigboard.mk b/ports/esp32/boards/Rapbit32/mpconfigboard.mk deleted file mode 100644 index adfd91a9f7411..0000000000000 --- a/ports/esp32/boards/Rapbit32/mpconfigboard.mk +++ /dev/null @@ -1,3 +0,0 @@ -SDKCONFIG += boards/sdkconfig.base - -FROZEN_MANIFEST ?= $(BOARD_DIR)/manifest.py diff --git a/ports/esp32/main.c b/ports/esp32/main.c index 12af4907a2fdc..2ed249276ced4 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -81,21 +81,12 @@ void mp_task(void *pvParameter) { #if MICROPY_PY_THREAD mp_thread_init(pxTaskGetStackStart(NULL), MP_TASK_STACK_SIZE / sizeof(uintptr_t)); #endif -<<<<<<< HEAD -<<<<<<< HEAD - + boot_upload_run(); -======= - #if CONFIG_USB_ENABLED - usb_init(); - #else ->>>>>>> 7408ca1d7857df5ea348da35c9ee12f70a024478 -======= #if CONFIG_USB_ENABLED usb_init(); #else ->>>>>>> 7408ca1d7857df5ea348da35c9ee12f70a024478 uart_init(); #endif machine_init(); @@ -154,7 +145,7 @@ void mp_task(void *pvParameter) { // run boot-up scripts pyexec_frozen_module("_boot.py"); -#if defined(MICROPY_HW_BOARD_KIDBRIGHT32) || defined(MICROPY_HW_BOARD_IPSTWIFI) || defined(MICROPY_HW_BOARD_TTGO_T_DISPLAY) +#if defined(MICROPY_HW_BOARD_KIDBRIGHT32) || defined(MICROPY_HW_BOARD_IPSTWIFI) || defined(MICROPY_HW_BOARD_TTGO_T_DISPLAY) || defined(MICROPY_HW_BOARD_RAPBIT32) pyexec_frozen_module("_board_setup.py"); #endif pyexec_file_if_exists("boot.py"); diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index 1cc30b71ec558..6beee75f1b91d 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -70,6 +70,9 @@ set(MICROPY_SOURCE_PORT ${PROJECT_DIR}/mpthreadport.c ${PROJECT_DIR}/machine_rtc.c ${PROJECT_DIR}/machine_sdcard.c + ${PROJECT_DIR}/esp_espnow.c + ${PROJECT_DIR}/ring_buffer.c + ${PROJECT_DIR}/boot_upload.c ) set(MICROPY_SOURCE_QSTR From 3abf61717d1810bc9a6d29ec9ad0e60deb0a56a5 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 22 May 2021 23:51:41 +0700 Subject: [PATCH 2/4] Upload new module --- ports/esp32/boards/Rapbit32/modules/buzzer.py | 2 +- .../esp32/boards/Rapbit32/modules/display.py | 604 ++++-------------- ports/esp32/boards/Rapbit32/modules/imu.py | 352 ---------- ports/esp32/boards/Rapbit32/modules/rtc.py | 48 -- ports/esp32/boards/Rapbit32/modules/sensor.py | 105 ++- ports/esp32/boards/Rapbit32/modules/servo.py | 18 +- ports/esp32/boards/Rapbit32/modules/switch.py | 30 +- .../boards/Rapbit32/modules/ultrasonic.py | 20 + ports/esp32/boards/Rapbit32/modules/usb.py | 21 - ports/esp32/boards/Rapbit32/mpconfigboard.h | 1 + 10 files changed, 260 insertions(+), 941 deletions(-) delete mode 100644 ports/esp32/boards/Rapbit32/modules/imu.py delete mode 100644 ports/esp32/boards/Rapbit32/modules/rtc.py create mode 100644 ports/esp32/boards/Rapbit32/modules/ultrasonic.py delete mode 100644 ports/esp32/boards/Rapbit32/modules/usb.py diff --git a/ports/esp32/boards/Rapbit32/modules/buzzer.py b/ports/esp32/boards/Rapbit32/modules/buzzer.py index 67a7ff3be3426..3706d8029b641 100644 --- a/ports/esp32/boards/Rapbit32/modules/buzzer.py +++ b/ports/esp32/boards/Rapbit32/modules/buzzer.py @@ -44,7 +44,7 @@ "SIL": 0 } -__buzzer = PWM(Pin(13), freq=2000, duty=0) +__buzzer = PWM(Pin(12), freq=2000, duty=0) volume = 50 bpm = 120 diff --git a/ports/esp32/boards/Rapbit32/modules/display.py b/ports/esp32/boards/Rapbit32/modules/display.py index 580459b741b17..c88bb44f0ac79 100644 --- a/ports/esp32/boards/Rapbit32/modules/display.py +++ b/ports/esp32/boards/Rapbit32/modules/display.py @@ -1,486 +1,122 @@ -# Original C++ code from https://gitlab.com/kidbright/kidbright32/-/blob/master/kidbright32/src/ht16k33.cpp -# Port & Dev to MicroPython by Sonthaya Nongnuch - -from time import sleep +from micropython import const +import framebuf from machine import Pin, I2C -i2c0 = I2C(0, scl=Pin(22), sda=Pin(21), freq=100000) - -HT16K33_ADDR = const(0x70) - -HT16K33_OSC_OFF = const(0x20) -HT16K33_OSC_ON = const(0x21) -HT16K33_DISP_OFF = const(0x80) -HT16K33_DISP_ON = const(0x81) -HT16K33_DIM_SET_8_16 = const(0xe7) -HT16K33_DIM_SET_16_16 = const(0xef) -HT16K33_DISP_ADDR_PTR = const(0x00) - -ht16k33_ptr_conv = (0, 2, 4, 6, 8, 10, 12, 14, 1, 3, 5, 7, 9, 11, 13, 15) - -font_6_8 = bytes([ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # 0x00 - 0x00, 0x7c, 0xa2, 0x8a, 0xa2, 0x7c, # 0x01 - 0x00, 0x7c, 0xd6, 0xf6, 0xd6, 0x7c, # 0x02 - 0x00, 0x38, 0x7c, 0x3e, 0x7c, 0x38, # 0x03 - 0x00, 0x18, 0x3c, 0x7e, 0x3c, 0x18, # 0x04 - 0x00, 0x0c, 0x6c, 0xfe, 0x6c, 0x0c, # 0x05 - 0x00, 0x18, 0x3a, 0x7e, 0x3a, 0x18, # 0x06 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # 0x07 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # 0x08 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # 0x09 - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # 0x0a - 0x00, 0x0c, 0x12, 0x52, 0x6c, 0x70, # 0x0b - 0x00, 0x60, 0x94, 0x9e, 0x94, 0x60, # 0x0c - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # 0x0d - 0x00, 0x06, 0x7e, 0x50, 0xac, 0xfc, # 0x0e - 0x00, 0x54, 0x38, 0x6c, 0x38, 0x54, # 0x0f - 0x00, 0x00, 0xfe, 0x7c, 0x38, 0x10, # 0x10 - 0x00, 0x10, 0x38, 0x7c, 0xfe, 0x00, # 0x11 - 0x00, 0x28, 0x6c, 0xfe, 0x6c, 0x28, # 0x12 - 0x00, 0x00, 0xfa, 0x00, 0xfa, 0x00, # 0x13 - 0x00, 0x60, 0x90, 0xfe, 0x80, 0xfe, # 0x14 - 0x00, 0x44, 0xb2, 0xaa, 0x9a, 0x44, # 0x15 - 0x00, 0x06, 0x06, 0x06, 0x06, 0x00, # 0x16 - 0x00, 0x28, 0x6d, 0xff, 0x6d, 0x28, # 0x17 - 0x00, 0x20, 0x60, 0xfe, 0x60, 0x20, # 0x18 - 0x00, 0x08, 0x0c, 0xfe, 0x0c, 0x08, # 0x19 - 0x00, 0x10, 0x10, 0x7c, 0x38, 0x10, # 0x1a - 0x00, 0x10, 0x38, 0x7c, 0x10, 0x10, # 0x1b - 0x00, 0x1e, 0x02, 0x02, 0x02, 0x02, # 0x1c - 0x00, 0x10, 0x7c, 0x10, 0x7c, 0x10, # 0x1d - 0x00, 0x0c, 0x3c, 0xfc, 0x3c, 0x0c, # 0x1e - 0x00, 0xc0, 0xf0, 0xfc, 0xf0, 0xc0, # 0x1f - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # 0x20 - 0x00, 0x00, 0x60, 0xfa, 0x60, 0x00, # 0x21 - 0x00, 0xe0, 0xc0, 0x00, 0xe0, 0xc0, # 0x22 - 0x00, 0x24, 0x7e, 0x24, 0x7e, 0x24, # 0x23 - 0x00, 0x24, 0xd4, 0x56, 0x48, 0x00, # 0x24 - 0x00, 0xc6, 0xc8, 0x10, 0x26, 0xc6, # 0x25 - 0x00, 0x6c, 0x92, 0x6a, 0x04, 0x0a, # 0x26 - 0x00, 0x00, 0xe0, 0xc0, 0x00, 0x00, # 0x27 - 0x00, 0x00, 0x7c, 0x82, 0x00, 0x00, # 0x28 - 0x00, 0x00, 0x82, 0x7c, 0x00, 0x00, # 0x29 - 0x00, 0x10, 0x7c, 0x38, 0x7c, 0x10, # 0x2a - 0x00, 0x10, 0x10, 0x7c, 0x10, 0x10, # 0x2b - 0x00, 0x00, 0x07, 0x06, 0x00, 0x00, # 0x2c - 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, # 0x2d - 0x00, 0x00, 0x06, 0x06, 0x00, 0x00, # 0x2e - 0x00, 0x04, 0x08, 0x10, 0x20, 0x40, # 0x2f - 0x00, 0x7c, 0x8a, 0x92, 0xa2, 0x7c, # 0x30 - 0x00, 0x00, 0x42, 0xfe, 0x02, 0x00, # 0x31 - 0x00, 0x46, 0x8a, 0x92, 0x92, 0x62, # 0x32 - 0x00, 0x44, 0x92, 0x92, 0x92, 0x6c, # 0x33 - 0x00, 0x18, 0x28, 0x48, 0xfe, 0x08, # 0x34 - 0x00, 0xf4, 0x92, 0x92, 0x92, 0x8c, # 0x35 - 0x00, 0x3c, 0x52, 0x92, 0x92, 0x0c, # 0x36 - 0x00, 0x80, 0x8e, 0x90, 0xa0, 0xc0, # 0x37 - 0x00, 0x6c, 0x92, 0x92, 0x92, 0x6c, # 0x38 - 0x00, 0x60, 0x92, 0x92, 0x94, 0x78, # 0x39 - 0x00, 0x00, 0x36, 0x36, 0x00, 0x00, # 0x3a - 0x00, 0x00, 0x37, 0x36, 0x00, 0x00, # 0x3b - 0x00, 0x10, 0x28, 0x44, 0x82, 0x00, # 0x3c - 0x00, 0x24, 0x24, 0x24, 0x24, 0x24, # 0x3d - 0x00, 0x00, 0x82, 0x44, 0x28, 0x10, # 0x3e - 0x00, 0x40, 0x80, 0x9a, 0x90, 0x60, # 0x3f - 0x00, 0x7c, 0x82, 0xba, 0xaa, 0x78, # 0x40 - 0x00, 0x7e, 0x88, 0x88, 0x88, 0x7e, # 0x41 - 0x00, 0xfe, 0x92, 0x92, 0x92, 0x6c, # 0x42 - 0x00, 0x7c, 0x82, 0x82, 0x82, 0x44, # 0x43 - 0x00, 0xfe, 0x82, 0x82, 0x82, 0x7c, # 0x44 - 0x00, 0xfe, 0x92, 0x92, 0x92, 0x82, # 0x45 - 0x00, 0xfe, 0x90, 0x90, 0x90, 0x80, # 0x46 - 0x00, 0x7c, 0x82, 0x92, 0x92, 0x5e, # 0x47 - 0x00, 0xfe, 0x10, 0x10, 0x10, 0xfe, # 0x48 - 0x00, 0x00, 0x82, 0xfe, 0x82, 0x00, # 0x49 - 0x00, 0x0c, 0x02, 0x02, 0x02, 0xfc, # 0x4a - 0x00, 0xfe, 0x10, 0x28, 0x44, 0x82, # 0x4b - 0x00, 0xfe, 0x02, 0x02, 0x02, 0x02, # 0x4c - 0x00, 0xfe, 0x40, 0x20, 0x40, 0xfe, # 0x4d - 0x00, 0xfe, 0x40, 0x20, 0x10, 0xfe, # 0x4e - 0x00, 0x7c, 0x82, 0x82, 0x82, 0x7c, # 0x4f - 0x00, 0xfe, 0x90, 0x90, 0x90, 0x60, # 0x50 - 0x00, 0x7c, 0x82, 0x8a, 0x84, 0x7a, # 0x51 - 0x00, 0xfe, 0x90, 0x90, 0x98, 0x66, # 0x52 - 0x00, 0x64, 0x92, 0x92, 0x92, 0x4c, # 0x53 - 0x00, 0x80, 0x80, 0xfe, 0x80, 0x80, # 0x54 - 0x00, 0xfc, 0x02, 0x02, 0x02, 0xfc, # 0x55 - 0x00, 0xf8, 0x04, 0x02, 0x04, 0xf8, # 0x56 - 0x00, 0xfc, 0x02, 0x3c, 0x02, 0xfc, # 0x57 - 0x00, 0xc6, 0x28, 0x10, 0x28, 0xc6, # 0x58 - 0x00, 0xe0, 0x10, 0x0e, 0x10, 0xe0, # 0x59 - 0x00, 0x8e, 0x92, 0xa2, 0xc2, 0x00, # 0x5a - 0x00, 0x00, 0xfe, 0x82, 0x82, 0x00, # 0x5b - 0x00, 0x40, 0x20, 0x10, 0x08, 0x04, # 0x5c - 0x00, 0x00, 0x82, 0x82, 0xfe, 0x00, # 0x5d - 0x00, 0x20, 0x40, 0x80, 0x40, 0x20, # 0x5e - 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, # 0x5f - 0x00, 0x00, 0xc0, 0xe0, 0x00, 0x00, # 0x60 - 0x00, 0x04, 0x2a, 0x2a, 0x2a, 0x1e, # 0x61 - 0x00, 0xfe, 0x22, 0x22, 0x22, 0x1c, # 0x62 - 0x00, 0x1c, 0x22, 0x22, 0x22, 0x04, # 0x63 'c' = 0x00, 0x1c, 0x22, 0x22, 0x22, 0x14 - 0x00, 0x1c, 0x22, 0x22, 0x22, 0xfe, # 0x64 - 0x00, 0x1c, 0x2a, 0x2a, 0x2a, 0x10, # 0x65 - 0x00, 0x10, 0x7e, 0x90, 0x90, 0x00, # 0x66 - 0x00, 0x18, 0x25, 0x25, 0x25, 0x3e, # 0x67 - 0x00, 0xfe, 0x20, 0x20, 0x1e, 0x00, # 0x68 - 0x00, 0x00, 0x00, 0x5e, 0x02, 0x00, # 0x69 'i' = 0x00, 0x00, 0x00, 0xbe, 0x02, 0x00 - 0x00, 0x02, 0x01, 0x21, 0xbe, 0x00, # 0x6a - 0x00, 0xfe, 0x08, 0x14, 0x22, 0x00, # 0x6b - 0x00, 0x00, 0x00, 0xfe, 0x02, 0x00, # 0x6c - 0x00, 0x3e, 0x20, 0x18, 0x20, 0x1e, # 0x6d - 0x00, 0x3e, 0x20, 0x20, 0x1e, 0x00, # 0x6e - 0x00, 0x1c, 0x22, 0x22, 0x22, 0x1c, # 0x6f - 0x00, 0x3f, 0x22, 0x22, 0x22, 0x1c, # 0x70 - 0x00, 0x1c, 0x22, 0x22, 0x22, 0x3f, # 0x71 - 0x00, 0x22, 0x1e, 0x22, 0x20, 0x10, # 0x72 - 0x00, 0x10, 0x2a, 0x2a, 0x2a, 0x04, # 0x73 - 0x00, 0x20, 0x7c, 0x22, 0x24, 0x00, # 0x74 - 0x00, 0x3c, 0x02, 0x04, 0x3e, 0x00, # 0x75 - 0x00, 0x38, 0x04, 0x02, 0x04, 0x38, # 0x76 - 0x00, 0x3c, 0x06, 0x0c, 0x06, 0x3c, # 0x77 - 0x00, 0x36, 0x08, 0x08, 0x36, 0x00, # 0x78 - 0x00, 0x39, 0x05, 0x06, 0x3c, 0x00, # 0x79 - 0x00, 0x26, 0x2a, 0x2a, 0x32, 0x00, # 0x7a - 0x00, 0x10, 0x7c, 0x82, 0x82, 0x00, # 0x7b - 0x00, 0x00, 0x00, 0xee, 0x00, 0x00, # 0x7c - 0x00, 0x00, 0x82, 0x82, 0x7c, 0x10, # 0x7d - 0x00, 0x40, 0x80, 0x40, 0x80, 0x00, # 0x7e - 0x00, 0x3c, 0x64, 0xc4, 0x64, 0x3c, # 0x7f - 0x00, 0x78, 0x85, 0x87, 0x84, 0x48, # 0x80 - 0x00, 0xbc, 0x02, 0x04, 0xbe, 0x00, # 0x81 - 0x00, 0x1c, 0x2a, 0x2a, 0xaa, 0x90, # 0x82 - 0x00, 0x04, 0xaa, 0xaa, 0xaa, 0x1e, # 0x83 - 0x00, 0x04, 0xaa, 0x2a, 0xaa, 0x1e, # 0x84 - 0x00, 0x04, 0xaa, 0xaa, 0x2a, 0x1e, # 0x85 - 0x00, 0x04, 0xea, 0xaa, 0xea, 0x1e, # 0x86 - 0x00, 0x38, 0x45, 0x47, 0x44, 0x28, # 0x87 - 0x00, 0x1c, 0xaa, 0xaa, 0xaa, 0x10, # 0x88 - 0x00, 0x1c, 0xaa, 0x2a, 0xaa, 0x10, # 0x89 - 0x00, 0x1c, 0xaa, 0xaa, 0x2a, 0x10, # 0x8a - 0x00, 0x00, 0x80, 0x3e, 0x82, 0x00, # 0x8b - 0x00, 0x00, 0x80, 0xbe, 0x82, 0x00, # 0x8c - 0x00, 0x00, 0x80, 0x3e, 0x02, 0x00, # 0x8d - 0x00, 0x0e, 0x94, 0x24, 0x94, 0x0e, # 0x8e - 0x00, 0x1e, 0xf4, 0xa4, 0xf4, 0x1e, # 0x8f - 0x00, 0x3e, 0x2a, 0x2a, 0xaa, 0xa2, # 0x90 - 0x00, 0x2c, 0x2a, 0x3e, 0x2a, 0x1a, # 0x91 - 0x00, 0x7e, 0x90, 0xfe, 0x92, 0x92, # 0x92 - 0x00, 0x1c, 0xa2, 0xa2, 0x9c, 0x00, # 0x93 - 0x00, 0x1c, 0xa2, 0x22, 0x9c, 0x00, # 0x94 - 0x00, 0x9c, 0xa2, 0x22, 0x1c, 0x00, # 0x95 - 0x00, 0x3c, 0x82, 0x84, 0xbe, 0x00, # 0x96 - 0x00, 0xbc, 0x82, 0x04, 0x3e, 0x00, # 0x97 - 0x00, 0x39, 0x85, 0x06, 0xbc, 0x00, # 0x98 - 0x00, 0xbc, 0x42, 0x42, 0xbc, 0x00, # 0x99 - 0x00, 0x3c, 0x82, 0x02, 0xbc, 0x00, # 0x9a - 0x01, 0x0e, 0x16, 0x1a, 0x1c, 0x20, # 0x9b - 0x00, 0x12, 0x7c, 0x92, 0x92, 0x46, # 0x9c - 0x00, 0x7e, 0x86, 0xba, 0xc2, 0xfc, # 0x9d - 0x00, 0x44, 0x28, 0x10, 0x28, 0x44, # 0x9e - 0x00, 0x02, 0x11, 0x7e, 0x90, 0x40, # 0x9f - 0x00, 0x04, 0x2a, 0xaa, 0xaa, 0x1e, # 0xa0 - 0x00, 0x00, 0x00, 0xbe, 0x82, 0x00, # 0xa1 - 0x00, 0x1c, 0x22, 0xa2, 0x9c, 0x00, # 0xa2 - 0x00, 0x3c, 0x02, 0x84, 0xbe, 0x00, # 0xa3 - 0x00, 0x5e, 0x90, 0x50, 0x8e, 0x00, # 0xa4 - 0x00, 0x5e, 0x88, 0x44, 0x9e, 0x00, # 0xa5 - 0x00, 0x10, 0xaa, 0xaa, 0xaa, 0x7a, # 0xa6 - 0x00, 0x72, 0x8a, 0x8a, 0x72, 0x00, # 0xa7 - 0x00, 0x0c, 0x12, 0xb2, 0x02, 0x04, # 0xa8 - 0x7c, 0x82, 0xba, 0xd2, 0xaa, 0x7c, # 0xa9 - 0x20, 0x20, 0x20, 0x20, 0x20, 0x38, # 0xaa - 0x00, 0xe8, 0x10, 0x32, 0x56, 0x0a, # 0xab - 0x00, 0xe8, 0x10, 0x2c, 0x54, 0x1e, # 0xac - 0x00, 0x00, 0x0c, 0xbe, 0x0c, 0x00, # 0xad - 0x00, 0x10, 0x28, 0x00, 0x10, 0x28, # 0xae - 0x00, 0x28, 0x10, 0x00, 0x28, 0x10, # 0xaf - 0x22, 0x88, 0x22, 0x88, 0x22, 0x88, # 0xb0 - 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, # 0xb1 - 0xdd, 0x77, 0xdd, 0x77, 0xdd, 0x77, # 0xb2 - 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, # 0xb3 - 0x10, 0x10, 0x10, 0xff, 0x00, 0x00, # 0xb4 - 0x00, 0x0e, 0x14, 0xa4, 0x94, 0x0e, # 0xb5 - 0x00, 0x0e, 0x94, 0xa4, 0x94, 0x0e, # 0xb6 - 0x00, 0x0e, 0x94, 0xa4, 0x14, 0x0e, # 0xb7 - 0x7c, 0x82, 0xba, 0xaa, 0x82, 0x7c, # 0xb8 - 0x50, 0xdf, 0x00, 0xff, 0x00, 0x00, # 0xb9 - 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, # 0xba - 0x50, 0x5f, 0x40, 0x7f, 0x00, 0x00, # 0xbb - 0x50, 0xd0, 0x10, 0xf0, 0x00, 0x00, # 0xbc - 0x00, 0x18, 0x24, 0x66, 0x24, 0x00, # 0xbd - 0x00, 0x94, 0x54, 0x3e, 0x54, 0x94, # 0xbe - 0x10, 0x10, 0x10, 0x1f, 0x00, 0x00, # 0xbf - 0x00, 0x00, 0x00, 0xf0, 0x10, 0x10, # 0xc0 - 0x10, 0x10, 0x10, 0xf0, 0x10, 0x10, # 0xc1 - 0x10, 0x10, 0x10, 0x1f, 0x10, 0x10, # 0xc2 - 0x00, 0x00, 0x00, 0xff, 0x10, 0x10, # 0xc3 - 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, # 0xc4 - 0x10, 0x10, 0x10, 0xff, 0x10, 0x10, # 0xc5 - 0x00, 0x04, 0x6a, 0xaa, 0x6a, 0x9e, # 0xc6 - 0x00, 0x0e, 0x54, 0xa4, 0x54, 0x8e, # 0xc7 - 0x00, 0xf0, 0x10, 0xd0, 0x50, 0x50, # 0xc8 - 0x00, 0x7f, 0x40, 0x5f, 0x50, 0x50, # 0xc9 - 0x50, 0xd0, 0x10, 0xd0, 0x50, 0x50, # 0xca - 0x50, 0x5f, 0x40, 0x5f, 0x50, 0x50, # 0xcb - 0x00, 0xff, 0x00, 0xdf, 0x50, 0x50, # 0xcc - 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, # 0xcd - 0x50, 0xdf, 0x00, 0xdf, 0x50, 0x50, # 0xce - 0x00, 0xba, 0x44, 0x44, 0x44, 0xba, # 0xcf - 0x00, 0x44, 0xaa, 0x9a, 0x0c, 0x00, # 0xd0 - 0x00, 0x10, 0xfe, 0x92, 0x82, 0x7c, # 0xd1 - 0x00, 0x3e, 0xaa, 0xaa, 0xaa, 0x22, # 0xd2 - 0x00, 0x3e, 0xaa, 0x2a, 0xaa, 0x22, # 0xd3 - 0x00, 0x3e, 0xaa, 0xaa, 0x2a, 0x22, # 0xd4 - 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, # 0xd5 - 0x00, 0x00, 0x22, 0xbe, 0xa2, 0x00, # 0xd6 - 0x00, 0x00, 0xa2, 0xbe, 0xa2, 0x00, # 0xd7 - 0x00, 0x00, 0xa2, 0x3e, 0xa2, 0x00, # 0xd8 - 0x10, 0x10, 0x10, 0xf0, 0x00, 0x00, # 0xd9 - 0x00, 0x00, 0x00, 0x1f, 0x10, 0x10, # 0xda - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, # 0xdb - 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, # 0xdc - 0x00, 0x00, 0x00, 0xee, 0x00, 0x00, # 0xdd - 0x00, 0x00, 0xa2, 0xbe, 0x22, 0x00, # 0xde - 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, # 0xdf - 0x00, 0x3c, 0x42, 0xc2, 0xbc, 0x00, # 0xe0 - 0x00, 0x7f, 0x52, 0x52, 0x2c, 0x00, # 0xe1 - 0x00, 0x3c, 0xc2, 0xc2, 0xbc, 0x00, # 0xe2 - 0x00, 0xbc, 0xc2, 0x42, 0x3c, 0x00, # 0xe3 - 0x00, 0x4c, 0x92, 0x52, 0x8c, 0x00, # 0xe4 - 0x00, 0x5c, 0xa2, 0x62, 0x9c, 0x00, # 0xe5 - 0x00, 0x3f, 0x04, 0x04, 0x38, 0x00, # 0xe6 - 0x00, 0x7f, 0x55, 0x14, 0x08, 0x00, # 0xe7 - 0x00, 0xff, 0xa5, 0x24, 0x18, 0x00, # 0xe8 - 0x00, 0x3c, 0x02, 0x82, 0xbc, 0x00, # 0xe9 - 0x00, 0x3c, 0x82, 0x82, 0xbc, 0x00, # 0xea - 0x00, 0xbc, 0x82, 0x02, 0x3c, 0x00, # 0xeb - 0x00, 0x39, 0x05, 0x86, 0xbc, 0x00, # 0xec - 0x00, 0x20, 0x10, 0x8e, 0x90, 0x20, # 0xed - 0x00, 0x00, 0x40, 0x40, 0x40, 0x00, # 0xee - 0x00, 0x00, 0xe0, 0xc0, 0x00, 0x00, # 0xef - 0x00, 0x00, 0x10, 0x10, 0x10, 0x00, # 0xf0 - 0x00, 0x00, 0x24, 0x74, 0x24, 0x00, # 0xf1 - 0x00, 0x24, 0x24, 0x24, 0x24, 0x24, # 0xf2 - 0xa0, 0xe8, 0x50, 0x2c, 0x54, 0x1e, # 0xf3 - 0x00, 0x60, 0x90, 0xfe, 0x80, 0xfe, # 0xf4 - 0x00, 0x44, 0xb2, 0xaa, 0x9a, 0x44, # 0xf5 - 0x00, 0x10, 0x10, 0x54, 0x10, 0x10, # 0xf6 - 0x00, 0x00, 0x10, 0x18, 0x18, 0x00, # 0xf7 - 0x00, 0x60, 0x90, 0x90, 0x60, 0x00, # 0xf8 - 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, # 0xf9 - 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, # 0xfa - 0x00, 0x40, 0xf0, 0x00, 0x00, 0x00, # 0xfb - 0x00, 0x90, 0xf0, 0xa0, 0x00, 0x00, # 0xfc - 0x00, 0x90, 0xb0, 0x50, 0x00, 0x00, # 0xfd - 0x00, 0x3c, 0x3c, 0x3c, 0x3c, 0x00, # 0xfe - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 # 0xff -]) - -font4x8 = bytes([ - 0x00, 0x7c, 0x44, 0x7c, # 0 - 0x00, 0x00, 0x7c, 0x00, # 1 - 0x00, 0x5c, 0x54, 0x74, # 2 - 0x00, 0x54, 0x54, 0x7c, # 3 - 0x00, 0x70, 0x10, 0x7c, # 4 - 0x00, 0x74, 0x54, 0x5c, # 5 - 0x00, 0x7c, 0x54, 0x5c, # 6 - 0x00, 0x40, 0x40, 0x7c, # 7 - 0x00, 0x7c, 0x54, 0x7c, # 8 - 0x00, 0x74, 0x54, 0x7c, # 9 - 0x00, 0x3c, 0x50, 0x3c, # A - 0x00, 0x7c, 0x54, 0x38, # B - 0x00, 0x38, 0x44, 0x44, # C - 0x00, 0x7c, 0x44, 0x38, # D - 0x00, 0x7c, 0x54, 0x54, # E - 0x00, 0x7c, 0x50, 0x50, # F - 0x00, 0x10, 0x10, 0x10, # - - 0x00, 0x00, 0x00, 0x00 -]) - -displayBuff = bytearray(16) - -def raw(data): - buffer = bytearray(17) - buffer[0] = HT16K33_DISP_ADDR_PTR - for i in range(16): - buffer[ht16k33_ptr_conv[i] + 1] = data[i] - i2c0.writeto(HT16K33_ADDR, buffer) - -def show(value): - global displayBuff - value = str(value) - value = bytearray(value) - value = value[-2:] # limit only 2-char - displayBuff[0] = 0 - displayBuff[1] = 0 - displayBuff[2] = 0 - displayBuff[3] = 0 - if (len(value) >= 2): - for i in range(6): - displayBuff[i + 4] = font_6_8[((value[0] * 6) + i)] - for i in range(6): - displayBuff[i + 4 + 6] = font_6_8[((value[1] * 6) + i)] - else: - for i in range(6): - displayBuff[i + 4] = 0 - for i in range(6): - displayBuff[i + 4 + 6] = font_6_8[((value[0] * 6) + i)] - raw(displayBuff) - - -def scroll(value, speed=0.06): - value = str(value) - value = bytearray(value) - buffer = bytearray(16 + (len(value) * 6) + 16) - for i in range(16): - buffer[i] = 0 - for x in range(len(value)): - for i in range(6): - buffer[i + 16 + (x * 6)] = font_6_8[((value[x] * 6) + i)] - for i in range(16): - buffer[16 + (len(value) * 6) + i] = 0 - for i in range(16 + (len(value) * 6)): - raw(buffer[i:(i+16)]) - sleep(speed) - clear() - - -def clear(): - global displayBuff - raw(b'\x00' * 16) - displayBuff = bytearray(16) - -def show4x8(value): - global displayBuff - displayBuff = bytearray(16) - value = str(value).upper() - value = bytearray(value) - value = value[:5] - value = value[:(5 if b'.' in value else 4)] - nextIndex = 0 - if (len(value) < (5 if b'.' in value else 4)): # fit to right - nextIndex = nextIndex + (((5 if b'.' in value else 4) - len(value)) * 4) - showDotFlag = False - for x in range(len(value)): - c = value[x] - charIndex = 0 - if c >= ord(b'0') and c <= ord(b'9'): - charIndex = c - ord(b'0') - elif c >= ord(b'a') and c <= ord(b'f'): - charIndex = c - ord(b'a') + 10 - elif c == ord(b'-'): - charIndex = 16 - elif c == ord(b'.'): - showDotFlag = True - continue - else: - nextIndex = nextIndex + 4 - continue - for i in range(4): - displayBuff[nextIndex] = font4x8[((charIndex * 4) + i)] | (0x04 if showDotFlag else 0) - if showDotFlag: - showDotFlag = False - nextIndex = nextIndex + 1 - raw(displayBuff) - -def left(value): - global displayBuff - def getCharIndex(c): - if c >= ord(b'0') and c <= ord(b'9'): - return c - ord(b'0') - elif c >= ord(b'a') and c <= ord(b'f'): - return c - ord(b'a') + 10 - elif c == ord(b'-'): - return 16 - else: - return 17 - value = int(value) - value = str(value).upper() - value = bytearray(value) - value = value[:2] - if len(value) == 1: - charIndex = getCharIndex(value[0]) - displayBuff[0] = 0x00 - displayBuff[1] = 0x00 - displayBuff[2] = 0x00 - displayBuff[3] = font4x8[(charIndex * 4) + 1] - displayBuff[4] = font4x8[(charIndex * 4) + 2] - displayBuff[5] = font4x8[(charIndex * 4) + 3] - displayBuff[6] = 0x00 - displayBuff[7] = 0x00 - elif len(value) == 2: - charIndex = getCharIndex(value[0]) - displayBuff[0] = font4x8[(charIndex * 4) + 1] - displayBuff[1] = font4x8[(charIndex * 4) + 2] - displayBuff[2] = font4x8[(charIndex * 4) + 3] - displayBuff[3] = 0x00 - - charIndex = getCharIndex(value[1]) - displayBuff[4] = font4x8[(charIndex * 4) + 1] - displayBuff[5] = font4x8[(charIndex * 4) + 2] - displayBuff[6] = font4x8[(charIndex * 4) + 3] - displayBuff[7] = 0x00 - raw(displayBuff) - -def right(value): - global displayBuff - def getCharIndex(c): - if c >= ord(b'0') and c <= ord(b'9'): - return c - ord(b'0') - elif c >= ord(b'a') and c <= ord(b'f'): - return c - ord(b'a') + 10 - elif c == ord(b'-'): - return 16 - else: - return 17 - value = int(value) - value = str(value).upper() - value = bytearray(value) - value = value[:2] - if len(value) == 1: - charIndex = getCharIndex(value[0]) - displayBuff[8] = 0x00 - displayBuff[9] = 0x00 - displayBuff[10] = 0x00 - displayBuff[11] = font4x8[(charIndex * 4) + 1] - displayBuff[12] = font4x8[(charIndex * 4) + 2] - displayBuff[13] = font4x8[(charIndex * 4) + 3] - displayBuff[14] = 0x00 - displayBuff[15] = 0x00 - elif len(value) == 2: - charIndex = getCharIndex(value[0]) - displayBuff[8] = 0x00 - displayBuff[9] = font4x8[(charIndex * 4) + 1] - displayBuff[10] = font4x8[(charIndex * 4) + 2] - displayBuff[11] = font4x8[(charIndex * 4) + 3] - - charIndex = getCharIndex(value[1]) - displayBuff[12] = 0x00 - displayBuff[13] = font4x8[(charIndex * 4) + 1] - displayBuff[14] = font4x8[(charIndex * 4) + 2] - displayBuff[15] = font4x8[(charIndex * 4) + 3] - raw(displayBuff) - -def plot(value): - global displayBuff - displayBuff = displayBuff[-15:] + b'\x00' - value = int(value) - if value >= 0 and value <= 7: - displayBuff[15] = 0x01 << int(value) - else: - displayBuff[15] = 0 - raw(displayBuff) - -def dot(x, y, value): - global displayBuff - if value: - displayBuff[x] = displayBuff[x] | (0x80 >> y) - else: - displayBuff[x] = displayBuff[x] & ((0x80 >> y) ^ 0xFF) - raw(displayBuff) - -clear() -i2c0.writeto(HT16K33_ADDR, bytes([ HT16K33_OSC_ON ])) -i2c0.writeto(HT16K33_ADDR, bytes([ HT16K33_DIM_SET_8_16 ])) -i2c0.writeto(HT16K33_ADDR, bytes([ HT16K33_DISP_ON ])) +# register definitions +SET_CONTRAST = const(0x81) +SET_ENTIRE_ON = const(0xA4) +SET_NORM_INV = const(0xA6) +SET_DISP = const(0xAE) +SET_MEM_ADDR = const(0x20) +SET_COL_ADDR = const(0x21) +SET_PAGE_ADDR = const(0x22) +SET_DISP_START_LINE = const(0x40) +SET_SEG_REMAP = const(0xA0) +SET_MUX_RATIO = const(0xA8) +SET_COM_OUT_DIR = const(0xC0) +SET_DISP_OFFSET = const(0xD3) +SET_COM_PIN_CFG = const(0xDA) +SET_DISP_CLK_DIV = const(0xD5) +SET_PRECHARGE = const(0xD9) +SET_VCOM_DESEL = const(0xDB) +SET_CHARGE_PUMP = const(0x8D) + +ADDR = const(0x3C) +pages = 64 // 8 + +i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=100000) +buffer = bytearray(pages * 128) +fbuff = framebuf.FrameBuffer(buffer, 128, 64, framebuf.MONO_VLSB) + +def write_cmd(cmd): + i2c.writeto(ADDR, bytes([ 0x80, cmd ])) + +def write_data(buf): + write_list = [b"\x40", None] # Co=0, D/C#=1 + write_list[1] = buf + i2c.writevto(ADDR, write_list) + +def show(): + write_cmd(SET_COL_ADDR) + write_cmd(0) + write_cmd(128 - 1) + write_cmd(SET_PAGE_ADDR) + write_cmd(0) + write_cmd(pages - 1) + write_data(buffer) + +def poweroff(): + write_cmd(SET_DISP | 0x00) + +def poweron(): + write_cmd(SET_DISP | 0x01) + +def contrast(contrast): + write_cmd(SET_CONTRAST) + write_cmd(contrast) + +def invert(invert): + write_cmd(SET_NORM_INV | (invert & 1)) + +def fill(c): + fbuff.fill(c) + +def pixel(x, y, c): + fbuff.pixel(x, y, c) + +def scroll(dx, dy): + fbuff.scroll(dx, dy) + +def text(string, x, y, c=1): + fbuff.text(string, x, y, c) + +def hline(x, y, w, c): + fbuff.hline(x, y, w, c) + +def vline(x, y, h, c): + fbuff.vline(x, y, h, c) + +def line(x1, y1, x2, y2, c): + fbuff.line(x1, y1, x2, y2, c) + +def rect(x, y, w, h, c): + fbuff.rect(x, y, w, h, c) + +def fill_rect(x, y, w, h, c): + fbuff.fill_rect(x, y, w, h, c) + +def blit(fbuf, x, y): + fbuff.blit(fbuf, x, y) + +def image(imageData, x, y): + buffer = bytearray(imageData[2:]) + fbuff.blit(framebuf.FrameBuffer(buffer, int(imageData[0]), int(imageData[1]), framebuf.MONO_HLSB), x, y) + buffer = None + +setupCMD = ( + SET_DISP | 0x00, # off + # address setting + SET_MEM_ADDR, 0x00, # horizontal + # resolution and layout + SET_DISP_START_LINE | 0x00, + SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0 + SET_MUX_RATIO, 64 - 1, + SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0 + SET_DISP_OFFSET, 0x00, + SET_COM_PIN_CFG, 0x12, + # timing and driving scheme + SET_DISP_CLK_DIV, 0x80, + SET_PRECHARGE, 0xF1, + SET_VCOM_DESEL, 0x30, # 0.83*Vcc + # display + SET_CONTRAST, 0xFF, # maximum + SET_ENTIRE_ON, # output follows RAM contents + SET_NORM_INV, # not inverted + SET_CHARGE_PUMP, 0x14, # charge pump + SET_DISP | 0x01, # on +) + +for cmd in setupCMD: + write_cmd(cmd) +fill(0) +show() diff --git a/ports/esp32/boards/Rapbit32/modules/imu.py b/ports/esp32/boards/Rapbit32/modules/imu.py deleted file mode 100644 index b99901377fe04..0000000000000 --- a/ports/esp32/boards/Rapbit32/modules/imu.py +++ /dev/null @@ -1,352 +0,0 @@ -# Dev by Sonthaya Nongnuch - -from machine import Pin, I2C -import math -import utime -import _thread - -MPU6050_ADDR = const(0x68) -LSM303AGR_ACC_ADDR = const(0x19) -LSM303AGR_MEG_ADDR = const(0x1E) - -MPU6050 = const(0) -LSM303AGR = const(1) - -acc = [0, 0, 0, 0] -gyro = [0, 0, 0, 0] -mag = [0, 0, 0, 0] -temp = 0.0 - -EVENT_SHAKE = const(0) -EVENT_BOARD_UP = const(1) -EVENT_BOARD_DOWN = const(2) -EVENT_SCREEN_UP = const(3) -EVENT_SCREEN_DOWN = const(4) -EVENT_TILT_LEFT = const(5) -EVENT_TILT_RIGHT = const(6) -EVENT_FREE_FALL = const(7) -EVENT_3G = const(8) -EVENT_6G = const(9) -EVENT_8G = const(10) - -__event_callback = [ None ] * 10 - -__startCalcLowStrengthContinue = False -__xStartCalc = 0 - -# Check sensor on board -i2c1 = I2C(1, scl=Pin(5), sda=Pin(4), freq=100000) -devAddrs = i2c1.scan() -__device = -1 -if MPU6050_ADDR in devAddrs: - __device = MPU6050 -elif LSM303AGR_ACC_ADDR in devAddrs: - __device = LSM303AGR -del devAddrs - -if __device == MPU6050: - i2c1.writeto_mem(MPU6050_ADDR, 0x6B, b'\x00') - i2c1.writeto_mem(MPU6050_ADDR, 0x1C, b'\x10') # 8g - i2c1.writeto_mem(MPU6050_ADDR, 0x1B, b'\x00') # 250 */sec -elif __device == LSM303AGR: - i2c1.writeto_mem(LSM303AGR_ACC_ADDR, 0x2E, b'\x00') # FIFO_CTRL_REG_A - i2c1.writeto_mem(LSM303AGR_ACC_ADDR, 0x20, b'\x57') # CTRL_REG1_A - i2c1.writeto_mem(LSM303AGR_ACC_ADDR, 0x23, b'\x20') # Full-scale selection, 8g - - i2c1.writeto_mem(LSM303AGR_MEG_ADDR, 0x60, b'\x8C') # CFG_REG_A_M - i2c1.writeto_mem(LSM303AGR_MEG_ADDR, 0x61, b'\x01') # CFG_REG_B_M - - -def b2i(x, y): - return (x << 8 | y) if not x & 0x80 else (-(((x ^ 255) << 8) | (y ^ 255) + 1)) - -def i2b(n): - if n < 0: - n = (-n ^ 0xFFFF) + 1 - return bytes([ (n >> 8) & 0xFF, n & 0xFF ]) - -def update(): - global acc, gyro, mag, temp - if __device == MPU6050: - buff = i2c1.readfrom_mem(MPU6050_ADDR, 0x3B, 14) - for i in range(3): - acc[i] = b2i(buff[(i * 2)], buff[(i * 2) + 1]) - acc[i] = round(acc[i] / 4096.0 * 1000.0, 2) - x = -acc[1] - y = acc[0] - z = -acc[2] - acc[0] = x - acc[1] = y - acc[2] = z - acc[3] = math.sqrt(math.pow(acc[0], 2) + math.pow(acc[1], 2) + math.pow(acc[2], 2)) - del x, y, z - temp = round(b2i(buff[6], buff[7]) / 340.00 + 36.53, 2) - for i in range(3): - gyro[i] = b2i(buff[(i * 2) + 8], buff[(i * 2) + 9]) - gyro[i] = round(gyro[i] / 131.0, 2) - gyro[3] = math.sqrt(math.pow(gyro[0], 2) + math.pow(gyro[1], 2) + math.pow(gyro[2], 2)) - elif __device == LSM303AGR: - buff = [0, 0, 0, 0, 0, 0] - for i in range(6): - value = i2c1.readfrom_mem(LSM303AGR_ACC_ADDR, 0x28 + i, 1)[0] - buff[i] = value - for i in range(3): - acc[i] = b2i(buff[(i * 2) + 1], buff[(i * 2) + 0]) - acc[i] = round(((acc[i] >> 6) * 15630 + 500) / 1000, 2) - acc[3] = math.sqrt(math.pow(acc[0], 2) + math.pow(acc[1], 2) + math.pow(acc[2], 2)) - acc[0] = acc[0] * -1 - acc[1] = acc[1] * -1 - buff = [0, 0, 0, 0, 0, 0] - for i in range(6): - buff[i] = i2c1.readfrom_mem(LSM303AGR_MEG_ADDR, 0x68 + i, 1)[0] - for i in range(3): - mag[i] = b2i(buff[(i * 2) + 1], buff[(i * 2) + 0]) - mag[i] = round(mag[i] * 1.5 * 0.1, 2) - mag[3] = math.sqrt(math.pow(mag[0], 2) + math.pow(mag[1], 2) + math.pow(mag[2], 2)) - -def rotation(): - x_g_value = acc[0] / 1000.0 # Acceleration in x-direction in g units - y_g_value = acc[1] / 1000.0 # Acceleration in y-direction in g units - z_g_value = -acc[2] / 1000.0 # Acceleration in z-direction in g units - - roll = (((math.atan2(z_g_value, x_g_value) * 180) / 3.14) + 180) - if roll >= 270: - roll = 270 - roll - elif roll >= 90: - roll = math.fmod(90 - roll, -180) + 180 - else: - roll = -90 - roll - roll = round(roll, 2) - - pitch = (((math.atan2(y_g_value, z_g_value) * 180) / 3.14) + 180) - pitch = 180 - pitch - pitch = round(pitch, 2) - - return (roll, pitch) - -calibrateMagFlag = False -mag_min = [ 99999, 99999, 99999 ] -mag_max = [ -99999, -99999, -99999 ] -def heading(): - global calibrateMagFlag - if not calibrateMagFlag: - if not loadCalibrateFromSRAM(): - calibrate_compass() - calibrateMagFlag = True - - try: - # use calibration values to shift and scale magnetometer measurements - x_mag = (0.0 + mag[0] - mag_min[0]) / (mag_max[0] - mag_min[0]) * 2 - 1 - y_mag = (0.0 + mag[1] - mag_min[1]) / (mag_max[1] - mag_min[1]) * 2 - 1 - z_mag = (0.0 + mag[2] - mag_min[2]) / (mag_max[2] - mag_min[2]) * 2 - 1 - - # Normalize acceleration measurements so they range from 0 to 1 - s = math.sqrt(math.pow(acc[0], 2) + math.pow(acc[1], 2) + math.pow(acc[2], 2)) - xAccelNorm = acc[0] / s - yAccelNorm = acc[1] / s - # DF("Acc norm (x, y): (%f, %f)\n", xAccelNorm, yAccelNorm) - - pitch = math.asin(-xAccelNorm) - roll = math.asin(yAccelNorm / math.cos(pitch)) - - # tilt compensated magnetic sensor measurements - x_mag_comp = x_mag * math.cos(pitch) + z_mag * math.sin(pitch) - y_mag_comp = x_mag * math.sin(roll) * math.sin(pitch) + y_mag * math.cos(roll) - z_mag * math.sin(roll) * math.cos(pitch) - - # arctangent of y/x converted to degrees - heading = 180 * math.atan2(x_mag_comp, y_mag_comp) / math.pi - - heading = (-heading) if heading <= 0 else (360 - heading) - return int(heading) - except: - return 0 - - -def calibrate_compass(): - global calibrateMagFlag, mag_min, mag_max - import display - from time import sleep - - mag_min = [ 99999, 99999, 99999 ] - mag_max = [ -99999, -99999, -99999 ] - - display.scroll("TILT TO FILL SCREEN") - - buffer = bytearray(16) - - before_index_cols = -1 - before_index_rows = -1 - - delay = 0 - pixelShow = False - while True: - if mag[0] < mag_min[0]: - mag_min[0] = mag[0] - if mag[0] > mag_max[0]: - mag_max[0] = mag[0] - - if mag[1] < mag_min[1]: - mag_min[1] = mag[1] - if mag[1] > mag_max[1]: - mag_max[1] = mag[1] - - if mag[2] < mag_min[2]: - mag_min[2] = mag[2] - if mag[2] > mag_max[2]: - mag_max[2] = mag[2] - - (roll, pitch) = rotation() - - index_cols = int((roll - -60) * (15 - 0) / (60 - -60) + 0) - index_rows = int((pitch - -60) * (7 - 0) / (60 - -60) + 0) - - if index_cols > 15: - index_cols = 15 - if index_rows > 7: - index_rows = 7 - - if before_index_cols == -1 or before_index_rows == -1: - before_index_cols = index_cols - before_index_rows = index_rows - - if index_cols >= 0 and index_cols <= 15 and index_rows >= 0 and index_rows <= 7: - if (index_cols != before_index_cols) or (index_rows != before_index_rows): - buffer[before_index_cols] |= 0x80 >> before_index_rows - - if pixelShow: - buffer[index_cols] |= 0x80 >> index_rows - if buffer == b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF': - break - else: - buffer[index_cols] &= ~(0x80 >> index_rows) - if delay >= 200: - delay = 0 - pixelShow = not pixelShow - - before_index_cols = index_cols - before_index_rows = index_rows - - display.raw(buffer) - - sleep(0.05) - delay += 50 - - display.scroll("FINISH") - saveCalibrateIntoSRAM() - calibrateMagFlag = True - -def loadCalibrateFromSRAM(): - buff = i2c1.readfrom_mem(0x6F, 0x20 + 0, 13) - crc = 0 - for i in range(12): - crc = crc + buff[i] - crc = crc & 0xFF - - if crc != buff[12]: - return False - - for i in range(3): - mag_min[i] = b2i(buff[(i * 2) + 0], buff[(i * 2) + 1]) / 100.0 - for i in range(3): - mag_max[i] = b2i(buff[(i * 2) + 0 + 6], buff[(i * 2) + 1 + 6]) / 100.0 - - return True - -def saveCalibrateIntoSRAM(): - buff = bytearray(13) - for i in range(3): - a = i2b(int(mag_min[i] * 100)) - buff[(i * 2) + 0] = a[0] - buff[(i * 2) + 1] = a[1] - for i in range(3): - a = i2b(int(mag_max[i] * 100)) - buff[(i * 2) + 0 + 6] = a[0] - buff[(i * 2) + 1 + 6] = a[1] - crc = 0 - for i in range(12): - crc = crc + buff[i] - buff[12] = crc & 0xFF - - i2c1.writeto_mem(0x6F, 0x20 + 0, buff) - return True - -def is_gesture(event, blocking=True): - global __xStartCalc, __startCalcLowStrengthContinue - if event == EVENT_SHAKE: - return acc[3] > 4000 - elif event == EVENT_BOARD_UP: - return acc[1] < -600 - elif event == EVENT_BOARD_DOWN: - return acc[1] > 600 - elif event == EVENT_SCREEN_UP: - pitch = rotation()[1] - return pitch >= -30 and pitch <= 30 - elif event == EVENT_SCREEN_DOWN: - pitch = rotation()[1] - return pitch >= 150 or pitch <= -150 - elif event == EVENT_TILT_LEFT: - roll = rotation()[0] - return roll <= -30 - elif event == EVENT_TILT_RIGHT: - roll = rotation()[0] - return roll >= 30 - elif event == EVENT_FREE_FALL: - if blocking: - lowStrengthContinue = False - for i in range(0, 240, 40): - if acc[3] < 500: - lowStrengthContinue = True - utime.sleep_ms(40) - else: - lowStrengthContinue = False - break - return lowStrengthContinue - else: - if acc[3] < 500: - if not __startCalcLowStrengthContinue: - __xStartCalc = utime.ticks_ms() - __startCalcLowStrengthContinue = True - else: - if (utime.ticks_ms() - __xStartCalc) >= 220E3: - __startCalcLowStrengthContinue = False - return True - else: - __xStartCalc = 0 - __startCalcLowStrengthContinue = False - return False - elif event == EVENT_3G: - return acc[3] > 3000 - - elif event == EVENT_6G: - return acc[3] > 6000 - - elif event == EVENT_8G: - return acc[3] > 8000 - else: - return False - -eventCallback = [] -callbackDoingFlag = [ False ] * 11 -for i in range(11): - eventCallback += [ [] ] - -def on(type, callback): - global eventCallback - eventCallback[type] += [ callback ] - -def IMULoopTask(): - global callbackDoingFlag - while True: - update() - for inx in range(11): - if is_gesture(inx, False): # non-blocking - if callbackDoingFlag[inx] == False: - for i in range(len(eventCallback[inx])): - _thread.start_new_thread(eventCallback[inx][i], ()) - callbackDoingFlag[inx] = True - else: - callbackDoingFlag[inx] = False - - utime.sleep_ms(20) - -_thread.start_new_thread(IMULoopTask, ()) diff --git a/ports/esp32/boards/Rapbit32/modules/rtc.py b/ports/esp32/boards/Rapbit32/modules/rtc.py deleted file mode 100644 index 320b6ad32488b..0000000000000 --- a/ports/esp32/boards/Rapbit32/modules/rtc.py +++ /dev/null @@ -1,48 +0,0 @@ -# Dev by Sonthaya Nongnuch - -from machine import Pin, I2C - -MCP79411_ADDR = const(0x6F) - -def datetime(value=()): - i2c1 = I2C(1, scl=Pin(5), sda=Pin(4), freq=100000) - - def bcd2dec(x): - return int(((x >> 4) * 10) + (x & 0x0F)) - - def dec2bcd(x): - return (int(x / 10) << 4) | (x % 10) - - if len(value) == 0: # Read - data = i2c1.readfrom_mem(MCP79411_ADDR, 0x00, 7) - year = bcd2dec(data[6]) + 2000 - month = bcd2dec(data[5] & 0x1F) - day = bcd2dec(data[4] & 0x3F) - hour = bcd2dec(data[2] & 0x3F) - minute = bcd2dec(data[1] & 0x7F) - second = bcd2dec(data[0] & 0x7F) - microsecond = 0 - tzinfo = 0 - return (year, month, day, hour, minute, second, microsecond, tzinfo) - else: # Write - data = bytearray(7) - - year = value[0] if len(value) >= 1 else 0 - month = value[1] if len(value) >= 2 else 1 - day = value[2] if len(value) >= 3 else 1 - hour = value[3] if len(value) >= 4 else 0 - minute = value[4] if len(value) >= 5 else 0 - second = value[5] if len(value) >= 6 else 0 - microsecond = value[6] if len(value) >= 7 else 0 - tzinfo = value[7] if len(value) >= 8 else 0 - - data[0] = (dec2bcd(second) & 0x7F) | 0x80 - data[1] = dec2bcd(minute) & 0x7F - data[2] = dec2bcd(hour) & 0x3F - data[3] = 0x01 - data[4] = dec2bcd(day) & 0x3F - data[5] = dec2bcd(month) & 0x1F - data[6] = dec2bcd(year - 2000) & 0xFF - - i2c1.writeto_mem(MCP79411_ADDR, 0x00, data) - return True diff --git a/ports/esp32/boards/Rapbit32/modules/sensor.py b/ports/esp32/boards/Rapbit32/modules/sensor.py index d7d0aa2eea91f..c7a22bba34a0f 100644 --- a/ports/esp32/boards/Rapbit32/modules/sensor.py +++ b/ports/esp32/boards/Rapbit32/modules/sensor.py @@ -1,24 +1,93 @@ # Dev by Sonthaya Nongnuch -from machine import Pin, I2C, ADC +from machine import Pin, ADC -LM73_ADDR = const(0x4D) -MAX_VALUE = const(800) -MIN_VALUE = const(200) +def light1_raw(): + return analogRead(39) -def light(): - adc = ADC(Pin(36)) +def light2_raw(): + return analogRead(35) + +def light3_raw(): + return analogRead(33) + +__white_threshold = 3000 +__black_threshold = 1500 +def white_threshold(threshold=None): + if threshold != None: + __white_threshold = threshold + return __white_threshold + +def black_threshold(threshold=None): + global __white_threshold + if threshold != None: + __black_threshold = threshold + return __black_threshold + +def light1_is_black(): + return True if light1_raw() <= __black_threshold else False + +def light2_is_black(): + return True if light2_raw() <= __black_threshold else False + +def light3_is_black(): + return True if light3_raw() <= __black_threshold else False + +def light1_is_white(): + return True if light1_raw() >= __white_threshold else False + +def light2_is_white(): + return True if light2_raw() >= __white_threshold else False + +def light3_is_white(): + return True if light3_raw() >= __white_threshold else False + +def ldr1_raw(): + return analogRead(34) + +def ldr2_raw(): + return analogRead(32) + +def ldr1(): + return min(max(int(((ldr1_raw() - 300) / (3000 - 300)) * 100), 0), 100) + +def ldr2(): + return min(max(int(((ldr2_raw() - 300) / (3000 - 300)) * 100), 0), 100) + +def knob_raw(): + return analogRead(36) + +def knob(): + return int(knob_raw() / 4095 * 100) + +def knob_volt(): + return round(knob_raw() / 4095 * 3.3, 2) + +def analogRead(pin_n): + adc = ADC(Pin(pin_n)) adc.atten(ADC.ATTN_11DB) adc.width(ADC.WIDTH_12BIT) - value = int(100 - ((adc.read() - MIN_VALUE) / (MAX_VALUE - MIN_VALUE) * 100)) - value = 100 if value > 100 else value - value = 0 if value < 0 else value - return value - -def temperature(): - i2c1 = I2C(1, scl=Pin(5), sda=Pin(4), freq=100000) - data = i2c1.readfrom_mem(LM73_ADDR, 0x00, 2) - temp = (((data[0] & 0x7F) << 8)| data[1]) >> 5 - temp = temp * 0.25 - temp = temp * (-1 if (data[0] & 0x80) != 0 else 1) - return temp + return adc.read() + +def sw1(): + return 1 - Pin(0, Pin.IN).value() + +def sw1_is_press(): + return False if Pin(0, Pin.IN).value() else True + +def sw1_is_release(): + return True if Pin(0, Pin.IN).value() else False + +""" +# Test only +from time import sleep + +while True: + # print("{}\t{}\t{}".format(light1(), light2(), light3())) + # print("{}\t{}".format(ldr1(), ldr2())) + # print("{}".format(knob())) + # print("{}".format(sw1())) + # print("{}\t{}".format(ldr1(), ldr2())) + print("{}\t{}".format(knob_volt(), knob_raw())) + sleep(0.1) +""" \ No newline at end of file diff --git a/ports/esp32/boards/Rapbit32/modules/servo.py b/ports/esp32/boards/Rapbit32/modules/servo.py index 44bd1571996a1..f1dd418a2bc7d 100644 --- a/ports/esp32/boards/Rapbit32/modules/servo.py +++ b/ports/esp32/boards/Rapbit32/modules/servo.py @@ -2,8 +2,20 @@ from machine import Pin, PWM -SV1 = Pin(15) -SV2 = Pin(17) +SV1 = Pin(2) +SV2 = Pin(15) + +SV1_obj = PWM(SV1, freq=50) +SV1_obj.duty(0) + +SV2_obj = PWM(SV2, freq=50) +SV2_obj.duty(0) def angle(pin, angle): - PWM(pin, freq=50).duty(int(25.57 + ((angle / 180.0) * 102.3))) + if pin == SV1: + SV1_obj.duty(int(25.57 + ((angle / 180.0) * 102.3))) + elif pin == SV2: + SV2_obj.duty(int(25.57 + ((angle / 180.0) * 102.3))) + else: + PWM(pin, freq=50).duty(int(25.57 + ((angle / 180.0) * 102.3))) + diff --git a/ports/esp32/boards/Rapbit32/modules/switch.py b/ports/esp32/boards/Rapbit32/modules/switch.py index 92ffd801f845f..fc9895f16e827 100644 --- a/ports/esp32/boards/Rapbit32/modules/switch.py +++ b/ports/esp32/boards/Rapbit32/modules/switch.py @@ -5,52 +5,53 @@ import utime S1 = Pin(16, Pin.IN, Pin.PULL_UP) -S2 = Pin(14, Pin.IN, Pin.PULL_UP) +# S2 = Pin(14, Pin.IN, Pin.PULL_UP) __s1_press = None __s1_release = None -__s2_press = None -__s2_release = None +# __s2_press = None +# __s2_release = None def __onSwitchChangesValue(pin): if pin.value(): callback = None if pin == S1: callback = __s1_release - elif pin == S2: - callback = __s2_release + # elif pin == S2: + # callback = __s2_release if callback: _thread.start_new_thread(callback, ()) else: callback = None if pin == S1: callback = __s1_press - elif pin == S2: - callback = __s2_press + # elif pin == S2: + # callback = __s2_press if callback: _thread.start_new_thread(callback, ()) S1.irq(handler=__onSwitchChangesValue, trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING) -S2.irq(handler=__onSwitchChangesValue, trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING) +# S2.irq(handler=__onSwitchChangesValue, trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING) def value(pin): return 0 if pin.value() else 1 def press(pin, callback): - global __s1_press, __s2_press + global __s1_press #, __s2_press if pin == S1: __s1_press = callback - elif pin == S2: - __s2_press = callback + # elif pin == S2: + # __s2_press = callback def release(pin, callback): - global __s1_release, __s2_release + global __s1_release #, __s2_release if pin == S1: __s1_release = callback - elif pin == S2: - __s2_release = callback + # elif pin == S2: + # __s2_release = callback +""" __s1_pressed = None __s2_pressed = None __s12_pressed = None @@ -114,3 +115,4 @@ def pressed(pin, callback): __s1_pressed = callback elif pin == S2: __s2_pressed = callback +""" diff --git a/ports/esp32/boards/Rapbit32/modules/ultrasonic.py b/ports/esp32/boards/Rapbit32/modules/ultrasonic.py new file mode 100644 index 0000000000000..7f4c070b593be --- /dev/null +++ b/ports/esp32/boards/Rapbit32/modules/ultrasonic.py @@ -0,0 +1,20 @@ +from machine import Pin +from machine import time_pulse_us +from time import sleep_us + +trigger = Pin(trig_pin, mode=Pin.OUT) +echo = Pin(echo_pin, mode=Pin.IN) + +def read(): + trigger.value(0) + sleep_us(5) + trigger.value(1) + sleep_us(10) + trigger.value(0) + + try: + pulse_time = time_pulse_us(echo, 1, 1000000) + d = (pulse_time / 2) / 29.1 + return int(d) if d < 400 else -1 + except OSError as ex: + return -1 diff --git a/ports/esp32/boards/Rapbit32/modules/usb.py b/ports/esp32/boards/Rapbit32/modules/usb.py deleted file mode 100644 index 3809f0ebc1c1a..0000000000000 --- a/ports/esp32/boards/Rapbit32/modules/usb.py +++ /dev/null @@ -1,21 +0,0 @@ -# Dev by Sonthaya Nongnuch - -from machine import Pin - -last_value = 0 - -def value(x): - global last_value - Pin(25, Pin.OUT).value(0 if x else 1) - last_value = x - -def on(): - value(1) - -def off(): - value(0) - -def toggle(): - global last_value - last_value = 0 if last_value else 1 - value(last_value) diff --git a/ports/esp32/boards/Rapbit32/mpconfigboard.h b/ports/esp32/boards/Rapbit32/mpconfigboard.h index 67d1bc35dce2e..7c605be17451b 100644 --- a/ports/esp32/boards/Rapbit32/mpconfigboard.h +++ b/ports/esp32/boards/Rapbit32/mpconfigboard.h @@ -1,2 +1,3 @@ #define MICROPY_HW_BOARD_NAME "Rapbit32" #define MICROPY_HW_MCU_NAME "ESP32" +#define MICROPY_HW_BOARD_RAPBIT32 1 \ No newline at end of file From 1e7931ddf5bcbe16c8023d1b48f2dc32116f73d8 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 23 May 2021 18:31:24 +0700 Subject: [PATCH 3/4] Add motor function --- .../boards/Rapbit32/modules/_board_setup.py | 11 +- ports/esp32/boards/Rapbit32/modules/motor.py | 85 +++++++++++++ ports/esp32/boards/Rapbit32/modules/sensor.py | 27 ++-- ports/esp32/boards/Rapbit32/modules/switch.py | 120 +++--------------- .../boards/Rapbit32/modules/ultrasonic.py | 20 --- ports/esp32/boards/Rapbit32/mpconfigboard.h | 2 +- 6 files changed, 125 insertions(+), 140 deletions(-) create mode 100644 ports/esp32/boards/Rapbit32/modules/motor.py delete mode 100644 ports/esp32/boards/Rapbit32/modules/ultrasonic.py diff --git a/ports/esp32/boards/Rapbit32/modules/_board_setup.py b/ports/esp32/boards/Rapbit32/modules/_board_setup.py index 9337457072022..c24ca81287e54 100644 --- a/ports/esp32/boards/Rapbit32/modules/_board_setup.py +++ b/ports/esp32/boards/Rapbit32/modules/_board_setup.py @@ -2,13 +2,10 @@ from machine import Pin import display import buzzer -import usb -Pin(17, Pin.OUT).value(1) -Pin(2, Pin.OUT).value(1) -Pin(15, Pin.OUT).value(1) -Pin(12, Pin.OUT).value(1) -display.clear() +Pin(25, Pin.OUT).value(0) # LED2 +Pin(23, Pin.OUT).value(0) # LDE1 +display.fill(0) +display.show() buzzer.off() -usb.off() """ diff --git a/ports/esp32/boards/Rapbit32/modules/motor.py b/ports/esp32/boards/Rapbit32/modules/motor.py new file mode 100644 index 0000000000000..d09870d8c2cd6 --- /dev/null +++ b/ports/esp32/boards/Rapbit32/modules/motor.py @@ -0,0 +1,85 @@ +# Dev by Sonthaya Nongnuch + +from machine import Pin, PWM +from time import sleep +from micropython import const + +FORWARD = const(1) +BACKWARD = const(2) +TURN_LEFT = const(3) +TURN_RIGHT = const(4) + +__m1_a = PWM(Pin(5), freq=1000, duty=1023) +__m1_b = PWM(Pin(4), freq=1000, duty=1023) +__m2_a = PWM(Pin(18), freq=1000, duty=1023) +__m2_b = PWM(Pin(19), freq=1000, duty=1023) + +def wheel(speed_left, speed_right): + dir1 = 0 if speed_left >= 0 else 1 + if speed_left < 0: + speed_left = speed_left * -1 + speed_left = 1023 - min(max(int(speed_left / 100 * 1023), 0), 1023) + __m1_a.duty(1023 if dir1 == 1 else speed_left) + __m1_b.duty(speed_left if dir1 == 1 else 1023) + + dir2 = 1 if speed_right >= 0 else 0 + if speed_right < 0: + speed_right = speed_right * -1 + speed_right = 1023 - min(max(int(speed_right / 100 * 1023), 0), 1023) + __m2_a.duty(1023 if dir2 == 1 else speed_right) + __m2_b.duty(speed_right if dir2 == 1 else 1023) + +def forward(speed=50, time=1): + wheel(speed, speed) + sleep(time) + wheel(0, 0) + +def backward(speed=50, time=1): + wheel(speed * -1, speed * -1) + sleep(time) + wheel(0, 0) + +def turn_left(speed=50, time=1): + wheel(0, speed) + sleep(time) + wheel(0, 0) + +def turn_right(speed=50, time=1): + wheel(speed, 0) + sleep(time) + wheel(0, 0) + +def move(dir, speed): + if dir == FORWARD: + wheel(speed, speed) + elif dir == BACKWARD: + wheel(speed * -1, speed * -1) + elif dir == TURN_LEFT: + wheel(0, speed) + elif dir == TURN_RIGHT: + wheel(speed, 0) + +def stop(): + wheel(0, 0) + +# Fixed bug +sleep(0.1) + +# Test only +""" +sleep(2) +# forward(50, 2) +sleep(2) +# backward(50, 2) +sleep(2) +# turn_left(50, 2) +sleep(2) +turn_right(50, 2) +sleep(2) + +while True: + move(TURN_RIGHT, 50) + sleep(1) + stop() + sleep(1) +""" diff --git a/ports/esp32/boards/Rapbit32/modules/sensor.py b/ports/esp32/boards/Rapbit32/modules/sensor.py index c7a22bba34a0f..b1d37f1f59827 100644 --- a/ports/esp32/boards/Rapbit32/modules/sensor.py +++ b/ports/esp32/boards/Rapbit32/modules/sensor.py @@ -1,6 +1,7 @@ # Dev by Sonthaya Nongnuch -from machine import Pin, ADC +from machine import Pin, ADC, time_pulse_us +from time import sleep_us def light1_raw(): return analogRead(39) @@ -12,7 +13,7 @@ def light3_raw(): return analogRead(33) __white_threshold = 3000 -__black_threshold = 1500 +__black_threshold = 2000 def white_threshold(threshold=None): if threshold != None: __white_threshold = threshold @@ -69,14 +70,24 @@ def analogRead(pin_n): adc.width(ADC.WIDTH_12BIT) return adc.read() -def sw1(): - return 1 - Pin(0, Pin.IN).value() -def sw1_is_press(): - return False if Pin(0, Pin.IN).value() else True +trigger = Pin(26, mode=Pin.OUT) +echo = Pin(27, mode=Pin.IN) + +def distance(): + trigger.value(0) + sleep_us(5) + trigger.value(1) + sleep_us(10) + trigger.value(0) + + try: + pulse_time = time_pulse_us(echo, 1, 1000000) + d = (pulse_time / 2) / 29.1 + return int(d) if d < 400 else -1 + except OSError as ex: + return -1 -def sw1_is_release(): - return True if Pin(0, Pin.IN).value() else False """ # Test only diff --git a/ports/esp32/boards/Rapbit32/modules/switch.py b/ports/esp32/boards/Rapbit32/modules/switch.py index fc9895f16e827..b15ea2be193f9 100644 --- a/ports/esp32/boards/Rapbit32/modules/switch.py +++ b/ports/esp32/boards/Rapbit32/modules/switch.py @@ -1,118 +1,30 @@ # Dev by Sonthaya Nongnuch from machine import Pin -import _thread -import utime -S1 = Pin(16, Pin.IN, Pin.PULL_UP) -# S2 = Pin(14, Pin.IN, Pin.PULL_UP) +SW1 = Pin(0, Pin.IN, Pin.PULL_UP) -__s1_press = None -__s1_release = None -# __s2_press = None -# __s2_release = None +__press = None +__release = None def __onSwitchChangesValue(pin): if pin.value(): - callback = None - if pin == S1: - callback = __s1_release - # elif pin == S2: - # callback = __s2_release - if callback: - _thread.start_new_thread(callback, ()) + if __release: + __release() else: - callback = None - if pin == S1: - callback = __s1_press - # elif pin == S2: - # callback = __s2_press - if callback: - _thread.start_new_thread(callback, ()) + if __press: + __press() -S1.irq(handler=__onSwitchChangesValue, trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING) -# S2.irq(handler=__onSwitchChangesValue, trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING) +SW1.irq(handler=__onSwitchChangesValue, trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING) -def value(pin): - return 0 if pin.value() else 1 +def value(): + return 0 if SW1.value() else 1 -def press(pin, callback): - global __s1_press #, __s2_press - if pin == S1: - __s1_press = callback - # elif pin == S2: - # __s2_press = callback +def press(callback): + global __press + __press = callback -def release(pin, callback): - global __s1_release #, __s2_release - if pin == S1: - __s1_release = callback - # elif pin == S2: - # __s2_release = callback - -""" -__s1_pressed = None -__s2_pressed = None -__s12_pressed = None - -S12 = 99 - -def SwitchLoopTask(): - sw1_press_start = None - sw1_press_flag = False - sw2_press_start = None - sw2_press_flag = False - while True: - sw1 = S1.value() - sw2 = S2.value() - - # S1 - if sw1 == 0: # S1 is press - if sw1_press_start == None: - sw1_press_start = utime.ticks_ms() - else: - if sw1_press_start != None: - diff = utime.ticks_ms() - sw1_press_start - sw1_press_flag = diff >= 40 and diff < 1000 - sw1_press_start = None - - # S2 - if sw2 == 0: # S2 is press - if sw2_press_start == None: - sw2_press_start = utime.ticks_ms() - else: - if sw2_press_start != None: - diff = utime.ticks_ms() - sw2_press_start - sw2_press_flag = diff >= 40 and diff < 1000 - sw2_press_start = None - - if sw1 == sw2 == 1: - if sw1_press_flag and sw2_press_flag: - if __s12_pressed: - _thread.start_new_thread(__s12_pressed, ()) - sw1_press_flag = False - sw2_press_flag = False - elif sw1_press_flag: - if __s1_pressed: - _thread.start_new_thread(__s1_pressed, ()) - sw1_press_flag = False - elif sw2_press_flag: - if __s2_pressed: - _thread.start_new_thread(__s2_pressed, ()) - sw2_press_flag = False - - utime.sleep_ms(20) - - -_thread.start_new_thread(SwitchLoopTask, ()) - -def pressed(pin, callback): - global __s1_pressed, __s2_pressed, __s12_pressed - if pin == S12: - __s12_pressed = callback - elif pin == S1: - __s1_pressed = callback - elif pin == S2: - __s2_pressed = callback -""" +def release(callback): + global __release + __release = callback diff --git a/ports/esp32/boards/Rapbit32/modules/ultrasonic.py b/ports/esp32/boards/Rapbit32/modules/ultrasonic.py deleted file mode 100644 index 7f4c070b593be..0000000000000 --- a/ports/esp32/boards/Rapbit32/modules/ultrasonic.py +++ /dev/null @@ -1,20 +0,0 @@ -from machine import Pin -from machine import time_pulse_us -from time import sleep_us - -trigger = Pin(trig_pin, mode=Pin.OUT) -echo = Pin(echo_pin, mode=Pin.IN) - -def read(): - trigger.value(0) - sleep_us(5) - trigger.value(1) - sleep_us(10) - trigger.value(0) - - try: - pulse_time = time_pulse_us(echo, 1, 1000000) - d = (pulse_time / 2) / 29.1 - return int(d) if d < 400 else -1 - except OSError as ex: - return -1 diff --git a/ports/esp32/boards/Rapbit32/mpconfigboard.h b/ports/esp32/boards/Rapbit32/mpconfigboard.h index 7c605be17451b..3d04c479880df 100644 --- a/ports/esp32/boards/Rapbit32/mpconfigboard.h +++ b/ports/esp32/boards/Rapbit32/mpconfigboard.h @@ -1,3 +1,3 @@ -#define MICROPY_HW_BOARD_NAME "Rapbit32" +#define MICROPY_HW_BOARD_NAME "Rapbit32(XA)" #define MICROPY_HW_MCU_NAME "ESP32" #define MICROPY_HW_BOARD_RAPBIT32 1 \ No newline at end of file From 0012a97c3117343e3eb6948862f86436f2a418eb Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 7 Aug 2021 15:41:02 +0700 Subject: [PATCH 4/4] Update OLED i2c address and add IR support --- .../esp32/boards/Rapbit32/modules/display.py | 2 +- ports/esp32/boards/Rapbit32/modules/ir.py | 57 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 ports/esp32/boards/Rapbit32/modules/ir.py diff --git a/ports/esp32/boards/Rapbit32/modules/display.py b/ports/esp32/boards/Rapbit32/modules/display.py index c88bb44f0ac79..35be1049a2afa 100644 --- a/ports/esp32/boards/Rapbit32/modules/display.py +++ b/ports/esp32/boards/Rapbit32/modules/display.py @@ -21,7 +21,7 @@ SET_VCOM_DESEL = const(0xDB) SET_CHARGE_PUMP = const(0x8D) -ADDR = const(0x3C) +ADDR = const(0x3D) pages = 64 // 8 i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=100000) diff --git a/ports/esp32/boards/Rapbit32/modules/ir.py b/ports/esp32/boards/Rapbit32/modules/ir.py new file mode 100644 index 0000000000000..de8d548d62417 --- /dev/null +++ b/ports/esp32/boards/Rapbit32/modules/ir.py @@ -0,0 +1,57 @@ +from machine import Pin +from time import ticks_us +from time import ticks_diff + +IR_PIN = 14 + +timeStart = -1 +data = 0 +dataBitIndex = 0 +irQueue = [ ] +pin = None +pinIR = None +lastData = 0 + +def onIRPinChange(p): + global timeStart, data, dataBitIndex, irQueue, lastData + if p.value() == 1: # 1 + timeStart = ticks_us() + else: # 0 + t = ticks_diff(ticks_us(), timeStart) + # print(t) + if t > 4000: # start signal + data = 0 + dataBitIndex = 0 + elif t > 2000 and t < 4000 and dataBitIndex == 0: + # print("RE") + irQueue.append(lastData) + else: + if dataBitIndex < 32: + data |= (1 if t > 1000 else 0) << (dataBitIndex) + dataBitIndex = dataBitIndex + 1 + if dataBitIndex == 32: + addr = data & 0xFF + iaddr = (data >> 8) & 0xFF + cmd = (data >> 16) & 0xFF + icmd = (data >> 24) & 0xFF + # print(hex(data)) + if addr == (iaddr ^ 0xFF) and cmd == (icmd ^ 0xFF): + # print("OK") + irQueue.append(cmd) + lastData = cmd + else: + print("ERROR") + lastData = 0 + dataBitIndex = -1 + +pinIR = Pin(IR_PIN, Pin.IN, Pin.PULL_UP) +pinIR.irq(onIRPinChange, Pin.IRQ_FALLING|Pin.IRQ_RISING) + +def read(): + global irQueue + if len(irQueue): + data = irQueue[0] + irQueue = irQueue[1:] + return data + else: + return 0