From 21ac50c94a78c676b358b50efc405681f37e3d27 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Sun, 1 Oct 2023 16:36:22 -0700 Subject: [PATCH] starting lisp core --- stage2/include/lisp.h | 39 ++++++++++++++++++++++++ stage2/include/memory.h | 10 ++++++ stage2/include/neopixel.h | 8 +++++ stage2/src/lisp.cc | 5 +++ stage2/src/main.cc | 56 +++------------------------------- stage2/src/memory.cc | 17 +++++++++++ stage2/src/neopixel.cc | 64 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 147 insertions(+), 52 deletions(-) create mode 100644 stage2/include/lisp.h create mode 100644 stage2/include/memory.h create mode 100644 stage2/include/neopixel.h create mode 100644 stage2/src/lisp.cc create mode 100644 stage2/src/memory.cc create mode 100644 stage2/src/neopixel.cc diff --git a/stage2/include/lisp.h b/stage2/include/lisp.h new file mode 100644 index 0000000..05799aa --- /dev/null +++ b/stage2/include/lisp.h @@ -0,0 +1,39 @@ +#pragma once +#include + +#define SYMBOL_TABLE_SIZE 2048 + +typedef char * symbol_t; + +#define TYPE_SYMBOL 1 +#define TYPE_NUMBER 2 +#define TYPE_STRING 3 +#define TYPE_FLOAT 4 +#define TYPE_LIST 5 + + +#define symbolp(x) (x->type == TYPE_SYMBOL) +#define numberp(x) (x->type == TYPE_NUMBER) +#define stringp(x) (x->type == TYPE_STRING) +#define floatp(x) (x->type == TYPE_FLOAT) +#define listp(x) (x->type == TYPE_LIST) + + +typedef struct cell { + uint8_t type; + union { + struct { + cell *head; + cell *tail; + }; + struct { + union { + symbol_t sym; + int num; + char *str; + float fnum; + }; + }; + }; +} cell_t; + diff --git a/stage2/include/memory.h b/stage2/include/memory.h new file mode 100644 index 0000000..e84547b --- /dev/null +++ b/stage2/include/memory.h @@ -0,0 +1,10 @@ +#pragma once +#include + +// memory.h contains the memory management code for the system. +// right now, it uses the stdlib malloc/free directly. +// +// TODO(kyle): switch to arena memory. + +void *sys_malloc(size_t); +void sys_free(void *); \ No newline at end of file diff --git a/stage2/include/neopixel.h b/stage2/include/neopixel.h new file mode 100644 index 0000000..da34716 --- /dev/null +++ b/stage2/include/neopixel.h @@ -0,0 +1,8 @@ +#pragma once + +namespace neopxl { + +void start(); +bool blink(bool); + +} // namespace neopxl \ No newline at end of file diff --git a/stage2/src/lisp.cc b/stage2/src/lisp.cc new file mode 100644 index 0000000..3934216 --- /dev/null +++ b/stage2/src/lisp.cc @@ -0,0 +1,5 @@ +#include "lisp.h" + + +static char SymbolTable[SYMBOL_TABLE_SIZE]; + diff --git a/stage2/src/main.cc b/stage2/src/main.cc index c4cb49a..758aef2 100644 --- a/stage2/src/main.cc +++ b/stage2/src/main.cc @@ -1,9 +1,9 @@ -#include #include #include #include #include "colors.h" +#include "neopixel.h" #define TFT_CS 9 #define TFT_DC 10 @@ -12,20 +12,13 @@ Adafruit_ILI9341 tft(TFT_CS, TFT_DC); -Adafruit_NeoPixel pixels(1, 11, NEO_GRB + NEO_KHZ800); -auto cOff = pixels.Color(0, 0, 0); -auto cGreen = pixels.Color(0, 255, 0); -auto cRed = pixels.Color(255, 0, 0); -auto cBlue = pixels.Color(0, 0, 255); - void setup() { Serial.begin(115200); - pixels.begin(); - pixels.clear(); - pixels.show(); + + neopxl::start(); tft.begin(); tft.setRotation(1); @@ -42,52 +35,11 @@ setup() } -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; - } - - return on; -} - - -void -pixelOn(uint32_t c) -{ - pixels.setPixelColor(0, c); - pixels.show(); -} - - -void -pixelOff() -{ - pixels.setPixelColor(0, cOff); - pixels.show(); -} - - void loop() { static bool on = false; - on = blink(on); + on = neopxl::blink(on); delayMicroseconds(100); } diff --git a/stage2/src/memory.cc b/stage2/src/memory.cc new file mode 100644 index 0000000..d6e4bce --- /dev/null +++ b/stage2/src/memory.cc @@ -0,0 +1,17 @@ +#include "memory.h" +#include +#include + +void * +sys_malloc(size_t size) +{ + return std::malloc(size); +} + + +void +sys_free(void *ptr) +{ + return std::free(ptr); +} + diff --git a/stage2/src/neopixel.cc b/stage2/src/neopixel.cc new file mode 100644 index 0000000..a3ad26e --- /dev/null +++ b/stage2/src/neopixel.cc @@ -0,0 +1,64 @@ +#include "neopixel.h" +#include + +namespace neopxl { +Adafruit_NeoPixel pixels(1, 11, NEO_GRB + NEO_KHZ800); + +auto cOff = pixels.Color(0, 0, 0); +auto cGreen = pixels.Color(0, 255, 0); +auto cRed = pixels.Color(255, 0, 0); +auto cBlue = pixels.Color(0, 0, 255); + + +static void +pixelOn(uint32_t c) +{ + pixels.setPixelColor(0, c); + pixels.show(); +} + + +static void +pixelOff() +{ + pixels.setPixelColor(0, cOff); + pixels.show(); +} + + +void +start() +{ + pixels.begin(); + pixels.clear(); + pixels.show(); +} + + +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; + } + + return on; +} + + + +}; // namespace neopxl \ No newline at end of file