From 39b4e049b75a76d3b6a4a248fc43c7a4fb9ef91a Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Sun, 1 Oct 2023 18:00:25 -0700 Subject: [PATCH] make symbols and add nil --- stage2/include/lisp.h | 14 ++++++-- stage2/include/neopixel.h | 3 +- stage2/platformio.ini | 1 + stage2/src/lisp.cc | 75 +++++++++++++++++++++++++++++++++++++++ stage2/src/main.cc | 9 +++-- stage2/src/neopixel.cc | 55 +++++++++++++++++----------- 6 files changed, 130 insertions(+), 27 deletions(-) diff --git a/stage2/include/lisp.h b/stage2/include/lisp.h index 05799aa..3ea7e10 100644 --- a/stage2/include/lisp.h +++ b/stage2/include/lisp.h @@ -3,7 +3,7 @@ #define SYMBOL_TABLE_SIZE 2048 -typedef char * symbol_t; +typedef unsigned int symbol_t; #define TYPE_SYMBOL 1 #define TYPE_NUMBER 2 @@ -18,7 +18,6 @@ typedef char * symbol_t; #define floatp(x) (x->type == TYPE_FLOAT) #define listp(x) (x->type == TYPE_LIST) - typedef struct cell { uint8_t type; union { @@ -35,5 +34,14 @@ typedef struct cell { }; }; }; -} cell_t; +}; + +symbol_t intern_symbol(const char *s); +symbol_t make_symbol(const char *s); + +static auto NIL = make_symbol("nil"); + + +cell head(cell x); +cell tail(cell x); diff --git a/stage2/include/neopixel.h b/stage2/include/neopixel.h index da34716..8fbf84b 100644 --- a/stage2/include/neopixel.h +++ b/stage2/include/neopixel.h @@ -3,6 +3,7 @@ namespace neopxl { void start(); -bool blink(bool); +void blink(); +void stop(); } // namespace neopxl \ No newline at end of file diff --git a/stage2/platformio.ini b/stage2/platformio.ini index b462576..0edfe19 100644 --- a/stage2/platformio.ini +++ b/stage2/platformio.ini @@ -24,6 +24,7 @@ lib_deps = adafruit/RTClib @ ^1.12.4 arduino-libraries/SD @ ^1.2.4 arturo182/BBQ10Keyboard + kisom/kasl @ ^1.0.1 SPI Wire diff --git a/stage2/src/lisp.cc b/stage2/src/lisp.cc index 3934216..bbe4f5c 100644 --- a/stage2/src/lisp.cc +++ b/stage2/src/lisp.cc @@ -1,5 +1,80 @@ #include "lisp.h" +#include "memory.h" +#include "neopixel.h" +#include +#include + +#define MAX_SYMBOL_LEN 24 static char SymbolTable[SYMBOL_TABLE_SIZE]; +static cell CellTable[SYMBOL_TABLE_SIZE]; // TODO(kyle): consider adding + // cell-table specific param. + + +static void +Abort() +{ + neopxl::stop(); + abort(); +} + + +symbol_t +intern_symbol(const char *s) +{ + char *cursor = SymbolTable; + + if (!(std::strncmp(s, cursor+1, MAX_SYMBOL_LEN) == 0)) { + cursor += cursor[0]; + } + + // cursor has reached the end of the symbol table, which means + // we are out of room. + if (cursor >= SymbolTable+SYMBOL_TABLE_SIZE) { + Abort(); + } + + // cursor has reached the end of the symbol list, which means it + // should be added to the symbol table. + if (cursor[0] == 0) { + cursor[0] = static_cast(strnlen(s, MAX_SYMBOL_LEN)); + memcpy(cursor+1, s, static_cast(cursor[0])); + cursor[cursor[0]+1] = 0; + } + + // return the symbol pointed to by the cursor. + return (symbol_t)(cursor); +} + + +static cell * +FindEmptyCell() +{ + auto cursor = CellTable; + + while (cursor < (CellTable + SYMBOL_TABLE_SIZE)) { + if (cursor->type == 0) { + return cursor; + } + } + + return nullptr; +} + + + +symbol_t +make_symbol(const char *s) +{ + cell *cell = FindEmptyCell(); + if (cell == nullptr) { + Abort(); + } + + cell->type = TYPE_SYMBOL; + cell->sym = intern_symbol(s); + + return cell->sym; +} diff --git a/stage2/src/main.cc b/stage2/src/main.cc index 76b9b27..f887ac7 100644 --- a/stage2/src/main.cc +++ b/stage2/src/main.cc @@ -2,6 +2,8 @@ #include #include +#include + #include "display.h" #include "neopixel.h" @@ -13,7 +15,7 @@ setup() Serial.begin(115200); neopxl::start(); - tft::Setup; + tft::Setup(); tft::SetCursor(0, 8); @@ -21,6 +23,9 @@ setup() tft::Println(" \\ (square 3)"); tft::Println(" 9"); tft::Println(" \\ "); + + kasl::scheduleThread(neopxl::blink); + kasl::startScheduler(); } @@ -29,6 +34,6 @@ loop() { static bool on = false; - on = neopxl::blink(on); + kasl::runScheduler(); delayMicroseconds(100); } diff --git a/stage2/src/neopixel.cc b/stage2/src/neopixel.cc index a3ad26e..50c3cc2 100644 --- a/stage2/src/neopixel.cc +++ b/stage2/src/neopixel.cc @@ -35,30 +35,43 @@ start() } -bool -blink(bool on) -{ - if (on) { - for (int i = 10; i < 255; i++) { - auto c = pixels.Color(0, (uint8_t)i, 0); - pixels.setPixelColor(0, c); - pixels.show(); - delay(4); - } - on = false; - } else { - for (int i = 255; i > 9; i--) { - auto c = pixels.Color(0, (uint8_t)i, 0); - pixels.setPixelColor(0, c); - pixels.show(); - delay(4); - } - on = true; - } +static bool run = true; - return on; + +void +blink() +{ + static bool on = false; + + while (run) { + if (on) { + for (int i = 10; i < 255; i++) { + auto c = pixels.Color(0, (uint8_t)i, 0); + pixels.setPixelColor(0, c); + pixels.show(); + delay(4); + } + } else { + for (int i = 255; i > 9; i--) { + auto c = pixels.Color(0, (uint8_t)i, 0); + pixels.setPixelColor(0, c); + pixels.show(); + delay(4); + } + } + + on = !on; + } } +void +stop() +{ + run = false; + delay(980); + pixels.setPixelColor(0, cRed); + pixels.show(); +} }; // namespace neopxl \ No newline at end of file