diff --git a/modules/base.ts b/bs-utils.ts similarity index 100% rename from modules/base.ts rename to bs-utils.ts diff --git a/microcontroller/ports/esp32/CMakeLists.txt b/microcontroller/ports/esp32/CMakeLists.txt index 1873fc1..2103587 100644 --- a/microcontroller/ports/esp32/CMakeLists.txt +++ b/microcontroller/ports/esp32/CMakeLists.txt @@ -4,7 +4,7 @@ # CMakeLists in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) -set(EXTRA_COMPONENT_DIRS "../../core" "../../../modules/esp32/c") +set(EXTRA_COMPONENT_DIRS "../../core" "../../../modules/std" "../../../modules/gpio") include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(bluescript) diff --git a/modules/esp32/bluescript/button.ts b/modules/esp32/bluescript/button.ts deleted file mode 100644 index 314ca56..0000000 --- a/modules/esp32/bluescript/button.ts +++ /dev/null @@ -1,4 +0,0 @@ -import type { integer, code } from "../../base"; - - -export const buttonOnPressed = (buttonPin: integer, callback: () => void) => {} \ No newline at end of file diff --git a/modules/esp32/bluescript/display.ts b/modules/esp32/bluescript/display.ts deleted file mode 100644 index 4fde96b..0000000 --- a/modules/esp32/bluescript/display.ts +++ /dev/null @@ -1,40 +0,0 @@ -import type { integer, code } from "../../base"; - -class Display { - ICON_HEART: integer; - ICON_SMALL_HEART: integer; - ICON_HAPPY_FACE: integer; - ICON_SAD_FACE: integer; - - constructor() { - this.ICON_HEART = 0; - this.ICON_SMALL_HEART = 1; - this.ICON_HAPPY_FACE = 2; - this.ICON_SAD_FACE = 3; - this.reset(); - } - - public reset() { - - } - - public fill(color: integer) { - - } - - public showIcon(icon: integer, color: integer, background: integer) { - - } - - public showString(str: string, color: integer, background: integer) { - - } - - public showInt(int: integer, color: integer, background: integer) { - - } - - public color(r: integer, g: integer, b: integer):integer { - return 2; - } -} \ No newline at end of file diff --git a/modules/esp32/bluescript/gpio.ts b/modules/esp32/bluescript/gpio.ts deleted file mode 100644 index d350ae4..0000000 --- a/modules/esp32/bluescript/gpio.ts +++ /dev/null @@ -1,13 +0,0 @@ -import type { integer, code } from "../../base"; - -class GPIO { - pinNum:integer; - - constructor(pinNum: integer) { - this.pinNum = pinNum; - } - - set(level: integer) { - - } -} \ No newline at end of file diff --git a/modules/esp32/bluescript/timer.ts b/modules/esp32/bluescript/timer.ts deleted file mode 100644 index 90cd2f6..0000000 --- a/modules/esp32/bluescript/timer.ts +++ /dev/null @@ -1,21 +0,0 @@ -import type { integer, code } from "../../base"; - - -export const setInterval = (func:()=>void, delay:integer):integer => { - return 2; -} - - -export const setTimeout = (func:()=>void, delay:integer):integer => { - return 2; -} - - -export const clearInterval = (timerId:integer):void => {} - - -export const clearTimeout = (timerId:integer):void => {} - -export const getTimeUs = ():integer => { - return 2; -} diff --git a/modules/esp32/c/button.c b/modules/esp32/c/button.c deleted file mode 100644 index 2a6e56e..0000000 --- a/modules/esp32/c/button.c +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include "driver/gpio.h" -#include "include/button.h" -#include "event.h" - - -#define ESP_INTR_FLAG_DEFAULT 0 - - -static bool is_isr_installed = false; - -void fbody_buttonOnPressed(value_t self, int32_t _buttonPin, value_t _callback) { - ROOT_SET(func_rootset, 1) - func_rootset.values[0] = _callback; - gpio_set_direction(_buttonPin, GPIO_MODE_INPUT); - gpio_set_intr_type(_buttonPin, GPIO_INTR_POSEDGE); - if (!is_isr_installed) { - gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT); - is_isr_installed = true; - } - gpio_isr_handler_add(_buttonPin, bs_event_push_from_isr, (void*)func_rootset.values[0]); - DELETE_ROOT_SET(func_rootset) -} -struct func_body _buttonOnPressed = { fbody_buttonOnPressed, "(i()v)v" }; diff --git a/modules/esp32/c/display-helper.c b/modules/esp32/c/display-helper.c deleted file mode 100644 index 56d5c44..0000000 --- a/modules/esp32/c/display-helper.c +++ /dev/null @@ -1,510 +0,0 @@ -#include -#include -#include "esp_system.h" -#include "driver/spi_master.h" -#include "driver/gpio.h" - - -#define LCD_HOST HSPI_HOST - -#define PIN_NUM_MISO 19 -#define PIN_NUM_MOSI 23 -#define PIN_NUM_CLK 18 -#define PIN_NUM_CS 14 - -#define PIN_NUM_DC 27 -#define PIN_NUM_RST 33 -#define PIN_NUM_BCKL 32 - -#define DISPLAY_WIDTH 320 -#define DISPLAY_HEIGHT 240 - -#define CHAR_WIDTH 8 -#define CHAR_HEIGHT 8 - -// To speed up transfers, every SPI transfer sends a bunch of lines. This define specifies how many. More means more memory use, -// but less overhead for setting up / finishing transfers. Make sure 240 is dividable by this. -#define PARALLEL_LINES 16 -#define MAX_TRANSFER_SIZE PARALLEL_LINES*DISPLAY_WIDTH*2+8 - -#define MAX(a,b) (((a) > (b)) ? (a) : (b)) -#define MIN(a,b) (((a) < (b)) ? (a) : (b)) - - -// Display commands -#define COLUMN_SET 0x2a -#define PAGE_SET 0x2b -#define RAM_WRITE 0x2c -#define RAM_READ 0x2e -#define DISPLAY_ON 0x29 -#define WAKE 0x11 -#define LINE_SET 0x37 -#define MADCTL 0x36 -#define DISPLAY_INVERSION_ON 0x21 - -uint8_t* get_font8x8_char(char chr); - -typedef struct { - uint8_t cmd; - uint8_t data[16]; - uint8_t databytes; //No of data in data; bit 7 = delay after set; 0xFF = end of cmds. -} lcd_init_cmd_t; - -spi_device_handle_t spi; - -#define CHAR_VMARGIN 4 -#define TEXT_HMARGIN 10 -#define TEXT_VMARGIN 10 - -int32_t text_dx = 0; -int32_t text_dy = 0; - -DRAM_ATTR const lcd_init_cmd_t init_cmds[]={ - // {0xCF, {0x00, 0x83, 0X30}, 3}, - {0xef, {0x03, 0x80, 0x02}, 3}, - {0xcf, {0x00, 0xc1, 0x30}, 3}, - {0xed, {0x64, 0x03, 0x12, 0x81}, 4}, - {0xe8, {0x85, 0x00, 0x78}, 3}, - {0xcb, {0x39, 0x2c, 0x00, 0x34, 0x02}, 5}, - {0xf7, {0x20}, 1}, - {0xea, {0x00, 0x00}, 2}, - {0xc0, {0x23}, 1}, - {0xc1, {0x10}, 1}, - {0xc5, {0x3e, 0x28}, 2}, - {0xc7, {0x86}, 1}, - {0x36, {0x48}, 1}, - {0x3a, {0x55}, 1}, - {0xb1, {0x00, 0x18}, 2}, - {0xb6, {0x08, 0x82, 0x27}, 3}, - {0xf2, {0x00}, 1}, - {0x26, {0x01}, 1}, - {0xe0, {0x0f, 0x31, 0x2b, 0x0c, 0x0e, 0x08, 0x4e, 0xf1, 0x37, 0x07, 0x10, 0x03, 0x0e, 0x09, 0x00}, 15}, - {0xe1, {0x00, 0x0e, 0x14, 0x03, 0x11, 0x07, 0x31, 0xc1, 0x48, 0x08, 0x0f, 0x0c, 0x31, 0x36, 0x0f}, 15}, -}; - -void spi_pre_transfer_callback(spi_transaction_t *t) { - int dc=(int)t->user; - gpio_set_level(PIN_NUM_DC, dc); -} - -void spi_write_cmd(const uint8_t cmd) { - esp_err_t ret; - spi_transaction_t t; - memset(&t, 0, sizeof(t)); //Zero out the transaction - t.length=8; //Command is 8 bits - t.tx_buffer=&cmd; //The data is the cmd itself - t.user=(void*)0; //D/C needs to be set to 0 - ret=spi_device_polling_transmit(spi, &t); //Transmit! - assert(ret==ESP_OK); //Should have had no issues. -} - -void spi_write_data(const uint8_t *data, int len) { - esp_err_t ret; - spi_transaction_t t; - if (len==0) return; //no need to send anything - memset(&t, 0, sizeof(t)); //Zero out the transaction - t.length=len*8; //Len is in bytes, transaction length is in bits. - t.tx_buffer=data; //Data - t.user=(void*)1; //D/C needs to be set to 1 - ret=spi_device_polling_transmit(spi, &t); //Transmit! - assert(ret==ESP_OK); //Should have had no issues. -} - -void spi_init() { - esp_err_t ret; - spi_bus_config_t buscfg={ - .miso_io_num=PIN_NUM_MISO, - .mosi_io_num=PIN_NUM_MOSI, - .sclk_io_num=PIN_NUM_CLK, - .quadwp_io_num=-1, - .quadhd_io_num=-1, - .max_transfer_sz=MAX_TRANSFER_SIZE - }; - spi_device_interface_config_t devcfg={ - .clock_speed_hz=10*1000*1000, //Clock out at 10 MHz - .mode=0, //SPI mode 0 - .spics_io_num=PIN_NUM_CS, //CS pin - .queue_size=7, //We want to be able to queue 7 transactions at a time - .pre_cb=spi_pre_transfer_callback, - }; - //Initialize the SPI bus - ret=spi_bus_initialize(LCD_HOST, &buscfg, 2); - ESP_ERROR_CHECK(ret); - //Attach the LCD to the SPI bus - ret=spi_bus_add_device(LCD_HOST, &devcfg, &spi); - - ESP_ERROR_CHECK(ret); -} - -void gpio_init() { - //Initialize non-SPI GPIOs - // gpio_set_direction(PIN_NUM_CS, GPIO_MODE_OUTPUT); - gpio_set_direction(PIN_NUM_DC, GPIO_MODE_OUTPUT); - gpio_set_direction(PIN_NUM_RST, GPIO_MODE_OUTPUT); - gpio_set_direction(PIN_NUM_BCKL, GPIO_MODE_OUTPUT); - - // gpio_set_level(PIN_NUM_CS, 1); - gpio_set_level(PIN_NUM_DC, 0); - gpio_set_level(PIN_NUM_RST, 0); - gpio_set_level(PIN_NUM_BCKL, 0); -} - -void write_block(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t *data, uint32_t datalen) { - if (datalen > MAX_TRANSFER_SIZE - 8) { - printf("Error: The length of data should be smaller than %d.\n", (int)datalen); - } - - esp_err_t ret; - int x; - static spi_transaction_t trans[6]; - for (x=0; x<6; x++) { - memset(&trans[x], 0, sizeof(spi_transaction_t)); - if ((x&1)==0) { - //Even transfers are commands - trans[x].length=8; - trans[x].user=(void*)0; - } else { - //Odd transfers are data - trans[x].length=8*4; - trans[x].user=(void*)1; - } - trans[x].flags=SPI_TRANS_USE_TXDATA; - } - - trans[0].tx_data[0] = COLUMN_SET; //Column Address Set - trans[1].tx_data[0] = x0 >> 8; //Start Col High - trans[1].tx_data[1] = x0 & 0xff; //Start Col Low - trans[1].tx_data[2] = x1 >> 8; //End Col High - trans[1].tx_data[3] = x1 & 0xff; //End Col Low - trans[2].tx_data[0] = PAGE_SET; //Page address set - trans[3].tx_data[0] = y0 >> 8; //Start page high - trans[3].tx_data[1] = y0 & 0xff; //start page low - trans[3].tx_data[2] = y1 >> 8; //end page high - trans[3].tx_data[3] = y1 & 0xff; //end page low - trans[4].tx_data[0] = RAM_WRITE; //memory write - trans[5].tx_buffer = data; //finally send the line data - trans[5].length = datalen * 16; //Data length, in bits - trans[5].flags = 0; //undo SPI_TRANS_USE_TXDATA flag - - //Queue all transactions. - for (x=0; x<6; x++) { - ret=spi_device_queue_trans(spi, &trans[x], portMAX_DELAY); - assert(ret==ESP_OK); - } -} - -void check_write_block_finish() { - spi_transaction_t *rtrans; - esp_err_t ret; - //Wait for all 6 transactions to be done and get back the results. - for (int x = 0; x<6; x++) { - ret=spi_device_get_trans_result(spi, &rtrans, portMAX_DELAY); - assert(ret == ESP_OK); - //We could inspect rtrans now if we received any info back. The LCD is treated as write-only, though. - } -} - -void display_draw(bool (*draw_func)(uint32_t, uint32_t), uint16_t color, uint16_t background) { - uint16_t *lines[2]; - //Allocate memory for the pixel buffers - for (int i = 0; i<2; i++) { - lines[i] = heap_caps_malloc(320*PARALLEL_LINES*sizeof(uint16_t), MALLOC_CAP_DMA); - assert(lines[i] != NULL); - } - int frame = 0; - //Indexes of the line currently being sent to the LCD and the line we're calculating. - int sending_line = -1; - int calc_line = 0; - - for (uint32_t line_start = 0; line_start < 240; line_start += PARALLEL_LINES) { - uint16_t *bmp = lines[calc_line]; - for (uint32_t y = line_start; y < line_start + PARALLEL_LINES; y++) { - for (uint32_t x = 0; x < DISPLAY_WIDTH; x++) { - *bmp++ = draw_func(x, y) ? color : background; - } - } - - //Finish up the sending process of the previous line, if any - if (sending_line != -1) check_write_block_finish(); - //Swap sending_line and calc_line - sending_line = calc_line; - calc_line = (calc_line == 1) ? 0 : 1; - //Send the line we currently calculated. - write_block(0, line_start, DISPLAY_WIDTH, line_start + PARALLEL_LINES, lines[sending_line], DISPLAY_WIDTH * PARALLEL_LINES); - //The line set is queued up for sending now; the actual sending happens in the - //background. We can go on to calculate the next line set as long as we do not - //touch line[sending_line]; the SPI sending process is still reading from that. - } - check_write_block_finish(); - heap_caps_free(lines[0]); - heap_caps_free(lines[1]); -} - -bool fill(uint32_t x, uint32_t y) { - return true; -} - -bool circle(uint32_t x, uint32_t y) { - return (x - 160) * (x - 160) + (y - 120) * (y - 120) > 50 * 50; -} - -bool heart(uint32_t x, uint32_t y) { - const int32_t a = 30; - const int32_t b = 30; - int32_t _x = (int32_t)x - 160; - int32_t _y = (int32_t)y - 120; - int32_t abs_x = _x > 0 ? _x : - _x; - - if (_y > 0) - return b * _y <= a * (2 * a - abs_x); - else - return _y * (_y + 2 * b) <= abs_x * (2 * a - abs_x); -} - -bool small_heart(uint32_t x, uint32_t y) { - const int32_t a = 20; - const int32_t b = 20; - int32_t _x = (int32_t)x - 160; - int32_t _y = (int32_t)y - 120; - int32_t abs_x = _x > 0 ? _x : - _x; - - if (_y > 0) - return b * _y <= a * (2 * a - abs_x); - else - return _y * (_y + 2 * b) <= abs_x * (2 * a - abs_x); -} - -bool happy_face(uint32_t x, uint32_t y) { - int32_t face_r = 80; - int32_t eye_x = 30; - int32_t eye_y = 30; - int32_t eye_r = 8; - int32_t mouse_r = 45; - int32_t line_width = 2; - - int32_t _x = (int32_t)x - 160; - int32_t _y = (int32_t)y - 120; - int32_t abs_x = _x > 0 ? _x : - _x; - int32_t sqr_r = _x * _x + _y * _y; - - if ((face_r - line_width) * (face_r - line_width) <= sqr_r && sqr_r <= (face_r + line_width) * (face_r + line_width)) { - return true; - } else if (_y >= 0 && ((mouse_r - line_width) * (mouse_r - line_width) <= sqr_r) && (sqr_r <= (mouse_r + line_width) * (mouse_r + line_width))) { - return true; - } else if ((abs_x - eye_x) * (abs_x - eye_x) + (_y + eye_y) * (_y + eye_y) <= eye_r * eye_r) { - return true; - } else { - return false; - } -} - -bool sad_face(uint32_t x, uint32_t y) { - int32_t face_r = 80; - int32_t eye_x = 30; - int32_t eye_y = 30; - int32_t eye_r = 8; - int32_t mouse_r = 50; - int32_t mouse_limit = 30; - int32_t mouse_y = 60; - int32_t line_width = 2; - - int32_t _x = (int32_t)x - 160; - int32_t _y = (int32_t)y - 120; - int32_t abs_x = _x > 0 ? _x : - _x; - int32_t sqr_r = _x * _x + _y * _y; - - if ((face_r - line_width) * (face_r - line_width) <= sqr_r && sqr_r <= (face_r + line_width) * (face_r + line_width)) { - return true; - } else if (0 <= _y && abs_x * (mouse_r - mouse_limit) <= (mouse_y - _y) * mouse_r && (mouse_r - line_width) * (mouse_r - line_width) <= _x * _x + (_y - mouse_y) * (_y - mouse_y) && _x * _x + (_y - mouse_y) * (_y - mouse_y) <= (mouse_r + line_width) * (mouse_r + line_width)) { - return true; - } else if ((abs_x - eye_x) * (abs_x - eye_x) + (_y + eye_y) * (_y + eye_y) <= eye_r * eye_r) { - return true; - } else { - return false; - } -} - -void display_char(char chr, uint32_t x, uint32_t y, uint16_t color, uint16_t background) { - uint8_t *chr_font8x8 = get_font8x8_char(chr); - uint16_t *chr_bmp = heap_caps_malloc(CHAR_WIDTH * CHAR_HEIGHT * sizeof(uint16_t), MALLOC_CAP_DMA); - for (int col = 0; col < CHAR_WIDTH; col++) { - uint8_t byte = chr_font8x8[col]; - for (int row = 0; row < CHAR_HEIGHT; row++) { - chr_bmp[row * CHAR_WIDTH + col] = byte & (1 << row) ? color : background; - } - } - - write_block(x, y, x + 7, y + 7, chr_bmp, 8 * 8); - check_write_block_finish(); -} - -void text_new_line() { - text_dx = 0; - text_dy += CHAR_HEIGHT + CHAR_VMARGIN; -} - -void display_text(char *text, uint16_t color, uint16_t background) { - char c = text[0]; - int i = 0; - while (c != '\0') { - if (c == '\n') { - text_new_line(); - c = text[++i]; - continue; - } - if (TEXT_HMARGIN * 2 + text_dx + CHAR_WIDTH > DISPLAY_WIDTH) { - text_new_line(); - continue; - } - if (TEXT_VMARGIN * 2 + text_dy > DISPLAY_HEIGHT) break; - - display_char(c, TEXT_HMARGIN + text_dx, TEXT_VMARGIN + text_dy, color, background); - c = text[++i]; - text_dx += CHAR_WIDTH; - } - - text_new_line(); -} - -const uint8_t font8x8[] = { - /* */ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - /* ! */ 0x00,0x00,0x00,0x00,0x4f,0x4f,0x00,0x00, - /* " */ 0x00,0x00,0x07,0x07,0x00,0x00,0x07,0x07, - /* # */ 0x00,0x14,0x7f,0x7f,0x14,0x14,0x7f,0x7f, - /* $ */ 0x00,0x00,0x24,0x2e,0x6b,0x6b,0x3a,0x12, - /* % */ 0x00,0x00,0x63,0x33,0x18,0x0c,0x66,0x63, - /* & */ 0x00,0x00,0x32,0x7f,0x4d,0x4d,0x77,0x72, - - /* flipped backtick! */ 0x00,0x00,0x00,0x00,0x04,0x06,0x03,0x01, - - /* ( */ 0x00,0x00,0x00,0x1c,0x3e,0x63,0x41,0x00, - /* ) */ 0x00,0x00,0x00,0x41,0x63,0x3e,0x1c,0x00, - /* * */ 0x00,0x08,0x2a,0x3e,0x1c,0x1c,0x3e,0x2a, - /* + */ 0x00,0x00,0x08,0x08,0x3e,0x3e,0x08,0x08, - /* , */ 0x00,0x00,0x00,0x80,0xe0,0x60,0x00,0x00, - /* - */ 0x00,0x00,0x08,0x08,0x08,0x08,0x08,0x08, - /* . */ 0x00,0x00,0x00,0x00,0x60,0x60,0x00,0x00, - /* / */ 0x00,0x00,0x40,0x60,0x30,0x18,0x0c,0x06, - - /* 0 */ 0x00,0x00,0x3e,0x7f,0x49,0x45,0x7f,0x3e, - /* 1 */ 0x00,0x00,0x40,0x44,0x7f,0x7f,0x40,0x40, - /* 2 */ 0x00,0x00,0x62,0x73,0x51,0x49,0x4f,0x46, - /* 3 */ 0x00,0x00,0x22,0x63,0x49,0x49,0x7f,0x36, - /* 4 */ 0x00,0x00,0x18,0x18,0x14,0x16,0x7f,0x7f, - /* 5 */ 0x00,0x00,0x27,0x67,0x45,0x45,0x7d,0x39, - /* 6 */ 0x00,0x00,0x3e,0x7f,0x49,0x49,0x7b,0x32, - /* 7 */ 0x00,0x00,0x03,0x03,0x79,0x7d,0x07,0x03, - /* 8 */ 0x00,0x00,0x36,0x7f,0x49,0x49,0x7f,0x36, - /* 9 */ 0x00,0x00,0x26,0x6f,0x49,0x49,0x7f,0x3e, - /* : */ 0x00,0x00,0x00,0x00,0x24,0x24,0x00,0x00, - /* ; */ 0x00,0x00,0x00,0x80,0xe4,0x64,0x00,0x00, - /* < */ 0x00,0x00,0x08,0x1c,0x36,0x63,0x41,0x41, - /* = */ 0x00,0x00,0x14,0x14,0x14,0x14,0x14,0x14, - /* > */ 0x00,0x00,0x41,0x41,0x63,0x36,0x1c,0x08, - /* ? */ 0x00,0x00,0x02,0x03,0x51,0x59,0x0f,0x06, - - /* @ */ 0x00,0x00,0x3e,0x7f,0x41,0x4d,0x4f,0x2e, - - /* A */ 0x00,0x00,0x7c,0x7e,0x0b,0x0b,0x7e,0x7c, - /* B */ 0x00,0x00,0x7f,0x7f,0x49,0x49,0x7f,0x36, - /* 1 */ 0x00,0x00,0x3e,0x7f,0x41,0x41,0x63,0x22, - /* D */ 0x00,0x00,0x7f,0x7f,0x41,0x63,0x3e,0x1c, - /* E */ 0x00,0x00,0x7f,0x7f,0x49,0x49,0x41,0x41, - /* F */ 0x00,0x00,0x7f,0x7f,0x09,0x09,0x01,0x01, - /* G */ 0x00,0x00,0x3e,0x7f,0x41,0x49,0x7b,0x3a, - /* H */ 0x00,0x00,0x7f,0x7f,0x08,0x08,0x7f,0x7f, - /* I */ 0x00,0x00,0x00,0x41,0x7f,0x7f,0x41,0x00, - /* J */ 0x00,0x00,0x20,0x60,0x41,0x7f,0x3f,0x01, - /* K */ 0x00,0x00,0x7f,0x7f,0x1c,0x36,0x63,0x41, - /* L */ 0x00,0x00,0x7f,0x7f,0x40,0x40,0x40,0x40, - /* M */ 0x00,0x00,0x7f,0x7f,0x06,0x0c,0x06,0x7f, - /* N */ 0x00,0x00,0x7f,0x7f,0x0e,0x1c,0x7f,0x7f, - /* O */ 0x00,0x00,0x3e,0x7f,0x41,0x41,0x7f,0x3e, - /* P */ 0x00,0x00,0x7f,0x7f,0x09,0x09,0x0f,0x06, - /* Q */ 0x00,0x00,0x1e,0x3f,0x21,0x61,0x7f,0x5e, - /* R */ 0x00,0x00,0x7f,0x7f,0x19,0x39,0x6f,0x46, - /* S */ 0x00,0x00,0x26,0x6f,0x49,0x49,0x7b,0x32, - /* T */ 0x00,0x00,0x01,0x01,0x7f,0x7f,0x01,0x01, - /* U */ 0x00,0x00,0x3f,0x7f,0x40,0x40,0x7f,0x3f, - /* V */ 0x00,0x00,0x1f,0x3f,0x60,0x60,0x3f,0x1f, - /* W */ 0x00,0x00,0x7f,0x7f,0x30,0x18,0x30,0x7f, - /* X */ 0x00,0x00,0x63,0x77,0x1c,0x1c,0x77,0x63, - /* Y */ 0x00,0x00,0x07,0x0f,0x78,0x78,0x0f,0x07, - /* Z */ 0x00,0x00,0x61,0x71,0x59,0x4d,0x47,0x43, - - /* [ */ 0x00,0x00,0x7f,0x7f,0x41,0x41,0x00,0x00, - /* flipped forward slash */ 0x06,0x0c,0x18,0x30,0x60,0x40,0x00,0x00,// 0x00,0x00,0x40,0x60,0x30,0x18,0x0c,0x06, - // /* (pound) */ 0x00,0x22,0x49,0x49,0x5e,0x7c,0x68,0x40, /* pound in stead of backslash */ - /* ] */ 0x00,0x00,0x41,0x41,0x7f,0x7f,0x00,0x00, - - /* 0x00,0x00,0x7E,0x42,0x42,0x00,0x00,0x00, // [ - 0x00,0x04,0x08,0x10,0x20,0x40,0x00,0x00, // - 0x00,0x00,0x42,0x42,0x7E,0x00,0x00,0x00, // ] */ - - 0x00,0x08,0x04,0x7E,0x04,0x08,0x00,0x00, // ^ - 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00, // _ - - /* backslash */ 0x00,0x01,0x03,0x06,0x04,0x00,0x00,0x00, - /* a */ 0x00,0x00,0x20,0x74,0x54,0x54,0x7c,0x78, - /* b */ 0x00,0x00,0x7e,0x7e,0x48,0x48,0x78,0x30, - /* c */ 0x00,0x00,0x38,0x7c,0x44,0x44,0x44,0x00, - /* d */ 0x00,0x00,0x30,0x78,0x48,0x48,0x7e,0x7e, - /* e */ 0x00,0x00,0x38,0x7c,0x54,0x54,0x5c,0x18, - /* f */ 0x00,0x00,0x00,0x08,0x7c,0x7e,0x0a,0x0a, - /* g */ 0x00,0x00,0x98,0xbc,0xa4,0xa4,0xfc,0x7c, - /* h */ 0x00,0x00,0x7e,0x7e,0x08,0x08,0x78,0x70, - /* i */ 0x00,0x00,0x00,0x48,0x7a,0x7a,0x40,0x00, - /* j */ 0x00,0x00,0x00,0x80,0x80,0x80,0xfa,0x7a, - /* k */ 0x00,0x00,0x7e,0x7e,0x10,0x38,0x68,0x40, - /* l */ 0x00,0x00,0x00,0x42,0x7e,0x7e,0x40,0x00, - /* m */ 0x00,0x00,0x7c,0x7c,0x18,0x38,0x1c,0x7c, - /* n */ 0x00,0x00,0x7c,0x7c,0x04,0x04,0x7c,0x78, - /* o */ 0x00,0x00,0x38,0x7c,0x44,0x44,0x7c,0x38, - /* p */ 0x00,0x00,0xfc,0xfc,0x24,0x24,0x3c,0x18, - /* q */ 0x00,0x00,0x18,0x3c,0x24,0x24,0xfc,0xfc, - /* r */ 0x00,0x00,0x7c,0x7c,0x04,0x04,0x0c,0x08, - /* s */ 0x00,0x00,0x48,0x5c,0x54,0x54,0x74,0x24, - /* t */ 0x00,0x00,0x04,0x04,0x3e,0x7e,0x44,0x44, - /* u */ 0x00,0x00,0x3c,0x7c,0x40,0x40,0x7c,0x7c, - /* v */ 0x00,0x00,0x1c,0x3c,0x60,0x60,0x3c,0x1c, - /* w */ 0x00,0x00,0x1c,0x7c,0x70,0x38,0x70,0x7c, - /* x */ 0x00,0x00,0x44,0x6c,0x38,0x38,0x6c,0x44, - /* y */ 0x00,0x00,0x9c,0xbc,0xa0,0xe0,0x7c,0x3c, - /* z */ 0x00,0x00,0x44,0x64,0x74,0x5c,0x4c,0x44, - 0x00,0x08,0x08,0x76,0x42,0x42,0x00,0x00, // { - 0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00, // | - 0x00,0x42,0x42,0x76,0x08,0x08,0x00,0x00, // } - 0x00,0x00,0x04,0x02,0x04,0x02,0x00,0x00, // ~ - - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, // BLOCK = 127 - /* (spades) */ 0x18,0x5c,0x7e,0x7f,0x7e,0x5c,0x18,0x00, // 128 - /* (heart) */ 0x0e,0x1f,0x3f,0x7e,0x3f,0x1f,0x0e,0x00, // 129 - /* (clubs) */ 0x00,0x0c,0x4c,0x73,0x73,0x4c,0x0c,0x00, // 130 - /* (diamonds) */ 0x08,0x1c,0x3e,0x7f,0x3e,0x1c,0x08,0x00, // 131 - - /* progress bar outline */ - 0x18, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, // |= // 132 - 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, // = 133 - 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x18, // =| 134 - - /* progress bar filled */ - 0x18, 0x3c, 0x3c, 0x3c, 0x3c, 0x3c, 0x3c, 0x3c, // 135 - 0x3c, 0x3c, 0x3c, 0x3c, 0x3c, 0x3c, 0x3c, 0x3c, // 136 - 0x3c, 0x3c, 0x3c, 0x3c, 0x3c, 0x3c, 0x3c, 0x18, // 137 - - /* arrows (navi) */ - 0x04, 0x0E, 0x1F, 0x04, 0x04, 0x04, 0xF8, 0x00, // 138 arrow right [correct orientation] - 0x00, 0xF8, 0x04, 0x04, 0x04, 0x1F, 0x0E, 0x04, // 139 arrow right [correct orientation] - 0x00, 0x04, 0x06, 0xFF, 0x06, 0x04, 0x00, 0x00, // 140 arrow up - - 6, 14, 31, 0xFB, 31, 14, 6, 0x00, // 141 GPS pin... - 0x00, 0x06, 0x0f, 0x09, 0x0f, 0x06, 0x00, 0x00, // 142 angle / degrees sign - - 0x00, 0x20, 0x60, 0xFF, 0x60, 0x20, 0x00, 0x00 // 143 arrow DOWN -}; - -uint8_t* get_font8x8_char(char chr) { - uint32_t idx = chr - ' '; - return font8x8 + idx * 8; -} - diff --git a/modules/esp32/c/display.c b/modules/esp32/c/display.c deleted file mode 100644 index bf5b81f..0000000 --- a/modules/esp32/c/display.c +++ /dev/null @@ -1,121 +0,0 @@ -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "include/display.h" -#include "display-helper.c" - -#define BS_DISPLAY_TAG "BS_DISPLAY" - -#define BS_DISPLAY_ICON_HEART 0 -#define BS_DISPLAY_ICON_SMALL_HEART 1 -#define BS_DISPLAY_ICON_HAPPY_FACE 2 -#define BS_DISPLAY_ICON_SAD_FACE 3 - - -void mth_0_Display(value_t self); -void mth_1_Display(value_t self, int32_t _color); -void mth_2_Display(value_t self, int32_t _icon, int32_t _color, int32_t _background); -void mth_3_Display(value_t self, value_t _str, int32_t _color, int32_t _background); -void mth_4_Display(value_t self, int32_t _int, int32_t _color, int32_t _background); -int32_t mth_5_Display(value_t self, int32_t _r, int32_t _g, int32_t _b); -const uint16_t plist_Display[] = { 1, 2, 3, 4 }; -extern CLASS_OBJECT(object_class, 1); -class_Display_t class_Display = { - .body = { .s = 4, .i = 4, .cn = "Display", .sc = &object_class.clazz , .pt = { .size = 4, .offset = 0, - .unboxed = 4, .prop_names = plist_Display, .unboxed_types = "iiii" }, .vtbl = { mth_0_Display, mth_1_Display, mth_2_Display, mth_3_Display, mth_4_Display, mth_5_Display }}}; - -static void cons_Display(value_t self) { - ROOT_SET(func_rootset, 1) - { - *get_obj_int_property(self, 0) = BS_DISPLAY_ICON_HEART; - *get_obj_int_property(self, 1) = BS_DISPLAY_ICON_SMALL_HEART; - *get_obj_int_property(self, 2) = BS_DISPLAY_ICON_HAPPY_FACE; - *get_obj_int_property(self, 3) = BS_DISPLAY_ICON_SAD_FACE; - spi_init(); - gpio_init(); - mth_0_Display(0); - int cmd = 0; - //Send all the commands - while (init_cmds[cmd].databytes!=0xff) { - spi_write_cmd(init_cmds[cmd].cmd); - spi_write_data(init_cmds[cmd].data, init_cmds[cmd].databytes&0x1F); - if (init_cmds[cmd].databytes&0x80) { - vTaskDelay(100 / portTICK_PERIOD_MS); - } - cmd++; - } - - spi_write_cmd(MADCTL); - uint8_t data[16] = {0x08}; - spi_write_data(data, 1&0x1F); - spi_write_cmd(DISPLAY_INVERSION_ON); - spi_write_cmd(WAKE); - vTaskDelay(120 / portTICK_PERIOD_MS); - spi_write_cmd(DISPLAY_ON); - gpio_set_level(PIN_NUM_BCKL, 1); - } - DELETE_ROOT_SET(func_rootset) -} - -value_t new_Display(value_t self) { cons_Display(self); return self; } - -// reset -void mth_0_Display(value_t self) { - gpio_set_level(PIN_NUM_BCKL, 0); - gpio_set_level(PIN_NUM_RST, 0); - vTaskDelay(50 / portTICK_PERIOD_MS); - gpio_set_level(PIN_NUM_RST, 1); - vTaskDelay(50 / portTICK_PERIOD_MS); -} - -// fill -void mth_1_Display(value_t self, int32_t _color) { - display_draw(fill, _color, _color); -} - -// show icon -void mth_2_Display(value_t self, int32_t _icon, int32_t _color, int32_t _background) { - switch (_icon) { - case BS_DISPLAY_ICON_HEART: - display_draw(heart, _color, _background); - break; - case BS_DISPLAY_ICON_SMALL_HEART: - display_draw(small_heart, _color, _background); - break; - case BS_DISPLAY_ICON_HAPPY_FACE: - display_draw(happy_face, _color, _background); - break; - case BS_DISPLAY_ICON_SAD_FACE: - display_draw(sad_face, _color, _background); - break; - default: - break; - } -} - -// show string -void mth_3_Display(value_t self, value_t _str, int32_t _color, int32_t _background) { - ROOT_SET(func_rootset, 1) - func_rootset.values[0] = _str; - char* text = gc_string_literal_cstr(_str); - display_text(text, (uint16_t)_color, (uint16_t)_background); - DELETE_ROOT_SET(func_rootset) -} - -// show int -void mth_4_Display(value_t self, int32_t _int, int32_t _color, int32_t _background) { - char buff[12]; // Max num length + blank - sprintf(buff, "%ld", _int); - display_text(&buff, (uint16_t)_color, (uint16_t)_background); -} - -// color -int32_t mth_5_Display(value_t self, int32_t _r, int32_t _g, int32_t _b) { - int32_t _color = 0; - _color |= ((_r >> 3) << 11); - _color |= ((_g >> 2) << 5); - _color |= ((_b >> 3) << 0); - _color = (_color >> 8) | (_color << 8); // big-endian - { int32_t ret_value_ = (_color); ; return ret_value_; } -} \ No newline at end of file diff --git a/modules/esp32/c/gpio.c b/modules/esp32/c/gpio.c deleted file mode 100644 index 04af88f..0000000 --- a/modules/esp32/c/gpio.c +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include "driver/gpio.h" -#include "include/gpio.h" - -void mth_0_GPIO(value_t self, int32_t _level); -extern CLASS_OBJECT(object_class, 1); -static const uint16_t plist_GPIO[] = { 1 }; -class_GPIO_t class_GPIO ={ - .body = { .s = 1, .i = 1, .cn = "GPIO", .sc = &object_class.clazz , .pt = { .size = 1, .offset = 0, - .unboxed = 1, .prop_names = plist_GPIO, .unboxed_types = "i" }, .vtbl = { mth_0_GPIO, }}}; - -static void cons_GPIO(value_t self, int32_t _pinNum) { - *get_obj_int_property(self, 0) = _pinNum; - gpio_set_direction(_pinNum, GPIO_MODE_OUTPUT); -} - -value_t new_GPIO(value_t self, int32_t p0) { cons_GPIO(self, p0); return self; } - - -void mth_0_GPIO(value_t self, int32_t _level) { - gpio_set_level(*get_obj_int_property(self, 0), _level); -} \ No newline at end of file diff --git a/modules/esp32/c/include/button.h b/modules/esp32/c/include/button.h deleted file mode 100644 index ec73e35..0000000 --- a/modules/esp32/c/include/button.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef __BS_BUTTON__ -#define __BS_BUTTON__ - -#include "c-runtime.h" -#include "section.h" - -void MD_SECTION_TEXT fbody_buttonOnPressed(value_t self, int32_t _buttonPin, value_t _callback); -extern MD_SECTION_DATA struct func_body _buttonOnPressed; - -#endif /* __BS_BUTTON__ */ diff --git a/modules/esp32/c/include/display.h b/modules/esp32/c/include/display.h deleted file mode 100644 index 2b9e7f2..0000000 --- a/modules/esp32/c/include/display.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef __BS_DISPLAY__ -#define __BS_DISPLAY__ - -#include "c-runtime.h" -#include "section.h" - -value_t MD_SECTION_TEXT new_Display(value_t self); - -typedef union { - struct class_object clazz; - struct { uint32_t s; uint32_t i; const char* const cn; const struct class_object* const sc; struct property_table pt; void* vtbl[6]; } body; -} class_Display_t; - -extern MD_SECTION_DATA class_Display_t class_Display; - -#endif /* __BS_DISPLAY__ */ diff --git a/modules/esp32/c/include/gpio.h b/modules/esp32/c/include/gpio.h deleted file mode 100644 index 6a2a1b1..0000000 --- a/modules/esp32/c/include/gpio.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef __BS_GPIO__ -#define __BS_GPIO__ - -#include "c-runtime.h" -#include "section.h" - -value_t MD_SECTION_TEXT new_GPIO(value_t self, int32_t p0); - -typedef union { - struct class_object clazz; - struct { uint32_t s; uint32_t i; const char* const cn; const struct class_object* const sc; struct property_table pt; void* vtbl[1]; } body; -} class_GPIO_t; - -extern MD_SECTION_DATA class_GPIO_t class_GPIO; - -#endif /* __BS_GPIO__ */ diff --git a/modules/esp32/c/include/section.h b/modules/esp32/c/include/section.h deleted file mode 100644 index 1842677..0000000 --- a/modules/esp32/c/include/section.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef __BS_MD_SECTION__ -#define __BS_MD_SECTION__ - -#include - -#define MD_SECTION_TEXT __attribute__((section(".modules_text"))) -#define MD_SECTION_DATA __attribute__((section(".modules_data"))) - -#endif /* __BS_MD_SECTION__ */ - diff --git a/modules/esp32/c/include/timer.h b/modules/esp32/c/include/timer.h deleted file mode 100644 index 9bcde42..0000000 --- a/modules/esp32/c/include/timer.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef __BS_TIMER__ -#define __BS_TIMER__ - -#include -#include "c-runtime.h" -#include "section.h" - -int32_t MD_SECTION_TEXT fbody_setInterval(value_t self, value_t _func, int32_t delay); -int32_t MD_SECTION_TEXT fbody_setTimeout(value_t self, value_t _func, int32_t delay); -void MD_SECTION_TEXT fbody_clearInterval(value_t self, int32_t timerId); -void MD_SECTION_TEXT fbody_clearTimeout(value_t self, int32_t timerId); -int32_t MD_SECTION_TEXT fbody_getTimeUs(value_t self); - -extern MD_SECTION_DATA struct func_body _setInterval; -extern MD_SECTION_DATA struct func_body _setTimeout; -extern MD_SECTION_DATA struct func_body _clearInterval; -extern MD_SECTION_DATA struct func_body _clearTimeout; -extern MD_SECTION_DATA struct func_body _getTimeUs; - -#endif /* __BS_TIMER__ */ diff --git a/modules/esp32/c/timer.c b/modules/esp32/c/timer.c deleted file mode 100644 index ce6db21..0000000 --- a/modules/esp32/c/timer.c +++ /dev/null @@ -1,76 +0,0 @@ -#include -#include "esp_timer.h" -#include "esp_log.h" -#include "include/timer.h" -#include "event.h" - -#define BS_TIMER_TAG "BS_TIMER" -#define NUM_TIMERS 10 - -esp_timer_handle_t timer_handlers[NUM_TIMERS] = {0}; - -static int32_t find_unused_timer_id() { - for (uint32_t i = 0; i < NUM_TIMERS; i++) { - if (timer_handlers[i] == 0) return i; - } - ESP_LOGE(BS_TIMER_TAG, "The number of used timers has exceeded the max numbers: %d", NUM_TIMERS); - return -1; -} - - -int32_t fbody_setInterval(value_t self, value_t _func, int32_t _delayMs) { - ROOT_SET(func_rootset, 1) - func_rootset.values[0] = _func; - int32_t timer_id = find_unused_timer_id(); - esp_timer_handle_t timer; - const esp_timer_create_args_t timer_args = { - .callback = &bs_event_push_from_isr, - .arg = (void*) func_rootset.values[0], - }; - ESP_ERROR_CHECK(esp_timer_create(&timer_args, &timer)); - esp_timer_start_periodic(timer, _delayMs*1000); - timer_handlers[timer_id] = timer; - { int32_t ret_value_ = (timer_id); DELETE_ROOT_SET(func_rootset); return ret_value_; } -} - - -int32_t fbody_setTimeout(value_t self, value_t _func, int32_t _delayMs) { - ROOT_SET(func_rootset, 1) - func_rootset.values[0] = _func; - int32_t timer_id = find_unused_timer_id(); - esp_timer_handle_t timer; - const esp_timer_create_args_t timer_args = { - .callback = &bs_event_push_from_isr, - .arg = (void*) func_rootset.values[0], - }; - ESP_ERROR_CHECK(esp_timer_create(&timer_args, &timer)); - esp_timer_start_once(timer, _delayMs*1000); - timer_handlers[timer_id] = timer; - { int32_t ret_value_ = (timer_id); DELETE_ROOT_SET(func_rootset); return ret_value_; } -} - - -void fbody_clearInterval(value_t self, int32_t _timerId) { - esp_timer_handle_t timer = timer_handlers[_timerId]; - esp_timer_stop(timer); - esp_timer_delete(timer); - timer_handlers[_timerId] = 0; -} - - -void fbody_clearTimeout(value_t self, int32_t _timerId) { - esp_timer_handle_t timer = timer_handlers[_timerId]; - esp_timer_delete(timer); - timer_handlers[_timerId] = 0; -} - -int32_t fbody_getTimeUs(value_t self) { - return (int32_t)esp_timer_get_time(); -} - - -struct func_body _setInterval = { fbody_setInterval, "(()vi)i" }; -struct func_body _setTimeout = { fbody_setTimeout, "(()vi)i" }; -struct func_body _clearInterval = { fbody_clearInterval, "(i)v" }; -struct func_body _clearTimeout = { fbody_clearTimeout, "(i)v" }; -struct func_body _getTimeUs = { fbody_getTimeUs, "(v)i" }; \ No newline at end of file diff --git a/modules/gpio/CMakeLists.txt b/modules/gpio/CMakeLists.txt new file mode 100644 index 0000000..d52448a --- /dev/null +++ b/modules/gpio/CMakeLists.txt @@ -0,0 +1,7 @@ +idf_component_register(SRCS "gpio.c" + INCLUDE_DIRS "include" + REQUIRES core driver + # WHOLE_ARCHIVE + ) + +target_compile_options(${COMPONENT_LIB} PRIVATE -mtext-section-literals) \ No newline at end of file diff --git a/modules/gpio/gpio-.c b/modules/gpio/gpio-.c new file mode 100644 index 0000000..09e6ea5 --- /dev/null +++ b/modules/gpio/gpio-.c @@ -0,0 +1,25 @@ +#include "c-runtime.h" +#include "driver/gpio.h" +#include "include/gpio.h" + +extern struct func_body _103112105111gpioOn; +extern CLASS_OBJECT(object_class, 1); +void bluescript_main0_103112105111(); +ROOT_SET_DECL(global_rootset0_103112105111, 0); + +void fbody_103112105111gpioOn(value_t self, int32_t _pin) { + ROOT_SET(func_rootset, 1) + func_rootset.values[0] = self; + { + gpio_set_direction(_pin, GPIO_MODE_OUTPUT); + gpio_set_level(_pin, 1); + } + DELETE_ROOT_SET(func_rootset) +} +struct func_body _103112105111gpioOn = { fbody_103112105111gpioOn, "(i)v" }; + +void bluescript_main0_103112105111() { + ROOT_SET_INIT(global_rootset0_103112105111, 0) + + +} diff --git a/modules/gpio/gpio.bs b/modules/gpio/gpio.bs new file mode 100644 index 0000000..72305df --- /dev/null +++ b/modules/gpio/gpio.bs @@ -0,0 +1,4 @@ +import type {integer} from "../../bs-utils" + + +export function gpioOn(pin: integer) {} \ No newline at end of file diff --git a/modules/gpio/gpio.c b/modules/gpio/gpio.c new file mode 100644 index 0000000..bdc2a6e --- /dev/null +++ b/modules/gpio/gpio.c @@ -0,0 +1,23 @@ + +#include +#include "../../microcontroller/core/include/c-runtime.h" +#include "../../microcontroller/core/include/profiler.h" +extern struct func_body _gpioOn; +extern CLASS_OBJECT(object_class, 1); +void bluescript_main0_(); +ROOT_SET_DECL(global_rootset0, 0); + +static void fbody_gpioOn(value_t self, int32_t _pin) { + ROOT_SET_N(func_rootset,1,VALUE_UNDEF) + func_rootset.values[0] = self; + { + } + DELETE_ROOT_SET(func_rootset) +} +struct func_body _gpioOn = { fbody_gpioOn, "(i)v" }; + +void bluescript_main0_() { + ROOT_SET_INIT(global_rootset0, 0) + + +} diff --git a/modules/gpio/include/gpio.h b/modules/gpio/include/gpio.h new file mode 100644 index 0000000..5eae92a --- /dev/null +++ b/modules/gpio/include/gpio.h @@ -0,0 +1,10 @@ +#ifndef __BS_GPIO__ +#define __BS_GPIO__ + +#include "c-runtime.h" + +void fbody_103112105111gpioOn(value_t self, int32_t _pin); +extern struct func_body _103112105111gpioOn; +void bluescript_main0_103112105111(); + +#endif /* __BS_GPIO__ */ diff --git a/modules/esp32/c/CMakeLists.txt b/modules/std/CMakeLists.txt similarity index 62% rename from modules/esp32/c/CMakeLists.txt rename to modules/std/CMakeLists.txt index ed9424c..9564d7c 100644 --- a/modules/esp32/c/CMakeLists.txt +++ b/modules/std/CMakeLists.txt @@ -1,8 +1,8 @@ idf_component_register( - SRCS "button.c" "timer.c" "display.c" "utils.c" "gpio.c" - INCLUDE_DIRS "include" + SRCS "std.c" + INCLUDE_DIRS "." REQUIRES core driver main LDFRAGMENTS linker.lf WHOLE_ARCHIVE) -target_compile_options(${COMPONENT_LIB} PRIVATE -mtext-section-literals) +target_compile_options(${COMPONENT_LIB} PRIVATE -mtext-section-literals) \ No newline at end of file diff --git a/modules/std/bsmodule.json b/modules/std/bsmodule.json new file mode 100644 index 0000000..e69de29 diff --git a/modules/esp32/c/linker.lf b/modules/std/linker.lf similarity index 89% rename from modules/esp32/c/linker.lf rename to modules/std/linker.lf index 9718487..e8d450c 100644 --- a/modules/esp32/c/linker.lf +++ b/modules/std/linker.lf @@ -21,4 +21,4 @@ entries: * (modules_text_default); modules_text -> flash_text KEEP() * (modules_data_default); - modules_data -> flash_rodata KEEP() + modules_data -> flash_rodata KEEP() \ No newline at end of file diff --git a/modules/esp32/bluescript/utils.ts b/modules/std/std.bs similarity index 85% rename from modules/esp32/bluescript/utils.ts rename to modules/std/std.bs index 72c655e..a378b96 100644 --- a/modules/esp32/bluescript/utils.ts +++ b/modules/std/std.bs @@ -1,4 +1,4 @@ -import type { integer, float } from "../../base"; +import type { integer, float } from "../../bs-utils"; export const print = (value: any) => { diff --git a/modules/esp32/c/utils.c b/modules/std/std.c similarity index 95% rename from modules/esp32/c/utils.c rename to modules/std/std.c index 63c45d0..0db42f0 100644 --- a/modules/esp32/c/utils.c +++ b/modules/std/std.c @@ -1,7 +1,7 @@ #include #include +#include "std.h" #include -#include "include/utils.h" #include "logger.h" #include "assert.h" @@ -63,4 +63,4 @@ struct func_body _fabs = { fbody_fabs, "(f)f" }; struct func_body _sqrt = { fbody_sqrt, "(f)f" }; struct func_body _print = { fbody_print, "(a)v" }; -struct func_body _randInt = {fbody_randInt, "(ii)i"}; +struct func_body _randInt = {fbody_randInt, "(ii)i"}; \ No newline at end of file diff --git a/modules/esp32/c/include/utils.h b/modules/std/std.h similarity index 78% rename from modules/esp32/c/include/utils.h rename to modules/std/std.h index 74e6559..f8f38c6 100644 --- a/modules/esp32/c/include/utils.h +++ b/modules/std/std.h @@ -1,9 +1,11 @@ -#ifndef __BS_PRINT__ -#define __BS_PRINT__ +#ifndef __BS_STD__ +#define __BS_STD__ #include #include "c-runtime.h" -#include "section.h" + +#define MD_SECTION_TEXT __attribute__((section(".modules_text"))) +#define MD_SECTION_DATA __attribute__((section(".modules_data"))) void MD_SECTION_TEXT fbody_print(value_t self, value_t _value); @@ -20,5 +22,4 @@ extern MD_SECTION_DATA struct func_body _abs; extern MD_SECTION_DATA struct func_body _fabs; extern MD_SECTION_DATA struct func_body _sqrt; -#endif /* __BS_PRINT__ */ - +#endif /* __BS_STD__ */ \ No newline at end of file diff --git a/notebook/src/hooks/repl-context.tsx b/notebook/src/hooks/repl-context.tsx index a4c5aef..0575aae 100644 --- a/notebook/src/hooks/repl-context.tsx +++ b/notebook/src/hooks/repl-context.tsx @@ -17,11 +17,12 @@ export type ReplContextT = { useFlash: boolean, iram: MemoryT, dram: MemoryT, + flash: MemoryT updateUseJIT: (useJIT: boolean) => void, - updateUseFlash: (useFlash: boolean) => Promise, + updateUseFlash: (useFlash: boolean) => void, setLatestCellCode: (code: string) => void, - reset: () => Promise, + resetStart: () => Promise, executeLatestCell: () => Promise, } @@ -36,11 +37,12 @@ export const ReplContext = createContext({ useFlash: false, iram: MemoryDummry, dram: MemoryDummry, + flash: MemoryDummry, updateUseJIT: (useJIT: boolean) => {}, - updateUseFlash: async (useFlash: boolean) => {}, + updateUseFlash: (useFlash: boolean) => {}, setLatestCellCode: (code: string) => {}, - reset: async () => {}, + resetStart: async () => {}, executeLatestCell: async () => {}, }); @@ -54,23 +56,23 @@ export default function ReplProvider({children}: {children: ReactNode}) { const [runtimeError, setRuntimeError] = useState([]) const iram = useMemory('IRAM') const dram = useMemory('DRAM') + const flash = useMemory('Flash') const bluetooth = useRef(new Bluetooth()) // To use these variables in callbacks const latestCellRef = useRef(latestCell) latestCellRef.current = latestCell - const iramRef = useRef(iram) - const dramRef = useRef(dram) - iramRef.current = iram - dramRef.current = dram + // const iramRef = useRef(iram) + // const dramRef = useRef(dram) + // iramRef.current = iram + // dramRef.current = dram - useEffect(() => { bluetooth.current.setNotificationHandler(onReceiveNotification); },[]) - - const reset = async () => { + + const resetStart = async () => { setReplState("loading") const bytecodeBuffer = new BytecodeBufferBuilder(MAX_MTU).reset().generate() try { @@ -82,25 +84,55 @@ export default function ReplProvider({children}: {children: ReactNode}) { } } + const onResetComplete = (meminfo: MemInfo) => { + network.reset(meminfo).then(() => { + setPostExecutionCells([]) + setOutput([]) + setRuntimeError([]) + setLatestCell({id: 0, code:'', state: 'user-writing'}) + setReplState("activated") + iram.actions.reset(meminfo.iram.address, meminfo.iram.size) + dram.actions.reset(meminfo.dram.address, meminfo.dram.size) + flash.actions.reset(meminfo.flash.address, meminfo.flash.size) + }).catch(e => { + // TODO: 要修正 + console.log(e) + window.alert(`Failed to reset: ${e.message}`) + }); + } + + const sendCompileResult = async (compileResult: network.CompileResult) => { + const bytecodeBuilder = new BytecodeBufferBuilder(MAX_MTU) + for (const update of compileResult.result) { + bytecodeBuilder.loadToRAM(update.iram.address, Buffer.from(update.iram.data, "hex")); + bytecodeBuilder.loadToRAM(update.dram.address, Buffer.from(update.dram.data, "hex")); + bytecodeBuilder.loadToFlash(update.flash.address, Buffer.from(update.flash.data, "hex")); + } + for (const update of compileResult.result) { + bytecodeBuilder.jump(update.entryPoint); + } + const bluetoothTime = await bluetooth.current.sendBuffers(bytecodeBuilder.generate()) + return bluetoothTime + } + + const setMemoryUpdates = (compileResult: network.CompileResult) => { + for (const update of compileResult.result) { + iram.actions.setUsedSegment(update.iram.address, Buffer.from(update.iram.data, "hex").length) + dram.actions.setUsedSegment(update.dram.address, Buffer.from(update.dram.data, "hex").length) + flash.actions.setUsedSegment(update.flash.address, Buffer.from(update.flash.data, "hex").length) + } + } + const executeLatestCell = async () => { + console.log('execute latest cell',latestCell.id) setLatestCell({...latestCell, compileError: '', state: 'compiling'}) try { - const compileResult = useJIT ? await network.compileWithProfiling(latestCell.code) : await network.compile(latestCell.code) - const iramBuffer = Buffer.from(compileResult.iram.data, "hex") - const dramBuffer = Buffer.from(compileResult.dram.data, "hex") - const flashBuffer = Buffer.from(compileResult.flash.data, "hex") - const bytecodeBuffer = - new BytecodeBufferBuilder(MAX_MTU) - .loadToRAM(compileResult.iram.address, iramBuffer) - .loadToRAM(compileResult.dram.address, dramBuffer) - .loadToFlash(compileResult.flash.address, flashBuffer) - .jump(compileResult.entryPoint) - .generate() + const compileResult = useJIT ? await network.compileWithProfiling(latestCell.code) : await network.compile(latestCell.code, useFlash) + console.log(compileResult) setLatestCell({...latestCell, compileError: '', state: 'sending'}) - iram.actions.setUsedSegment(compileResult.iram.address, iramBuffer.length) - dram.actions.setUsedSegment(compileResult.dram.address, dramBuffer.length) - const bluetoothTime = await bluetooth.current.sendBuffers(bytecodeBuffer) + const bluetoothTime = await sendCompileResult(compileResult) const compileTime = compileResult.compileTime + setMemoryUpdates(compileResult) setLatestCell({...latestCell, state: 'executing', time: {compile: compileTime, bluetooth: bluetoothTime}}) } catch (error: any) { if (error instanceof CompileError) { @@ -117,58 +149,31 @@ export default function ReplProvider({children}: {children: ReactNode}) { setLatestCell({...latestCell, code}) } - const updateUseFlash = async (useFlash: boolean) => { - setUseFlash(useFlash) - await reset() - } - - const onDeviceResetComplete = (meminfo: MemInfo) => { - network.reset(meminfo, useFlash).then(() => { - setPostExecutionCells([]) - setOutput([]) - setRuntimeError([]) - setLatestCell({id: 0, code:'', state: 'user-writing'}) - setReplState("activated") - iram.actions.reset(meminfo.iram.address, meminfo.iram.size) - dram.actions.reset(meminfo.dram.address, meminfo.dram.size) - }).catch(e => { - // TODO: 要修正 - console.log(e) - window.alert(`Failed to reset: ${e.message}`) - }); - } - const onExecutionComplete = (executionTime: number) => { if (latestCellRef.current.time !== undefined && latestCellRef.current.time?.execution === undefined) { latestCellRef.current.time.execution = executionTime latestCellRef.current.state = 'done' const nextCellId = latestCellRef.current.id + 1 - setPostExecutionCells((cells) => - [...cells, latestCellRef.current] - ) + const current = latestCellRef.current + setPostExecutionCells((cells) => [...cells, current]) setLatestCell({id: nextCellId, code: '', state: 'user-writing'}) } } - const jitCompile = (fid: number, paramtypes: string[]) => { - network.jitCompile(fid, paramtypes).then((compileResult) => { - console.log(compileResult) - const iramBuffer = Buffer.from(compileResult.iram.data, "hex") - const dramBuffer = Buffer.from(compileResult.dram.data, "hex") - iramRef.current.actions.setUsedSegment(compileResult.iram.address, iramBuffer.length) - dramRef.current.actions.setUsedSegment(compileResult.dram.address, dramBuffer.length) - const bytecodeBuffer = - new BytecodeBufferBuilder(MAX_MTU) - .loadToRAM(compileResult.iram.address, iramBuffer) - .loadToRAM(compileResult.dram.address, dramBuffer) - .loadToFlash(compileResult.flash.address, Buffer.from(compileResult.flash.data, "hex")) - .jump(compileResult.entryPoint) - .generate() - bluetooth.current.sendBuffers(bytecodeBuffer).then(() => console.log("JIT finish!")) - }) + const jitCompile = async (fid: number, paramtypes: string[]) => { + try { + const compileResult = await network.jitCompile(fid, paramtypes) + await sendCompileResult(compileResult) + setMemoryUpdates(compileResult) + console.log('JIT finish') + } catch (error: any) { + // TODO: 要修正 + console.log(error) + window.alert(`Failed to compile: ${error.message}`) + } } - const onReceiveNotification = (event: Event) => { + const onReceiveNotification = async (event: Event) => { // @ts-ignore const value = event.target.value as any; const parseResult = bytecodeParser(value); @@ -181,7 +186,7 @@ export default function ReplProvider({children}: {children: ReactNode}) { break; case BYTECODE.RESULT_MEMINFO: console.log(parseResult.meminfo) - onDeviceResetComplete(parseResult.meminfo) + onResetComplete(parseResult.meminfo) break; case BYTECODE.RESULT_EXECTIME: onExecutionComplete(parseResult.exectime) @@ -191,10 +196,21 @@ export default function ReplProvider({children}: {children: ReactNode}) { jitCompile(parseResult.fid, parseResult.paramtypes) break; } - } } + const updateUseFlash = (useFlash: boolean) => { + if (useFlash) + setUseJIT(false) + setUseFlash(useFlash) + } + + const updateUseJIT = (useJIT: boolean) => { + if (useJIT) + setUseFlash(false) + setUseJIT(useJIT) + } + return ( {children} diff --git a/notebook/src/hooks/use-memory.ts b/notebook/src/hooks/use-memory.ts index 1879ed3..669ac77 100644 --- a/notebook/src/hooks/use-memory.ts +++ b/notebook/src/hooks/use-memory.ts @@ -46,7 +46,7 @@ export function useMemory(memoryName: string): MemoryT { const setUsedSegment = (startAddress: number, size: number) => { const start = startAddress - address - setBuffer(buffer.map((v, i) => v || (i >= (start / UNIT_SIZE) && i < (start + size) / UNIT_SIZE))) + setBuffer(buffer => buffer.map((v, i) => v || (i >= (start / UNIT_SIZE) && i < (start + size) / UNIT_SIZE))) setUsedSize(used => used + size) } diff --git a/notebook/src/services/bluetooth.ts b/notebook/src/services/bluetooth.ts index d9e8aa8..e8a2649 100644 --- a/notebook/src/services/bluetooth.ts +++ b/notebook/src/services/bluetooth.ts @@ -31,7 +31,7 @@ export default class Bluetooth { return end - start } - public setNotificationHandler(handler: (event: Event) => void) { + public setNotificationHandler(handler: (event: Event) => Promise) { this.notificationHandler = handler; } diff --git a/notebook/src/services/network.ts b/notebook/src/services/network.ts index 5bf7d16..e1cae77 100644 --- a/notebook/src/services/network.ts +++ b/notebook/src/services/network.ts @@ -2,17 +2,20 @@ import axios from "axios"; import { CompileError, InternalError } from "../utils/error"; import { MemInfo } from "../utils/type"; +export type MemoryUpdate = { + iram: {address: number, data: string}, + dram: {address: number, data: string}, + flash: {address: number, data: string}, + entryPoint: number, +} export type CompileResult = { - iram: {address: number, data: string}, - dram: {address: number, data: string}, - flash: {address: number, data: string}, - entryPoint: number, + result: MemoryUpdate[], compileTime: number } -export async function compile(src: string): Promise { - return post("compile", {src}); +export async function compile(src: string, useFlash: boolean): Promise { + return post("compile", {src, useFlash}); } export async function compileWithProfiling(src: string): Promise { @@ -23,8 +26,8 @@ export async function jitCompile(funcId: number, paramTypes: string[]): Promise< return post("jit-compile", {funcId, paramTypes}); } -export async function reset(memInfo:MemInfo, useFlash:boolean) { - return post("reset", {...memInfo, useFlash}); +export async function reset(memInfo:MemInfo) { + return post("reset", memInfo); } async function post(path: string, body: object) { diff --git a/notebook/src/view/components/code-area.tsx b/notebook/src/view/components/code-area.tsx index 3cef8c3..37d2652 100644 --- a/notebook/src/view/components/code-area.tsx +++ b/notebook/src/view/components/code-area.tsx @@ -29,7 +29,7 @@ function InitialScreen() { return (
Click Here to Start Coding - +
) } @@ -66,7 +66,7 @@ function ButtonBar() { const replContext = useContext(ReplContext) return ( - + replContext.updateUseJIT(e.target.checked)} checked={replContext.useJIT}>Use JIT replContext.updateUseFlash(e.target.checked)} checked={replContext.useFlash}>Use Flash @@ -89,7 +89,7 @@ function Cell(props: { let CellButton = () => { if (state === 'user-writing') - return