diff --git a/simulation/Makefile b/simulation/Makefile new file mode 100644 index 0000000..07510f0 --- /dev/null +++ b/simulation/Makefile @@ -0,0 +1,65 @@ +CC=clang +CXX=clang++ + +CFLAGS= -O0 -g -Wall -Wextra -Wpedantic -Wno-gnu -mmacosx-version-min=10.9 -F/Library/Frameworks +CFLAGS += -DARDUINO=10808 -D__MK20DX256__ + +CXXFLAGS= $(CFLAGS) -std=c++14 + +LIBS=-framework SDL2 -lc++ -lc -framework OpenGL +LDFLAGS=-macosx_version_min 10.9 + +SYSINC = ~/Documents/Arduino/libraries/Filters ./include +INCS = ../NuEVI ./include + +INCDIRS = $(addprefix -isystem ,$(SYSINC)) +INCDIRS += $(addprefix -I,$(INCS)) + + +TARGET=nuevisim + +CXXFILES= ../NuEVI/menu.cpp \ + src/nuevisim.cpp \ + src/simeeprom.cpp \ + src/Print.cpp \ + src/simserial.cpp \ + src/simwire.cpp \ + src/simmidi.cpp \ + src/filters.cpp \ + src/Adafruit_GFX_sim.cpp \ + src/Adafruit_SSD1306_sim.cpp \ + src/Adafruit_MPR121_sim.cpp + +OBJS=$(CXXFILES:.cpp=.o) $(CFILES:.c=.o) + +all: $(TARGET) + +nuevisim: $(OBJS) + $(LD) $(LDFLAGS) -o $(TARGET) $(LIBS) $^ + +%.o: %.c + $(CC) $(CFLAGS) $(INCDIRS) -c -o $@ $< + +%.o: %.cpp + $(CXX) $(CXXFLAGS) $(INCDIRS) -c -o $@ $< + +clean: + rm -f $(TARGET) $(OBJS) *.o + +.PHONY: all + + +# Dependecies +DEPS=make.deps + +.PHONY: deps mrproper +mrproper: clean + rm $(DEPS) +deps: $(DEPS) + +H_DEPS=$(wildcard src/*.h) $(wildcard ../NuEVI/*.h) + +make.deps: $(CXXFILES) $(H_DEPS) + $(CXX) $(CXXFLAGS) -Wno-deprecated $(INCDIRS) -MM $(DEPS_HS) $^ > $@ + +-include .deps/* diff --git a/simulation/include/Adafruit_GFX.h b/simulation/include/Adafruit_GFX.h new file mode 100644 index 0000000..c08075c --- /dev/null +++ b/simulation/include/Adafruit_GFX.h @@ -0,0 +1,232 @@ +#ifndef _ADAFRUIT_GFX_H +#define _ADAFRUIT_GFX_H + +#if ARDUINO >= 100 + #include "Arduino.h" + #include "Print.h" +#else + #include "WProgram.h" +#endif +#include "gfxfont.h" + +/// A generic graphics superclass that can handle all sorts of drawing. At a minimum you can subclass and provide drawPixel(). At a maximum you can do a ton of overriding to optimize. Used for any/all Adafruit displays! +class Adafruit_GFX : public Print { + +public: // Exposed simulation variables... + bool dimmed_; + bool enabled_; + bool inverted_; + + public: + + Adafruit_GFX(int16_t w, int16_t h); // Constructor + + // This MUST be defined by the subclass: + virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0; ///< Virtual drawPixel() function to draw to the screen/framebuffer/etc, must be overridden in subclass. @param x X coordinate. @param y Y coordinate. @param color 16-bit pixel color. + + // TRANSACTION API / CORE DRAW API + // These MAY be overridden by the subclass to provide device-specific + // optimized code. Otherwise 'generic' versions are used. + virtual void startWrite(void); + virtual void writePixel(int16_t x, int16_t y, uint16_t color); + virtual void writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); + virtual void writeFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color); + virtual void writeFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color); + virtual void writeLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color); + virtual void endWrite(void); + + // CONTROL API + // These MAY be overridden by the subclass to provide device-specific + // optimized code. Otherwise 'generic' versions are used. + virtual void setRotation(uint8_t r); + virtual void invertDisplay(boolean i); + + // BASIC DRAW API + // These MAY be overridden by the subclass to provide device-specific + // optimized code. Otherwise 'generic' versions are used. + virtual void + // It's good to implement those, even if using transaction API + drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color), + drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color), + fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color), + fillScreen(uint16_t color), + // Optional and probably not necessary to change + drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color), + drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); + + // These exist only with Adafruit_GFX (no subclass overrides) + void + drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color), + drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, + uint16_t color), + fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color), + fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, + int16_t delta, uint16_t color), + drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, + int16_t x2, int16_t y2, uint16_t color), + fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, + int16_t x2, int16_t y2, uint16_t color), + drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, + int16_t radius, uint16_t color), + fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, + int16_t radius, uint16_t color), + drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], + int16_t w, int16_t h, uint16_t color), + drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], + int16_t w, int16_t h, uint16_t color, uint16_t bg), + drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, + int16_t w, int16_t h, uint16_t color), + drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, + int16_t w, int16_t h, uint16_t color, uint16_t bg), + drawXBitmap(int16_t x, int16_t y, const uint8_t bitmap[], + int16_t w, int16_t h, uint16_t color), + drawGrayscaleBitmap(int16_t x, int16_t y, const uint8_t bitmap[], + int16_t w, int16_t h), + drawGrayscaleBitmap(int16_t x, int16_t y, uint8_t *bitmap, + int16_t w, int16_t h), + drawGrayscaleBitmap(int16_t x, int16_t y, + const uint8_t bitmap[], const uint8_t mask[], + int16_t w, int16_t h), + drawGrayscaleBitmap(int16_t x, int16_t y, + uint8_t *bitmap, uint8_t *mask, int16_t w, int16_t h), + drawRGBBitmap(int16_t x, int16_t y, const uint16_t bitmap[], + int16_t w, int16_t h), + drawRGBBitmap(int16_t x, int16_t y, uint16_t *bitmap, + int16_t w, int16_t h), + drawRGBBitmap(int16_t x, int16_t y, + const uint16_t bitmap[], const uint8_t mask[], + int16_t w, int16_t h), + drawRGBBitmap(int16_t x, int16_t y, + uint16_t *bitmap, uint8_t *mask, int16_t w, int16_t h), + drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, + uint16_t bg, uint8_t size), + setCursor(int16_t x, int16_t y), + setTextColor(uint16_t c), + setTextColor(uint16_t c, uint16_t bg), + setTextSize(uint8_t s), + setTextWrap(boolean w), + cp437(boolean x=true), + setFont(const GFXfont *f = NULL), + getTextBounds(const char *string, int16_t x, int16_t y, + int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h), + getTextBounds(const __FlashStringHelper *s, int16_t x, int16_t y, + int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h), + getTextBounds(const String &str, int16_t x, int16_t y, + int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h); + + +#if ARDUINO >= 100 + virtual size_t write(uint8_t); +#else + virtual void write(uint8_t); +#endif + + int16_t height(void) const; + int16_t width(void) const; + + uint8_t getRotation(void) const; + + // get current cursor position (get rotation safe maximum values, using: width() for x, height() for y) + int16_t getCursorX(void) const; + int16_t getCursorY(void) const; + + protected: + void + charBounds(char c, int16_t *x, int16_t *y, + int16_t *minx, int16_t *miny, int16_t *maxx, int16_t *maxy); + const int16_t + WIDTH, ///< This is the 'raw' display width - never changes + HEIGHT; ///< This is the 'raw' display height - never changes + int16_t + _width, ///< Display width as modified by current rotation + _height, ///< Display height as modified by current rotation + cursor_x, ///< x location to start print()ing text + cursor_y; ///< y location to start print()ing text + uint16_t + textcolor, ///< 16-bit background color for print() + textbgcolor; ///< 16-bit text color for print() + uint8_t + textsize, ///< Desired magnification of text to print() + rotation; ///< Display rotation (0 thru 3) + boolean + wrap, ///< If set, 'wrap' text at right edge of display + _cp437; ///< If set, use correct CP437 charset (default is off) + GFXfont + *gfxFont; ///< Pointer to special font +}; + + +/// A simple drawn button UI element +class Adafruit_GFX_Button { + + public: + Adafruit_GFX_Button(void); + // "Classic" initButton() uses center & size + void initButton(Adafruit_GFX *gfx, int16_t x, int16_t y, + uint16_t w, uint16_t h, uint16_t outline, uint16_t fill, + uint16_t textcolor, char *label, uint8_t textsize); + // New/alt initButton() uses upper-left corner & size + void initButtonUL(Adafruit_GFX *gfx, int16_t x1, int16_t y1, + uint16_t w, uint16_t h, uint16_t outline, uint16_t fill, + uint16_t textcolor, char *label, uint8_t textsize); + void drawButton(boolean inverted = false); + boolean contains(int16_t x, int16_t y); + + void press(boolean p); + boolean isPressed(); + boolean justPressed(); + boolean justReleased(); + + private: + Adafruit_GFX *_gfx; + int16_t _x1, _y1; // Coordinates of top-left corner + uint16_t _w, _h; + uint8_t _textsize; + uint16_t _outlinecolor, _fillcolor, _textcolor; + char _label[10]; + + boolean currstate, laststate; +}; + + +/// A GFX 1-bit canvas context for graphics +class GFXcanvas1 : public Adafruit_GFX { + public: + GFXcanvas1(uint16_t w, uint16_t h); + ~GFXcanvas1(void); + void drawPixel(int16_t x, int16_t y, uint16_t color), + fillScreen(uint16_t color); + uint8_t *getBuffer(void); + private: + uint8_t *buffer; +}; + + +/// A GFX 8-bit canvas context for graphics +class GFXcanvas8 : public Adafruit_GFX { + public: + GFXcanvas8(uint16_t w, uint16_t h); + ~GFXcanvas8(void); + void drawPixel(int16_t x, int16_t y, uint16_t color), + fillScreen(uint16_t color), + writeFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color); + + uint8_t *getBuffer(void); + private: + uint8_t *buffer; +}; + + +/// A GFX 16-bit canvas context for graphics +class GFXcanvas16 : public Adafruit_GFX { + public: + GFXcanvas16(uint16_t w, uint16_t h); + ~GFXcanvas16(void); + void drawPixel(int16_t x, int16_t y, uint16_t color), + fillScreen(uint16_t color); + uint16_t *getBuffer(void); + private: + uint16_t *buffer; +}; + +#endif // _ADAFRUIT_GFX_H diff --git a/simulation/include/Adafruit_MPR121.h b/simulation/include/Adafruit_MPR121.h new file mode 100644 index 0000000..5b0e6fe --- /dev/null +++ b/simulation/include/Adafruit_MPR121.h @@ -0,0 +1,109 @@ +/*! + * @file Adafruit_MPR121.h + * + This is a library for the MPR121 12-Channel Capacitive Sensor + + Designed specifically to work with the MPR121 breakout from Adafruit + ----> https://www.adafruit.com/products/1982 + + These sensors use I2C to communicate, 2+ pins are required to + interface + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + BSD license, all text above must be included in any redistribution + * + */ + +#ifndef ADAFRUIT_MPR121_H +#define ADAFRUIT_MPR121_H + +// #if (ARDUINO >= 100) + #include "Arduino.h" +// #else +// #include "WProgram.h" +// #endif +#include + +// The default I2C address +#define MPR121_I2CADDR_DEFAULT 0x5A ///< default I2C address + +/** + ***************************************************************************************** + * @brief Device register map + ****************************************************************************************/ +enum { + MPR121_TOUCHSTATUS_L = 0x00, + MPR121_TOUCHSTATUS_H = 0x01, + MPR121_FILTDATA_0L = 0x04, + MPR121_FILTDATA_0H = 0x05, + MPR121_BASELINE_0 = 0x1E, + MPR121_MHDR = 0x2B, + MPR121_NHDR = 0x2C, + MPR121_NCLR = 0x2D, + MPR121_FDLR = 0x2E, + MPR121_MHDF = 0x2F, + MPR121_NHDF = 0x30, + MPR121_NCLF = 0x31, + MPR121_FDLF = 0x32, + MPR121_NHDT = 0x33, + MPR121_NCLT = 0x34, + MPR121_FDLT = 0x35, + + MPR121_TOUCHTH_0 = 0x41, + MPR121_RELEASETH_0 = 0x42, + MPR121_DEBOUNCE = 0x5B, + MPR121_CONFIG1 = 0x5C, + MPR121_CONFIG2 = 0x5D, + MPR121_CHARGECURR_0 = 0x5F, + MPR121_CHARGETIME_1 = 0x6C, + MPR121_ECR = 0x5E, + MPR121_AUTOCONFIG0 = 0x7B, + MPR121_AUTOCONFIG1 = 0x7C, + MPR121_UPLIMIT = 0x7D, + MPR121_LOWLIMIT = 0x7E, + MPR121_TARGETLIMIT = 0x7F, + + MPR121_GPIODIR = 0x76, + MPR121_GPIOEN = 0x77, + MPR121_GPIOSET = 0x78, + MPR121_GPIOCLR = 0x79, + MPR121_GPIOTOGGLE = 0x7A, + + MPR121_SOFTRESET = 0x80, +}; + +//.. thru to 0x1C/0x1D + +/**************************************************************************/ +/*! + @brief Class that stores state and functions for interacting with MPR121 + proximity capacitive touch sensor controller. +*/ +/**************************************************************************/ +class Adafruit_MPR121 { + public: + // Hardware I2C + Adafruit_MPR121(void); + + boolean begin(uint8_t i2caddr = MPR121_I2CADDR_DEFAULT); + + uint16_t filteredData(uint8_t t); + uint16_t baselineData(uint8_t t); + + uint8_t readRegister8(uint8_t reg); + uint16_t readRegister16(uint8_t reg); + void writeRegister(uint8_t reg, uint8_t value); + uint16_t touched(void); + // Add deprecated attribute so that the compiler shows a warning + void setThreshholds(uint8_t touch, uint8_t release) __attribute__((deprecated)); + void setThresholds(uint8_t touch, uint8_t release); + + private: + int8_t _i2caddr; + uint8_t _registers[24]; +}; + +#endif // ADAFRUIT_MPR121_H \ No newline at end of file diff --git a/simulation/include/Adafruit_SSD1306.h b/simulation/include/Adafruit_SSD1306.h new file mode 100644 index 0000000..9d48800 --- /dev/null +++ b/simulation/include/Adafruit_SSD1306.h @@ -0,0 +1,169 @@ +/*! + * @file Adafruit_SSD1306.h + * + * This is part of for Adafruit's SSD1306 library for monochrome + * OLED displays: http://www.adafruit.com/category/63_98 + * + * These displays use I2C or SPI to communicate. I2C requires 2 pins + * (SCL+SDA) and optionally a RESET pin. SPI requires 4 pins (MOSI, SCK, + * select, data/command) and optionally a reset pin. Hardware SPI or + * 'bitbang' software SPI are both supported. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * Written by Limor Fried/Ladyada for Adafruit Industries, with + * contributions from the open source community. + * + * BSD license, all text above, and the splash screen header file, + * must be included in any redistribution. + * + */ + +#ifndef _Adafruit_SSD1306_H_ +#define _Adafruit_SSD1306_H_ + +#include + +#include + +typedef void* SPIClass; + +// #if defined(__AVR__) +// typedef volatile uint8_t PortReg; +// typedef uint8_t PortMask; +// #define HAVE_PORTREG +// #elif defined(__SAM3X8E__) +// typedef volatile RwReg PortReg; +// typedef uint32_t PortMask; +// #define HAVE_PORTREG +// #elif defined(__arm__) || defined(ARDUINO_FEATHER52) +// typedef volatile uint32_t PortReg; +// typedef uint32_t PortMask; +// #define HAVE_PORTREG +// #endif + +#define BLACK 0 ///< Draw 'off' pixels +#define WHITE 1 ///< Draw 'on' pixels +#define INVERSE 2 ///< Invert pixels + +#define SSD1306_MEMORYMODE 0x20 ///< See datasheet +#define SSD1306_COLUMNADDR 0x21 ///< See datasheet +#define SSD1306_PAGEADDR 0x22 ///< See datasheet +#define SSD1306_SETCONTRAST 0x81 ///< See datasheet +#define SSD1306_CHARGEPUMP 0x8D ///< See datasheet +#define SSD1306_SEGREMAP 0xA0 ///< See datasheet +#define SSD1306_DISPLAYALLON_RESUME 0xA4 ///< See datasheet +#define SSD1306_DISPLAYALLON 0xA5 ///< Not currently used +#define SSD1306_NORMALDISPLAY 0xA6 ///< See datasheet +#define SSD1306_INVERTDISPLAY 0xA7 ///< See datasheet +#define SSD1306_SETMULTIPLEX 0xA8 ///< See datasheet +#define SSD1306_DISPLAYOFF 0xAE ///< See datasheet +#define SSD1306_DISPLAYON 0xAF ///< See datasheet +#define SSD1306_COMSCANINC 0xC0 ///< Not currently used +#define SSD1306_COMSCANDEC 0xC8 ///< See datasheet +#define SSD1306_SETDISPLAYOFFSET 0xD3 ///< See datasheet +#define SSD1306_SETDISPLAYCLOCKDIV 0xD5 ///< See datasheet +#define SSD1306_SETPRECHARGE 0xD9 ///< See datasheet +#define SSD1306_SETCOMPINS 0xDA ///< See datasheet +#define SSD1306_SETVCOMDETECT 0xDB ///< See datasheet + +#define SSD1306_SETLOWCOLUMN 0x00 ///< Not currently used +#define SSD1306_SETHIGHCOLUMN 0x10 ///< Not currently used +#define SSD1306_SETSTARTLINE 0x40 ///< See datasheet + +#define SSD1306_EXTERNALVCC 0x01 ///< External display voltage source +#define SSD1306_SWITCHCAPVCC 0x02 ///< Gen. display voltage from 3.3V + +#define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26 ///< Init rt scroll +#define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27 ///< Init left scroll +#define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29 ///< Init diag scroll +#define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A ///< Init diag scroll +#define SSD1306_DEACTIVATE_SCROLL 0x2E ///< Stop scroll +#define SSD1306_ACTIVATE_SCROLL 0x2F ///< Start scroll +#define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3 ///< Set scroll range + +// Deprecated size stuff for backwards compatibility with old sketches +#if defined SSD1306_128_64 + #define SSD1306_LCDWIDTH 128 ///< DEPRECATED: width w/SSD1306_128_64 defined + #define SSD1306_LCDHEIGHT 64 ///< DEPRECATED: height w/SSD1306_128_64 defined +#endif +#if defined SSD1306_128_32 + #define SSD1306_LCDWIDTH 128 ///< DEPRECATED: width w/SSD1306_128_32 defined + #define SSD1306_LCDHEIGHT 32 ///< DEPRECATED: height w/SSD1306_128_32 defined +#endif +#if defined SSD1306_96_16 + #define SSD1306_LCDWIDTH 96 ///< DEPRECATED: width w/SSD1306_96_16 defined + #define SSD1306_LCDHEIGHT 16 ///< DEPRECATED: height w/SSD1306_96_16 defined +#endif + +/*! + @brief Class that stores state and functions for interacting with + SSD1306 OLED displays. +*/ +class Adafruit_SSD1306 : public Adafruit_GFX { + public: + // NEW CONSTRUCTORS -- recommended for new projects + Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi=&Wire, int8_t rst_pin=-1, + uint32_t clkDuring=400000UL, uint32_t clkAfter=100000UL); + Adafruit_SSD1306(uint8_t w, uint8_t h, int8_t mosi_pin, int8_t sclk_pin, + int8_t dc_pin, int8_t rst_pin, int8_t cs_pin); + Adafruit_SSD1306(uint8_t w, uint8_t h, SPIClass *spi, + int8_t dc_pin, int8_t rst_pin, int8_t cs_pin, uint32_t bitrate=8000000UL); + + // DEPRECATED CONSTRUCTORS - for back compatibility, avoid in new projects + Adafruit_SSD1306(int8_t mosi_pin, int8_t sclk_pin, int8_t dc_pin, + int8_t rst_pin, int8_t cs_pin); + Adafruit_SSD1306(int8_t dc_pin, int8_t rst_pin, int8_t cs_pin); + Adafruit_SSD1306(int8_t rst_pin = -1); + + ~Adafruit_SSD1306(void); + + boolean begin(uint8_t switchvcc=SSD1306_SWITCHCAPVCC, + uint8_t i2caddr=0, boolean reset=true, + boolean periphBegin=true); + void display(void); + void clearDisplay(void); + void invertDisplay(boolean i); + void dim(boolean dim); + void drawPixel(int16_t x, int16_t y, uint16_t color); + virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color); + virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color); + void startscrollright(uint8_t start, uint8_t stop); + void startscrollleft(uint8_t start, uint8_t stop); + void startscrolldiagright(uint8_t start, uint8_t stop); + void startscrolldiagleft(uint8_t start, uint8_t stop); + void stopscroll(void); + void ssd1306_command(uint8_t c); + boolean getPixel(int16_t x, int16_t y); + uint8_t *getBuffer(void); + + private: + inline void SPIwrite(uint8_t d) __attribute__((always_inline)); + void drawFastHLineInternal(int16_t x, int16_t y, int16_t w, + uint16_t color); + void drawFastVLineInternal(int16_t x, int16_t y, int16_t h, + uint16_t color); + void ssd1306_command1(uint8_t c); + void ssd1306_commandList(const uint8_t *c, uint8_t n); + + SPIClass *spi; + TwoWire *wire; + uint8_t *buffer; + int8_t i2caddr, vccstate, page_end; + int8_t mosiPin , clkPin , dcPin , csPin, rstPin; +#ifdef HAVE_PORTREG + PortReg *mosiPort , *clkPort , *dcPort , *csPort; + PortMask mosiPinMask, clkPinMask, dcPinMask, csPinMask; +#endif +#if defined(SPI_HAS_TRANSACTION) + SPISettings spiSettings; +#endif +#if ARDUINO >= 157 + uint32_t wireClk; // Wire speed for SSD1306 transfers + uint32_t restoreClk; // Wire speed following SSD1306 transfers +#endif +}; + +#endif // _Adafruit_SSD1306_H_ diff --git a/simulation/include/Arduino.h b/simulation/include/Arduino.h new file mode 100644 index 0000000..b687cc2 --- /dev/null +++ b/simulation/include/Arduino.h @@ -0,0 +1,98 @@ +#ifndef __ARDUINO_H__ +#define __ARDUINO_H__ + +#include +#include +#include +#include +#include + +#include "Wiring.h" + +#include "core_pins.h" + +#define PROGMEM + +#ifndef _BV +#define _BV(x) (1u<<(x)) +#endif + +// SPI CTRL REG BITS +#define SPIE 7 +#define SPE 6 +#define DORD 5 +#define MSTR 4 +#define CPOL 3 +#define CPHA 2 +#define SPR1 1 +#define SPR0 0 + + +#define A11 (0xa1) + +/* +SPCR +| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | +| SPIE | SPE | DORD | MSTR | CPOL | CPHA | SPR1 | SPR0 | + +SPIE - Enables the SPI interrupt when 1 +SPE - Enables the SPI when 1 +DORD - Sends data least Significant Bit First when 1, most Significant Bit first when 0 +MSTR - Sets the Arduino in master mode when 1, slave mode when 0 +CPOL - Sets the data clock to be idle when high if set to 1, idle when low if set to 0 +CPHA - Samples data on the falling edge of the data clock when 1, rising edge when 0 +SPR1 and SPR0 - Sets the SPI speed, 00 is fastest (4MHz) 11 is slowest (250KHz) +*/ + + + +#define LSBFIRST 0 +#define MSBFIRST 1 + +typedef bool boolean; +typedef uint8_t byte; + +// class Print +// { +// public: +// }; + + +class SimSerial +{ +public: + void begin(uint32_t speed); + void println(uint32_t value); + void println(); + void println(const char* str); + void print(const char* str); + void print(uint32_t intValue); +}; + +extern SimSerial Serial; + +//extern void putString(int row, int col, int color, const char* msg, const FONT_INFO fontInfo); + +uint16_t micros(); +uint16_t millis(); +void delay(uint32_t millis); +void delayMicroseconds(uint32_t micros); + + +void pinMode(uint8_t, uint8_t); + +#ifdef __cplusplus +extern "C" { +#endif +//void digitalWrite(uint8_t, uint8_t); +//int digitalRead(uint8_t); + +#ifdef __cplusplus +} +#endif + +int analogRead(uint8_t); +void analogReference(uint8_t mode); +void analogWrite(uint8_t, int); + +#endif diff --git a/simulation/include/EEPROM.h b/simulation/include/EEPROM.h new file mode 100644 index 0000000..5e300db --- /dev/null +++ b/simulation/include/EEPROM.h @@ -0,0 +1,42 @@ +#ifndef __EEPROM_H +#define __EEPROM_H + +#include + +struct EEPROMClass +{ + //Basic user access methods. + // EERef operator[]( const int idx ) { return idx; } + uint8_t read( int idx ); // { return EERef( idx ); } + void write( int idx, uint8_t val ); // { (EERef( idx )) = val; } + void update( int idx, uint8_t val ); // { EERef( idx ).update( val ); } + + //STL and C++11 iteration capability. + // EEPtr begin() { return 0x00; } + // EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. + uint16_t length(); // { return E2END + 1; } + + //Functionality to 'get' and 'put' objects to and from EEPROM. +// template< typename T > T &get( int idx, T &t ){ +// EEPtr e = idx; +// uint8_t *ptr = (uint8_t*) &t; +// for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e; +// return t; +// } + +// template< typename T > const T &put( int idx, const T &t ){ +// const uint8_t *ptr = (const uint8_t*) &t; +// #ifdef __arm__ +// eeprom_write_block(ptr, (void *)idx, sizeof(T)); +// #else +// EEPtr e = idx; +// for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ ); +// #endif +// return t; +// } +}; + +static EEPROMClass EEPROM __attribute__ ((unused)); + + +#endif diff --git a/simulation/include/Print.h b/simulation/include/Print.h new file mode 100644 index 0000000..65b89e9 --- /dev/null +++ b/simulation/include/Print.h @@ -0,0 +1,136 @@ +/* Teensyduino Core Library + * http://www.pjrc.com/teensy/ + * Copyright (c) 2017 PJRC.COM, LLC. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * 1. The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * 2. If the Software is incorporated into a build system that allows + * selection among a list of target devices, then similar target + * devices manufactured by PJRC.COM must be included in the list of + * target devices and selectable in the same manner. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef Print_h +#define Print_h + +#include +#include // for size_t - gives sprintf and other stuff to all sketches & libs +#include +#include "core_id.h" +#include "WString.h" +#include "Printable.h" + +#define DEC 10 +#define HEX 16 +#define OCT 8 +#define BIN 2 + + +#ifndef isnan +#define isnan(__x) (__builtin_isnan (__x)) +#endif + +#ifndef isinf +#define isinf(__x) (__builtin_isinf (__x)) +#endif + + +class __FlashStringHelper; + +class Print +{ + public: + constexpr Print() : write_error(0) {} + virtual size_t write(uint8_t b) = 0; + size_t write(const char *str) { return write((const uint8_t *)str, strlen(str)); } + virtual size_t write(const uint8_t *buffer, size_t size); + virtual int availableForWrite(void) { return 0; } + virtual void flush() { } + size_t write(const char *buffer, size_t size) { return write((const uint8_t *)buffer, size); } +// size_t print(const String &s); + size_t print(char c) { return write((uint8_t)c); } + size_t print(const char s[]) { return write(s); } + size_t print(const __FlashStringHelper *f) { return write((const char *)f); } + + size_t print(uint8_t b) { return printNumber(b, 10, 0); } + size_t print(int n) { return print((long)n); } + size_t print(unsigned int n) { return printNumber(n, 10, 0); } + size_t print(long n); + size_t print(unsigned long n) { return printNumber(n, 10, 0); } + + size_t print(unsigned char n, int base) { return printNumber(n, base, 0); } + size_t print(int n, int base) { return (base == 10) ? print(n) : printNumber(n, base, 0); } + size_t print(unsigned int n, int base) { return printNumber(n, base, 0); } + size_t print(long n, int base) { return (base == 10) ? print(n) : printNumber(n, base, 0); } + size_t print(unsigned long n, int base) { return printNumber(n, base, 0); } + + size_t print(double n, int digits = 2) { return printFloat(n, digits); } + size_t print(const Printable &obj) { return obj.printTo(*this); } + size_t println(void); +// size_t println(const String &s) { return print(s) + println(); } + size_t println(char c) { return print(c) + println(); } + size_t println(const char s[]) { return print(s) + println(); } + size_t println(const __FlashStringHelper *f) { return print(f) + println(); } + + size_t println(uint8_t b) { return print(b) + println(); } + size_t println(int n) { return print(n) + println(); } + size_t println(unsigned int n) { return print(n) + println(); } + size_t println(long n) { return print(n) + println(); } + size_t println(unsigned long n) { return print(n) + println(); } + + size_t println(unsigned char n, int base) { return print(n, base) + println(); } + size_t println(int n, int base) { return print(n, base) + println(); } + size_t println(unsigned int n, int base) { return print(n, base) + println(); } + size_t println(long n, int base) { return print(n, base) + println(); } + size_t println(unsigned long n, int base) { return print(n, base) + println(); } + + size_t println(double n, int digits = 2) { return print(n, digits) + println(); } + size_t println(const Printable &obj) { return obj.printTo(*this) + println(); } + int getWriteError() { return write_error; } + void clearWriteError() { setWriteError(0); } + int printf(const char *format, ...); + int printf(const __FlashStringHelper *format, ...); + protected: + void setWriteError(int err = 1) { write_error = err; } + private: + char write_error; + size_t printFloat(double n, uint8_t digits); +#ifdef __MKL26Z64__ + size_t printNumberDec(unsigned long n, uint8_t sign); + size_t printNumberHex(unsigned long n); + size_t printNumberBin(unsigned long n); + size_t printNumberAny(unsigned long n, uint8_t base); + inline size_t printNumber(unsigned long n, uint8_t base, uint8_t sign) __attribute__((always_inline)) { + // when "base" is a constant (pretty much always), the + // compiler optimizes this to a single function call. + if (base == 0) return write((uint8_t)n); + if (base == 10 || base < 2) return printNumberDec(n, sign); + if (base == 16) return printNumberHex(n); + if (base == 2) return printNumberBin(n); + return printNumberAny(n, base); + } +#else + size_t printNumber(unsigned long n, uint8_t base, uint8_t sign); +#endif +}; + + +#endif diff --git a/simulation/include/Printable.h b/simulation/include/Printable.h new file mode 100644 index 0000000..0e96cdc --- /dev/null +++ b/simulation/include/Printable.h @@ -0,0 +1,42 @@ +/* + Printable.h - Interface class that allows printing of complex types + Copyright (c) 2011 Adrian McEwen. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef Printable_h +#define Printable_h + +#ifdef __cplusplus + +// #include "new.h" + +class Print; + +/** The Printable class provides a way for new classes to allow themselves to be printed. + By deriving from Printable and implementing the printTo method, it will then be possible + for users to print out instances of this class by passing them into the usual + Print::print and Print::println methods. +*/ +class Printable +{ + public: + virtual size_t printTo(Print& p) const = 0; +}; + + +#endif +#endif diff --git a/simulation/include/SPI.h b/simulation/include/SPI.h new file mode 100644 index 0000000..b0a1cc8 --- /dev/null +++ b/simulation/include/SPI.h @@ -0,0 +1,165 @@ +#ifndef __SPI_H__ +#define __SPI_H__ + +#include + + +#define SPIClass SimSPI + + +#ifndef SPI_MODE0 + #define SPI_MODE0 0 +#endif + +#ifndef SPI_CLOCK_DIV2 + #define SPI_CLOCK_DIV2 0 +#endif + +#define SPDR *SPI.SPI_dataReg +#define SPCR *SPI.SPI_ctrlReg +#define SPSR *SPI.SPI_statusReg + +//#define SPSR 0xff +#define SPIF (SPI.SPI_IF) + + +// Forward declare +class SimSPI; +class DataRegister; +class SPICtrlRegister; +class SPIStatusRegister; + + +extern SimSPI SPI; + + +class ISPIDevice +{ +public: + virtual uint8_t spiSlaveWrite( uint8_t ) = 0; +}; + + + +class SimSPI +{ +public: + SimSPI(); + + void AddDevice( ISPIDevice* device ); + + void begin(); + + void transfer( uint8_t something ); + + void setDataMode( uint8_t mode ); + void setClockDivider( uint32_t divider ); + void writeReg( DataRegister *reg, uint8_t data); + + // Members + DataRegister* SPI_dataReg; + SPICtrlRegister* SPI_ctrlReg; + SPIStatusRegister* SPI_statusReg; + + + uint8_t SPI_IF; + +private: + ISPIDevice** devices; + uint32_t devicesArraySize; + + ISPIDevice* currentSlave; + +}; + + +class HWRegister +{ +public: + HWRegister( SimSPI& owner) : spi_( owner ) {} +protected: + SimSPI& spi_; +}; + + +class DataRegister : HWRegister +{ +public: + DataRegister( SimSPI& owner) : HWRegister( owner ) {} + void operator=(uint8_t data) + { + spi_.writeReg( this, data ); + } + + operator uint8_t() + { + // TODO: Get last read byte + return 0xffu; + } +}; + + +class SPIStatusRegister : HWRegister +{ +public: + SPIStatusRegister( SimSPI& owner) : HWRegister( owner ) {} + void operator =(uint8_t value) + { + reg_ = value; + } + + void operator |=(uint8_t value) + { + reg_ |= value; + } + + void operator &=(uint8_t value) + { + reg_ &= value; + } +/* + operator uint8_t() + { + return reg_|0xff; + } +*/ + operator uint32_t() + { + return reg_|0xff; + } + + uint8_t reg_; +}; + +class SPICtrlRegister : HWRegister +{ +public: + SPICtrlRegister( SimSPI& owner) : HWRegister( owner ) {} + + void operator =(uint8_t value) + { + reg_ = value; + } + + void operator |=(uint8_t value) + { + reg_ |= value; + } + + void operator &=(uint8_t value) + { + reg_ &= value; + } + + + operator uint8_t() + { + return reg_; + } + + + uint8_t reg_; +}; + + +#endif diff --git a/simulation/include/WString.h b/simulation/include/WString.h new file mode 100644 index 0000000..608cd9e --- /dev/null +++ b/simulation/include/WString.h @@ -0,0 +1,227 @@ +/* + WString.h - String library for Wiring & Arduino + ...mostly rewritten by Paul Stoffregen... + Copyright (c) 2009-10 Hernando Barragan. All right reserved. + Copyright 2011, Paul Stoffregen, paul@pjrc.com + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef String_class_h +#define String_class_h +#ifdef __cplusplus + +#include +#include +#include +//#include "avr_functions.h" + +// Not needed here, but some libs assume WString.h or Print.h +// gives them PROGMEM and other AVR stuff. +//#include "avr/pgmspace.h" + +// When compiling programs with this class, the following gcc parameters +// dramatically increase performance and memory (RAM) efficiency, typically +// with little or no increase in code size. +// -felide-constructors +// -std=c++0x + +// Brian Cook's "no overhead" Flash String type (message on Dec 14, 2010) +// modified by Mikal Hart for his FlashString library +class __FlashStringHelper; +#ifndef F +#define F(string_literal) ((const __FlashStringHelper *)(string_literal)) +#endif + +// An inherited class for holding the result of a concatenation. These +// result objects are assumed to be writable by subsequent concatenations. +class StringSumHelper; + +// The string class +class String +{ +public: + // constructors + String(const char *cstr = (const char *)NULL); + String(const __FlashStringHelper *pgmstr); + String(const String &str); + #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) + String(String &&rval); + String(StringSumHelper &&rval); + #endif + String(char c); + String(unsigned char c); + String(int, unsigned char base=10); + String(unsigned int, unsigned char base=10); + String(long, unsigned char base=10); + String(unsigned long, unsigned char base=10); + String(float num, unsigned char digits=2); + String(double num, unsigned char digits=2) : String((float)num, digits) {} + ~String(void); + + // memory management + unsigned char reserve(unsigned int size); + inline unsigned int length(void) const {return len;} + + // copy and move + String & copy(const char *cstr, unsigned int length); + String & copy(const __FlashStringHelper *s) { return copy((const char *)s, strlen((const char *)s)); } + void move(String &rhs); + String & operator = (const String &rhs); + String & operator = (const char *cstr); + String & operator = (const __FlashStringHelper *pgmstr); + #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) + String & operator = (String &&rval); + String & operator = (StringSumHelper &&rval); + #endif + String & operator = (char c); + + // append + String & append(const String &str); + String & append(const char *cstr); + String & append(const __FlashStringHelper *s) {return append((const char *)s, strlen((const char *)s)); } + String & append(char c); + String & append(unsigned char c) {return append((int)c);} + String & append(int num); + String & append(unsigned int num); + String & append(long num); + String & append(unsigned long num); + String & append(float num); + String & append(double num) {return append((float)num);} + String & operator += (const String &rhs) {return append(rhs);} + String & operator += (const char *cstr) {return append(cstr);} + String & operator += (const __FlashStringHelper *pgmstr) {return append(pgmstr);} + String & operator += (char c) {return append(c);} + String & operator += (unsigned char c) {return append((int)c);} + String & operator += (int num) {return append(num);} + String & operator += (unsigned int num) {return append(num);} + String & operator += (long num) {return append(num);} + String & operator += (unsigned long num) {return append(num);} + String & operator += (float num) {return append(num);} + String & operator += (double num) {return append(num);} + + // concatenate + friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs); + friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr); + friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *pgmstr); + friend StringSumHelper & operator + (const StringSumHelper &lhs, char c); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char c); + friend StringSumHelper & operator + (const StringSumHelper &lhs, int num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, long num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, float num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, double num); + String & concat(const String &str) {return append(str);} + String & concat(const char *cstr) {return append(cstr);} + String & concat(const __FlashStringHelper *pgmstr) {return append(pgmstr);} + String & concat(char c) {return append(c);} + String & concat(unsigned char c) {return append((int)c);} + String & concat(int num) {return append(num);} + String & concat(unsigned int num) {return append(num);} + String & concat(long num) {return append(num);} + String & concat(unsigned long num) {return append(num);} + String & concat(float num) {return append(num);} + String & concat(double num) {return append(num);} + + // comparison + int compareTo(const String &s) const; + unsigned char equals(const String &s) const; + unsigned char equals(const char *cstr) const; + //unsigned char equals(const __FlashStringHelper *pgmstr) const; + unsigned char operator == (const String &rhs) const {return equals(rhs);} + unsigned char operator == (const char *cstr) const {return equals(cstr);} + unsigned char operator == (const __FlashStringHelper *s) const {return equals((const char *)s);} + unsigned char operator != (const String &rhs) const {return !equals(rhs);} + unsigned char operator != (const char *cstr) const {return !equals(cstr);} + unsigned char operator != (const __FlashStringHelper *s) const {return !equals(s);} + unsigned char operator < (const String &rhs) const; + unsigned char operator > (const String &rhs) const; + unsigned char operator <= (const String &rhs) const; + unsigned char operator >= (const String &rhs) const; + unsigned char equalsIgnoreCase(const String &s) const; + unsigned char startsWith( const String &prefix) const; + unsigned char startsWith(const String &prefix, unsigned int offset) const; + unsigned char endsWith(const String &suffix) const; + + // character acccess + char charAt(unsigned int index) const; + void setCharAt(unsigned int index, char c); + char operator [] (unsigned int index) const; + char& operator [] (unsigned int index); + void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const; + void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const + {getBytes((unsigned char *)buf, bufsize, index);} + const char * c_str() const { return buffer; } + + // search + int indexOf( char ch ) const; + int indexOf( char ch, unsigned int fromIndex ) const; + int indexOf( const String &str ) const; + int indexOf( const String &str, unsigned int fromIndex ) const; + int lastIndexOf( char ch ) const; + int lastIndexOf( char ch, unsigned int fromIndex ) const; + int lastIndexOf( const String &str ) const; + int lastIndexOf( const String &str, unsigned int fromIndex ) const; + String substring( unsigned int beginIndex ) const; + String substring( unsigned int beginIndex, unsigned int endIndex ) const; + + // modification + String & replace(char find, char replace); + String & replace(const String& find, const String& replace); + String & remove(unsigned int index); + String & remove(unsigned int index, unsigned int count); + String & toLowerCase(void); + String & toUpperCase(void); + String & trim(void); + + // parsing/conversion + long toInt(void) const; + float toFloat(void) const; + +protected: + char *buffer; // the actual char array + unsigned int capacity; // the array length minus one (for the '\0') + unsigned int len; // the String length (not counting the '\0') + unsigned char flags; // unused, for future features +protected: + void init(void); + unsigned char changeBuffer(unsigned int maxStrLen); + String & append(const char *cstr, unsigned int length); +private: + // allow for "if (s)" without the complications of an operator bool(). + // for more information http://www.artima.com/cppsource/safebool.html + typedef void (String::*StringIfHelperType)() const; + void StringIfHelper() const {} +public: + operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; } +}; + +class StringSumHelper : public String +{ +public: + StringSumHelper(const String &s) : String(s) {} + StringSumHelper(const char *p) : String(p) {} + StringSumHelper(const __FlashStringHelper *pgmstr) : String(pgmstr) {} + StringSumHelper(char c) : String(c) {} + StringSumHelper(unsigned char c) : String(c) {} + StringSumHelper(int num) : String(num, 10) {} + StringSumHelper(unsigned int num) : String(num, 10) {} + StringSumHelper(long num) : String(num, 10) {} + StringSumHelper(unsigned long num) : String(num, 10) {} +}; + +#endif // __cplusplus +#endif // String_class_h diff --git a/simulation/include/Wire.h b/simulation/include/Wire.h new file mode 100644 index 0000000..6b258a5 --- /dev/null +++ b/simulation/include/Wire.h @@ -0,0 +1,38 @@ +#ifndef __WIRE_H__ +#define __WIRE_H__ + +#include +#define TwoWire SimWire + +class SimWire +{ +public: + SimWire(bool verbose = false); + + void begin(); + void begin( uint8_t address ); // For slaves. + void end(); + void setClock(uint32_t); + void beginTransmission(uint8_t address); + void beginTransmission(int); + uint8_t endTransmission(); + uint8_t endTransmission(uint8_t); + uint8_t requestFrom(uint8_t address, uint8_t count); + uint8_t requestFrom(uint8_t, uint8_t, uint8_t); + uint8_t requestFrom(uint8_t, uint8_t, uint32_t, uint8_t, uint8_t); + size_t write(uint8_t); + size_t write(const uint8_t *, size_t); + int available(); + int read(); + int peek(); + void flush(); + void onReceive( void (*)(int) ); + void onRequest( void (*)(void) ); +private: + bool verbose_; +}; + +extern SimWire Wire; + + +#endif diff --git a/simulation/include/Wiring.h b/simulation/include/Wiring.h new file mode 100644 index 0000000..2873ee5 --- /dev/null +++ b/simulation/include/Wiring.h @@ -0,0 +1,197 @@ +/* Teensyduino Core Library + * http://www.pjrc.com/teensy/ + * Copyright (c) 2017 PJRC.COM, LLC. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * 1. The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * 2. If the Software is incorporated into a build system that allows + * selection among a list of target devices, then similar target + * devices manufactured by PJRC.COM must be included in the list of + * target devices and selectable in the same manner. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef Wiring_h +#define Wiring_h + +#include +#include +// #include "binary.h" +#include "core_id.h" +// #include "core_pins.h" + +// type_traits interferes with min() and other defines +// include it early, so we can define these later +// for Arduino compatibility +#ifdef __cplusplus +#include +// when the input number is an integer type, do all math as 32 bit signed long +template +long map(T _x, A _in_min, B _in_max, C _out_min, D _out_max, typename std::enable_if::value >::type* = 0) +{ + long x = _x, in_min = _in_min, in_max = _in_max, out_min = _out_min, out_max = _out_max; + // Arduino's traditional algorithm + //return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; + // st42's suggestion: https://github.com/arduino/Arduino/issues/2466#issuecomment-69873889 + // more conversation: + // https://forum.pjrc.com/threads/44503-map()-function-improvements + if ((in_max - in_min) > (out_max - out_min)) { + return (x - in_min) * (out_max - out_min+1) / (in_max - in_min+1) + out_min; + } else { + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; + } +} +// when the input is a float or double, do all math using the input's type +template +T map(T x, A in_min, B in_max, C out_min, D out_max, typename std::enable_if::value >::type* = 0) +{ + return (x - (T)in_min) * ((T)out_max - (T)out_min) / ((T)in_max - (T)in_min) + (T)out_min; +} +//#include // this isn't really needed, is it? (slows down compiling) +#include +// https://forum.pjrc.com/threads/44596-Teensyduino-1-37-Beta-2-(Arduino-1-8-3-support)?p=145150&viewfull=1#post145150 +template +constexpr auto min(A&& a, B&& b) -> decltype(a < b ? std::forward(a) : std::forward(b)) { + return a < b ? std::forward(a) : std::forward(b); +} +template +constexpr auto max(A&& a, B&& b) -> decltype(a < b ? std::forward(a) : std::forward(b)) { + return a >= b ? std::forward(a) : std::forward(b); +} +#else // not C++ +#define min(a, b) ({ \ + typeof(a) _a = (a); \ + typeof(b) _b = (b); \ + (_a < _b) ? _a : _b; \ +}) +#define max(a, b) ({ \ + typeof(a) _a = (a); \ + typeof(b) _b = (b); \ + (_a > _b) ? _a : _b; \ +}) +#endif + + +#ifdef PI +#undef PI +#endif +#define PI 3.1415926535897932384626433832795 +#define HALF_PI 1.5707963267948966192313216916398 +#define TWO_PI 6.283185307179586476925286766559 +#define DEG_TO_RAD 0.017453292519943295769236907684886 +#define RAD_TO_DEG 57.295779513082320876798154814105 + +#ifndef M_PI +#define M_PI 3.1415926535897932384626433832795 +#endif +#ifndef M_SQRT2 +#define M_SQRT2 1.4142135623730950488016887 +#endif + +#define SERIAL 0 +#define DISPLAY 1 + +// undefine stdlib's abs if encountered +#ifdef abs +#undef abs +#endif + +#if __cplusplus >= 201103L && defined(__STRICT_ANSI__) +#define typeof(a) decltype(a) +#endif + +#define abs(x) ({ \ + typeof(x) _x = (x); \ + (_x > 0) ? _x : -_x; \ +}) +#define constrain(amt, low, high) ({ \ + typeof(amt) _amt = (amt); \ + typeof(low) _low = (low); \ + typeof(high) _high = (high); \ + (_amt < _low) ? _low : ((_amt > _high) ? _high : _amt); \ +}) +#define round(x) ({ \ + typeof(x) _x = (x); \ + (_x>=0) ? (long)(_x+0.5) : (long)(_x-0.5); \ +}) +#define radians(deg) ((deg)*DEG_TO_RAD) +#define degrees(rad) ((rad)*RAD_TO_DEG) +#define sq(x) ({ \ + typeof(x) _x = (x); \ + _x * _x; \ +}) + +#ifdef __cplusplus +extern "C"{ +#endif + +extern double exp10(double x); +extern float exp10f(float x); +extern long double exp10l(long double x); +extern double pow10(double x); +extern float pow10f(float x); +extern long double pow10l(long double x); + +#define stricmp(a, b) strcasecmp(a, b) + +#define sei() __enable_irq() +#define cli() __disable_irq() +#define interrupts() __enable_irq() +#define noInterrupts() __disable_irq() + +#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L ) +#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() ) +#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() ) + +#define lowByte(w) ((uint8_t)((w) & 0xFF)) +#define highByte(w) ((uint8_t)((w) >> 8)) + +#define bitRead(value, bit) (((value) >> (bit)) & 0x01) +#define bitSet(value, bit) ((value) |= (1UL << (bit))) +#define bitClear(value, bit) ((value) &= ~(1UL << (bit))) +#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit))) + +typedef unsigned int word; + +#define bit(b) (1UL << (b)) + +typedef uint8_t byte; + +uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout); +void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, byte val); + +void setup(void); +void loop(void); + +#ifdef __cplusplus +} // extern "C" +#endif + +// fix C++ boolean issue +// https://github.com/arduino/Arduino/pull/2151 +#ifdef __cplusplus +typedef bool boolean; +#else +typedef uint8_t boolean; +#define false 0 +#define true (!false) +#endif + +#endif diff --git a/simulation/include/binary.h b/simulation/include/binary.h new file mode 100644 index 0000000..68e747a --- /dev/null +++ b/simulation/include/binary.h @@ -0,0 +1,517 @@ +// This header file is in the public domain. + +#ifndef Binary_h +#define Binary_h + +#define B0 0 +#define B00 0 +#define B000 0 +#define B0000 0 +#define B00000 0 +#define B000000 0 +#define B0000000 0 +#define B00000000 0 +#define B1 1 +#define B01 1 +#define B001 1 +#define B0001 1 +#define B00001 1 +#define B000001 1 +#define B0000001 1 +#define B00000001 1 +#define B10 2 +#define B010 2 +#define B0010 2 +#define B00010 2 +#define B000010 2 +#define B0000010 2 +#define B00000010 2 +#define B11 3 +#define B011 3 +#define B0011 3 +#define B00011 3 +#define B000011 3 +#define B0000011 3 +#define B00000011 3 +#define B100 4 +#define B0100 4 +#define B00100 4 +#define B000100 4 +#define B0000100 4 +#define B00000100 4 +#define B101 5 +#define B0101 5 +#define B00101 5 +#define B000101 5 +#define B0000101 5 +#define B00000101 5 +#define B110 6 +#define B0110 6 +#define B00110 6 +#define B000110 6 +#define B0000110 6 +#define B00000110 6 +#define B111 7 +#define B0111 7 +#define B00111 7 +#define B000111 7 +#define B0000111 7 +#define B00000111 7 +#define B1000 8 +#define B01000 8 +#define B001000 8 +#define B0001000 8 +#define B00001000 8 +#define B1001 9 +#define B01001 9 +#define B001001 9 +#define B0001001 9 +#define B00001001 9 +#define B1010 10 +#define B01010 10 +#define B001010 10 +#define B0001010 10 +#define B00001010 10 +#define B1011 11 +#define B01011 11 +#define B001011 11 +#define B0001011 11 +#define B00001011 11 +#define B1100 12 +#define B01100 12 +#define B001100 12 +#define B0001100 12 +#define B00001100 12 +#define B1101 13 +#define B01101 13 +#define B001101 13 +#define B0001101 13 +#define B00001101 13 +#define B1110 14 +#define B01110 14 +#define B001110 14 +#define B0001110 14 +#define B00001110 14 +#define B1111 15 +#define B01111 15 +#define B001111 15 +#define B0001111 15 +#define B00001111 15 +#define B10000 16 +#define B010000 16 +#define B0010000 16 +#define B00010000 16 +#define B10001 17 +#define B010001 17 +#define B0010001 17 +#define B00010001 17 +#define B10010 18 +#define B010010 18 +#define B0010010 18 +#define B00010010 18 +#define B10011 19 +#define B010011 19 +#define B0010011 19 +#define B00010011 19 +#define B10100 20 +#define B010100 20 +#define B0010100 20 +#define B00010100 20 +#define B10101 21 +#define B010101 21 +#define B0010101 21 +#define B00010101 21 +#define B10110 22 +#define B010110 22 +#define B0010110 22 +#define B00010110 22 +#define B10111 23 +#define B010111 23 +#define B0010111 23 +#define B00010111 23 +#define B11000 24 +#define B011000 24 +#define B0011000 24 +#define B00011000 24 +#define B11001 25 +#define B011001 25 +#define B0011001 25 +#define B00011001 25 +#define B11010 26 +#define B011010 26 +#define B0011010 26 +#define B00011010 26 +#define B11011 27 +#define B011011 27 +#define B0011011 27 +#define B00011011 27 +#define B11100 28 +#define B011100 28 +#define B0011100 28 +#define B00011100 28 +#define B11101 29 +#define B011101 29 +#define B0011101 29 +#define B00011101 29 +#define B11110 30 +#define B011110 30 +#define B0011110 30 +#define B00011110 30 +#define B11111 31 +#define B011111 31 +#define B0011111 31 +#define B00011111 31 +#define B100000 32 +#define B0100000 32 +#define B00100000 32 +#define B100001 33 +#define B0100001 33 +#define B00100001 33 +#define B100010 34 +#define B0100010 34 +#define B00100010 34 +#define B100011 35 +#define B0100011 35 +#define B00100011 35 +#define B100100 36 +#define B0100100 36 +#define B00100100 36 +#define B100101 37 +#define B0100101 37 +#define B00100101 37 +#define B100110 38 +#define B0100110 38 +#define B00100110 38 +#define B100111 39 +#define B0100111 39 +#define B00100111 39 +#define B101000 40 +#define B0101000 40 +#define B00101000 40 +#define B101001 41 +#define B0101001 41 +#define B00101001 41 +#define B101010 42 +#define B0101010 42 +#define B00101010 42 +#define B101011 43 +#define B0101011 43 +#define B00101011 43 +#define B101100 44 +#define B0101100 44 +#define B00101100 44 +#define B101101 45 +#define B0101101 45 +#define B00101101 45 +#define B101110 46 +#define B0101110 46 +#define B00101110 46 +#define B101111 47 +#define B0101111 47 +#define B00101111 47 +#define B110000 48 +#define B0110000 48 +#define B00110000 48 +#define B110001 49 +#define B0110001 49 +#define B00110001 49 +#define B110010 50 +#define B0110010 50 +#define B00110010 50 +#define B110011 51 +#define B0110011 51 +#define B00110011 51 +#define B110100 52 +#define B0110100 52 +#define B00110100 52 +#define B110101 53 +#define B0110101 53 +#define B00110101 53 +#define B110110 54 +#define B0110110 54 +#define B00110110 54 +#define B110111 55 +#define B0110111 55 +#define B00110111 55 +#define B111000 56 +#define B0111000 56 +#define B00111000 56 +#define B111001 57 +#define B0111001 57 +#define B00111001 57 +#define B111010 58 +#define B0111010 58 +#define B00111010 58 +#define B111011 59 +#define B0111011 59 +#define B00111011 59 +#define B111100 60 +#define B0111100 60 +#define B00111100 60 +#define B111101 61 +#define B0111101 61 +#define B00111101 61 +#define B111110 62 +#define B0111110 62 +#define B00111110 62 +#define B111111 63 +#define B0111111 63 +#define B00111111 63 +#define B1000000 64 +#define B01000000 64 +#define B1000001 65 +#define B01000001 65 +#define B1000010 66 +#define B01000010 66 +#define B1000011 67 +#define B01000011 67 +#define B1000100 68 +#define B01000100 68 +#define B1000101 69 +#define B01000101 69 +#define B1000110 70 +#define B01000110 70 +#define B1000111 71 +#define B01000111 71 +#define B1001000 72 +#define B01001000 72 +#define B1001001 73 +#define B01001001 73 +#define B1001010 74 +#define B01001010 74 +#define B1001011 75 +#define B01001011 75 +#define B1001100 76 +#define B01001100 76 +#define B1001101 77 +#define B01001101 77 +#define B1001110 78 +#define B01001110 78 +#define B1001111 79 +#define B01001111 79 +#define B1010000 80 +#define B01010000 80 +#define B1010001 81 +#define B01010001 81 +#define B1010010 82 +#define B01010010 82 +#define B1010011 83 +#define B01010011 83 +#define B1010100 84 +#define B01010100 84 +#define B1010101 85 +#define B01010101 85 +#define B1010110 86 +#define B01010110 86 +#define B1010111 87 +#define B01010111 87 +#define B1011000 88 +#define B01011000 88 +#define B1011001 89 +#define B01011001 89 +#define B1011010 90 +#define B01011010 90 +#define B1011011 91 +#define B01011011 91 +#define B1011100 92 +#define B01011100 92 +#define B1011101 93 +#define B01011101 93 +#define B1011110 94 +#define B01011110 94 +#define B1011111 95 +#define B01011111 95 +#define B1100000 96 +#define B01100000 96 +#define B1100001 97 +#define B01100001 97 +#define B1100010 98 +#define B01100010 98 +#define B1100011 99 +#define B01100011 99 +#define B1100100 100 +#define B01100100 100 +#define B1100101 101 +#define B01100101 101 +#define B1100110 102 +#define B01100110 102 +#define B1100111 103 +#define B01100111 103 +#define B1101000 104 +#define B01101000 104 +#define B1101001 105 +#define B01101001 105 +#define B1101010 106 +#define B01101010 106 +#define B1101011 107 +#define B01101011 107 +#define B1101100 108 +#define B01101100 108 +#define B1101101 109 +#define B01101101 109 +#define B1101110 110 +#define B01101110 110 +#define B1101111 111 +#define B01101111 111 +#define B1110000 112 +#define B01110000 112 +#define B1110001 113 +#define B01110001 113 +#define B1110010 114 +#define B01110010 114 +#define B1110011 115 +#define B01110011 115 +#define B1110100 116 +#define B01110100 116 +#define B1110101 117 +#define B01110101 117 +#define B1110110 118 +#define B01110110 118 +#define B1110111 119 +#define B01110111 119 +#define B1111000 120 +#define B01111000 120 +#define B1111001 121 +#define B01111001 121 +#define B1111010 122 +#define B01111010 122 +#define B1111011 123 +#define B01111011 123 +#define B1111100 124 +#define B01111100 124 +#define B1111101 125 +#define B01111101 125 +#define B1111110 126 +#define B01111110 126 +#define B1111111 127 +#define B01111111 127 +#define B10000000 128 +#define B10000001 129 +#define B10000010 130 +#define B10000011 131 +#define B10000100 132 +#define B10000101 133 +#define B10000110 134 +#define B10000111 135 +#define B10001000 136 +#define B10001001 137 +#define B10001010 138 +#define B10001011 139 +#define B10001100 140 +#define B10001101 141 +#define B10001110 142 +#define B10001111 143 +#define B10010000 144 +#define B10010001 145 +#define B10010010 146 +#define B10010011 147 +#define B10010100 148 +#define B10010101 149 +#define B10010110 150 +#define B10010111 151 +#define B10011000 152 +#define B10011001 153 +#define B10011010 154 +#define B10011011 155 +#define B10011100 156 +#define B10011101 157 +#define B10011110 158 +#define B10011111 159 +#define B10100000 160 +#define B10100001 161 +#define B10100010 162 +#define B10100011 163 +#define B10100100 164 +#define B10100101 165 +#define B10100110 166 +#define B10100111 167 +#define B10101000 168 +#define B10101001 169 +#define B10101010 170 +#define B10101011 171 +#define B10101100 172 +#define B10101101 173 +#define B10101110 174 +#define B10101111 175 +#define B10110000 176 +#define B10110001 177 +#define B10110010 178 +#define B10110011 179 +#define B10110100 180 +#define B10110101 181 +#define B10110110 182 +#define B10110111 183 +#define B10111000 184 +#define B10111001 185 +#define B10111010 186 +#define B10111011 187 +#define B10111100 188 +#define B10111101 189 +#define B10111110 190 +#define B10111111 191 +#define B11000000 192 +#define B11000001 193 +#define B11000010 194 +#define B11000011 195 +#define B11000100 196 +#define B11000101 197 +#define B11000110 198 +#define B11000111 199 +#define B11001000 200 +#define B11001001 201 +#define B11001010 202 +#define B11001011 203 +#define B11001100 204 +#define B11001101 205 +#define B11001110 206 +#define B11001111 207 +#define B11010000 208 +#define B11010001 209 +#define B11010010 210 +#define B11010011 211 +#define B11010100 212 +#define B11010101 213 +#define B11010110 214 +#define B11010111 215 +#define B11011000 216 +#define B11011001 217 +#define B11011010 218 +#define B11011011 219 +#define B11011100 220 +#define B11011101 221 +#define B11011110 222 +#define B11011111 223 +#define B11100000 224 +#define B11100001 225 +#define B11100010 226 +#define B11100011 227 +#define B11100100 228 +#define B11100101 229 +#define B11100110 230 +#define B11100111 231 +#define B11101000 232 +#define B11101001 233 +#define B11101010 234 +#define B11101011 235 +#define B11101100 236 +#define B11101101 237 +#define B11101110 238 +#define B11101111 239 +#define B11110000 240 +#define B11110001 241 +#define B11110010 242 +#define B11110011 243 +#define B11110100 244 +#define B11110101 245 +#define B11110110 246 +#define B11110111 247 +#define B11111000 248 +#define B11111001 249 +#define B11111010 250 +#define B11111011 251 +#define B11111100 252 +#define B11111101 253 +#define B11111110 254 +#define B11111111 255 + +#endif diff --git a/simulation/include/core_id.h b/simulation/include/core_id.h new file mode 100644 index 0000000..9a8881e --- /dev/null +++ b/simulation/include/core_id.h @@ -0,0 +1,5 @@ +// This header file is in the public domain. + +#ifndef CORE_TEENSY +#define CORE_TEENSY +#endif diff --git a/simulation/include/core_pins.h b/simulation/include/core_pins.h new file mode 100644 index 0000000..94afb53 --- /dev/null +++ b/simulation/include/core_pins.h @@ -0,0 +1,2115 @@ +/* Teensyduino Core Library + * http://www.pjrc.com/teensy/ + * Copyright (c) 2017 PJRC.COM, LLC. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * 1. The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * 2. If the Software is incorporated into a build system that allows + * selection among a list of target devices, then similar target + * devices manufactured by PJRC.COM must be included in the list of + * target devices and selectable in the same manner. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef _core_pins_h_ +#define _core_pins_h_ + +// #include "kinetis.h" +#include "pins_arduino.h" + +#define HIGH 1 +#define LOW 0 +#define INPUT 0 +#define OUTPUT 1 +#define INPUT_PULLUP 2 +#define INPUT_PULLDOWN 3 +#define OUTPUT_OPENDRAIN 4 +#define INPUT_DISABLE 5 +#define LSBFIRST 0 +#define MSBFIRST 1 + +#ifndef _BV +#define _BV(n) (1<<(n)) +#endif + +#define CHANGE 4 +#define FALLING 2 +#define RISING 3 + +// Pin Arduino +// 0 B16 RXD +// 1 B17 TXD +// 2 D0 +// 3 A12 FTM1_CH0 +// 4 A13 FTM1_CH1 +// 5 D7 FTM0_CH7 OC0B/T1 +// 6 D4 FTM0_CH4 OC0A +// 7 D2 +// 8 D3 ICP1 +// 9 C3 FTM0_CH2 OC1A +// 10 C4 FTM0_CH3 SS/OC1B +// 11 C6 MOSI/OC2A +// 12 C7 MISO +// 13 C5 SCK +// 14 D1 +// 15 C0 +// 16 B0 (FTM1_CH0) +// 17 B1 (FTM1_CH1) +// 18 B3 SDA +// 19 B2 SCL +// 20 D5 FTM0_CH5 +// 21 D6 FTM0_CH6 +// 22 C1 FTM0_CH0 +// 23 C2 FTM0_CH1 +// 24 A5 (FTM0_CH2) +// 25 B19 +// 26 E1 +// 27 C9 +// 28 C8 +// 29 C10 +// 30 C11 +// 31 E0 +// 32 B18 +// 33 A4 (FTM0_CH1) +// (34) analog only +// (35) analog only +// (36) analog only +// (37) analog only + +// not available to user: +// A0 FTM0_CH5 SWD Clock +// A1 FTM0_CH6 USB ID +// A2 FTM0_CH7 SWD Trace +// A3 FTM0_CH0 SWD Data + +#if defined(__MK20DX128__) +#define CORE_NUM_TOTAL_PINS 34 +#define CORE_NUM_DIGITAL 34 +#define CORE_NUM_INTERRUPT 34 +#define CORE_NUM_ANALOG 14 +#define CORE_NUM_PWM 10 +#elif defined(__MK20DX256__) +#define CORE_NUM_TOTAL_PINS 34 +#define CORE_NUM_DIGITAL 34 +#define CORE_NUM_INTERRUPT 34 +#define CORE_NUM_ANALOG 21 +#define CORE_NUM_PWM 12 +#elif defined(__MKL26Z64__) +#define CORE_NUM_TOTAL_PINS 27 +#define CORE_NUM_DIGITAL 27 +#define CORE_NUM_INTERRUPT 24 // really only 18, but 6 "holes" +#define CORE_NUM_ANALOG 13 +#define CORE_NUM_PWM 10 +#elif defined(__MK64FX512__) +#define CORE_NUM_TOTAL_PINS 64 +#define CORE_NUM_DIGITAL 64 +#define CORE_NUM_INTERRUPT 64 +#define CORE_NUM_ANALOG 27 +#define CORE_NUM_PWM 20 +#elif defined(__MK66FX1M0__) +#define CORE_NUM_TOTAL_PINS 64 +#define CORE_NUM_DIGITAL 64 +#define CORE_NUM_INTERRUPT 64 +#define CORE_NUM_ANALOG 25 +#define CORE_NUM_PWM 22 +#endif + +// These MAX_PIN_PORTx values have the highest Kinetis pin index +// that is used for a given port. +#if defined(__MK20DX128__) || defined(__MK20DX256__) +#define CORE_MAX_PIN_PORTA 13 +#define CORE_MAX_PIN_PORTB 19 +#define CORE_MAX_PIN_PORTC 11 +#define CORE_MAX_PIN_PORTD 7 +#define CORE_MAX_PIN_PORTE 1 +#elif defined(__MKL26Z64__) +#define CORE_MAX_PIN_PORTA 2 +#define CORE_MAX_PIN_PORTB 17 +#define CORE_MAX_PIN_PORTC 7 +#define CORE_MAX_PIN_PORTD 7 +#define CORE_MAX_PIN_PORTE 30 +#elif defined(__MK64FX512__) || defined(__MK66FX1M0__) +#define CORE_MAX_PIN_PORTA 29 +#define CORE_MAX_PIN_PORTB 23 +#define CORE_MAX_PIN_PORTC 11 +#define CORE_MAX_PIN_PORTD 15 +#define CORE_MAX_PIN_PORTE 26 +#endif + + +#if defined(__MK20DX128__) || defined(__MK20DX256__) + +#define CORE_PIN0_BIT 16 +#define CORE_PIN1_BIT 17 +#define CORE_PIN2_BIT 0 +#define CORE_PIN3_BIT 12 +#define CORE_PIN4_BIT 13 +#define CORE_PIN5_BIT 7 +#define CORE_PIN6_BIT 4 +#define CORE_PIN7_BIT 2 +#define CORE_PIN8_BIT 3 +#define CORE_PIN9_BIT 3 +#define CORE_PIN10_BIT 4 +#define CORE_PIN11_BIT 6 +#define CORE_PIN12_BIT 7 +#define CORE_PIN13_BIT 5 +#define CORE_PIN14_BIT 1 +#define CORE_PIN15_BIT 0 +#define CORE_PIN16_BIT 0 +#define CORE_PIN17_BIT 1 +#define CORE_PIN18_BIT 3 +#define CORE_PIN19_BIT 2 +#define CORE_PIN20_BIT 5 +#define CORE_PIN21_BIT 6 +#define CORE_PIN22_BIT 1 +#define CORE_PIN23_BIT 2 +#define CORE_PIN24_BIT 5 +#define CORE_PIN25_BIT 19 +#define CORE_PIN26_BIT 1 +#define CORE_PIN27_BIT 9 +#define CORE_PIN28_BIT 8 +#define CORE_PIN29_BIT 10 +#define CORE_PIN30_BIT 11 +#define CORE_PIN31_BIT 0 +#define CORE_PIN32_BIT 18 +#define CORE_PIN33_BIT 4 + +#define CORE_PIN0_BITMASK (1<<(CORE_PIN0_BIT)) +#define CORE_PIN1_BITMASK (1<<(CORE_PIN1_BIT)) +#define CORE_PIN2_BITMASK (1<<(CORE_PIN2_BIT)) +#define CORE_PIN3_BITMASK (1<<(CORE_PIN3_BIT)) +#define CORE_PIN4_BITMASK (1<<(CORE_PIN4_BIT)) +#define CORE_PIN5_BITMASK (1<<(CORE_PIN5_BIT)) +#define CORE_PIN6_BITMASK (1<<(CORE_PIN6_BIT)) +#define CORE_PIN7_BITMASK (1<<(CORE_PIN7_BIT)) +#define CORE_PIN8_BITMASK (1<<(CORE_PIN8_BIT)) +#define CORE_PIN9_BITMASK (1<<(CORE_PIN9_BIT)) +#define CORE_PIN10_BITMASK (1<<(CORE_PIN10_BIT)) +#define CORE_PIN11_BITMASK (1<<(CORE_PIN11_BIT)) +#define CORE_PIN12_BITMASK (1<<(CORE_PIN12_BIT)) +#define CORE_PIN13_BITMASK (1<<(CORE_PIN13_BIT)) +#define CORE_PIN14_BITMASK (1<<(CORE_PIN14_BIT)) +#define CORE_PIN15_BITMASK (1<<(CORE_PIN15_BIT)) +#define CORE_PIN16_BITMASK (1<<(CORE_PIN16_BIT)) +#define CORE_PIN17_BITMASK (1<<(CORE_PIN17_BIT)) +#define CORE_PIN18_BITMASK (1<<(CORE_PIN18_BIT)) +#define CORE_PIN19_BITMASK (1<<(CORE_PIN19_BIT)) +#define CORE_PIN20_BITMASK (1<<(CORE_PIN20_BIT)) +#define CORE_PIN21_BITMASK (1<<(CORE_PIN21_BIT)) +#define CORE_PIN22_BITMASK (1<<(CORE_PIN22_BIT)) +#define CORE_PIN23_BITMASK (1<<(CORE_PIN23_BIT)) +#define CORE_PIN24_BITMASK (1<<(CORE_PIN24_BIT)) +#define CORE_PIN25_BITMASK (1<<(CORE_PIN25_BIT)) +#define CORE_PIN26_BITMASK (1<<(CORE_PIN26_BIT)) +#define CORE_PIN27_BITMASK (1<<(CORE_PIN27_BIT)) +#define CORE_PIN28_BITMASK (1<<(CORE_PIN28_BIT)) +#define CORE_PIN29_BITMASK (1<<(CORE_PIN29_BIT)) +#define CORE_PIN30_BITMASK (1<<(CORE_PIN30_BIT)) +#define CORE_PIN31_BITMASK (1<<(CORE_PIN31_BIT)) +#define CORE_PIN32_BITMASK (1<<(CORE_PIN32_BIT)) +#define CORE_PIN33_BITMASK (1<<(CORE_PIN33_BIT)) + +#define CORE_PIN0_PORTREG GPIOB_PDOR +#define CORE_PIN1_PORTREG GPIOB_PDOR +#define CORE_PIN2_PORTREG GPIOD_PDOR +#define CORE_PIN3_PORTREG GPIOA_PDOR +#define CORE_PIN4_PORTREG GPIOA_PDOR +#define CORE_PIN5_PORTREG GPIOD_PDOR +#define CORE_PIN6_PORTREG GPIOD_PDOR +#define CORE_PIN7_PORTREG GPIOD_PDOR +#define CORE_PIN8_PORTREG GPIOD_PDOR +#define CORE_PIN9_PORTREG GPIOC_PDOR +#define CORE_PIN10_PORTREG GPIOC_PDOR +#define CORE_PIN11_PORTREG GPIOC_PDOR +#define CORE_PIN12_PORTREG GPIOC_PDOR +#define CORE_PIN13_PORTREG GPIOC_PDOR +#define CORE_PIN14_PORTREG GPIOD_PDOR +#define CORE_PIN15_PORTREG GPIOC_PDOR +#define CORE_PIN16_PORTREG GPIOB_PDOR +#define CORE_PIN17_PORTREG GPIOB_PDOR +#define CORE_PIN18_PORTREG GPIOB_PDOR +#define CORE_PIN19_PORTREG GPIOB_PDOR +#define CORE_PIN20_PORTREG GPIOD_PDOR +#define CORE_PIN21_PORTREG GPIOD_PDOR +#define CORE_PIN22_PORTREG GPIOC_PDOR +#define CORE_PIN23_PORTREG GPIOC_PDOR +#define CORE_PIN24_PORTREG GPIOA_PDOR +#define CORE_PIN25_PORTREG GPIOB_PDOR +#define CORE_PIN26_PORTREG GPIOE_PDOR +#define CORE_PIN27_PORTREG GPIOC_PDOR +#define CORE_PIN28_PORTREG GPIOC_PDOR +#define CORE_PIN29_PORTREG GPIOC_PDOR +#define CORE_PIN30_PORTREG GPIOC_PDOR +#define CORE_PIN31_PORTREG GPIOE_PDOR +#define CORE_PIN32_PORTREG GPIOB_PDOR +#define CORE_PIN33_PORTREG GPIOA_PDOR + +#define CORE_PIN0_PORTSET GPIOB_PSOR +#define CORE_PIN1_PORTSET GPIOB_PSOR +#define CORE_PIN2_PORTSET GPIOD_PSOR +#define CORE_PIN3_PORTSET GPIOA_PSOR +#define CORE_PIN4_PORTSET GPIOA_PSOR +#define CORE_PIN5_PORTSET GPIOD_PSOR +#define CORE_PIN6_PORTSET GPIOD_PSOR +#define CORE_PIN7_PORTSET GPIOD_PSOR +#define CORE_PIN8_PORTSET GPIOD_PSOR +#define CORE_PIN9_PORTSET GPIOC_PSOR +#define CORE_PIN10_PORTSET GPIOC_PSOR +#define CORE_PIN11_PORTSET GPIOC_PSOR +#define CORE_PIN12_PORTSET GPIOC_PSOR +#define CORE_PIN13_PORTSET GPIOC_PSOR +#define CORE_PIN14_PORTSET GPIOD_PSOR +#define CORE_PIN15_PORTSET GPIOC_PSOR +#define CORE_PIN16_PORTSET GPIOB_PSOR +#define CORE_PIN17_PORTSET GPIOB_PSOR +#define CORE_PIN18_PORTSET GPIOB_PSOR +#define CORE_PIN19_PORTSET GPIOB_PSOR +#define CORE_PIN20_PORTSET GPIOD_PSOR +#define CORE_PIN21_PORTSET GPIOD_PSOR +#define CORE_PIN22_PORTSET GPIOC_PSOR +#define CORE_PIN23_PORTSET GPIOC_PSOR +#define CORE_PIN24_PORTSET GPIOA_PSOR +#define CORE_PIN25_PORTSET GPIOB_PSOR +#define CORE_PIN26_PORTSET GPIOE_PSOR +#define CORE_PIN27_PORTSET GPIOC_PSOR +#define CORE_PIN28_PORTSET GPIOC_PSOR +#define CORE_PIN29_PORTSET GPIOC_PSOR +#define CORE_PIN30_PORTSET GPIOC_PSOR +#define CORE_PIN31_PORTSET GPIOE_PSOR +#define CORE_PIN32_PORTSET GPIOB_PSOR +#define CORE_PIN33_PORTSET GPIOA_PSOR + +#define CORE_PIN0_PORTCLEAR GPIOB_PCOR +#define CORE_PIN1_PORTCLEAR GPIOB_PCOR +#define CORE_PIN2_PORTCLEAR GPIOD_PCOR +#define CORE_PIN3_PORTCLEAR GPIOA_PCOR +#define CORE_PIN4_PORTCLEAR GPIOA_PCOR +#define CORE_PIN5_PORTCLEAR GPIOD_PCOR +#define CORE_PIN6_PORTCLEAR GPIOD_PCOR +#define CORE_PIN7_PORTCLEAR GPIOD_PCOR +#define CORE_PIN8_PORTCLEAR GPIOD_PCOR +#define CORE_PIN9_PORTCLEAR GPIOC_PCOR +#define CORE_PIN10_PORTCLEAR GPIOC_PCOR +#define CORE_PIN11_PORTCLEAR GPIOC_PCOR +#define CORE_PIN12_PORTCLEAR GPIOC_PCOR +#define CORE_PIN13_PORTCLEAR GPIOC_PCOR +#define CORE_PIN14_PORTCLEAR GPIOD_PCOR +#define CORE_PIN15_PORTCLEAR GPIOC_PCOR +#define CORE_PIN16_PORTCLEAR GPIOB_PCOR +#define CORE_PIN17_PORTCLEAR GPIOB_PCOR +#define CORE_PIN18_PORTCLEAR GPIOB_PCOR +#define CORE_PIN19_PORTCLEAR GPIOB_PCOR +#define CORE_PIN20_PORTCLEAR GPIOD_PCOR +#define CORE_PIN21_PORTCLEAR GPIOD_PCOR +#define CORE_PIN22_PORTCLEAR GPIOC_PCOR +#define CORE_PIN23_PORTCLEAR GPIOC_PCOR +#define CORE_PIN24_PORTCLEAR GPIOA_PCOR +#define CORE_PIN25_PORTCLEAR GPIOB_PCOR +#define CORE_PIN26_PORTCLEAR GPIOE_PCOR +#define CORE_PIN27_PORTCLEAR GPIOC_PCOR +#define CORE_PIN28_PORTCLEAR GPIOC_PCOR +#define CORE_PIN29_PORTCLEAR GPIOC_PCOR +#define CORE_PIN30_PORTCLEAR GPIOC_PCOR +#define CORE_PIN31_PORTCLEAR GPIOE_PCOR +#define CORE_PIN32_PORTCLEAR GPIOB_PCOR +#define CORE_PIN33_PORTCLEAR GPIOA_PCOR + +#define CORE_PIN0_DDRREG GPIOB_PDDR +#define CORE_PIN1_DDRREG GPIOB_PDDR +#define CORE_PIN2_DDRREG GPIOD_PDDR +#define CORE_PIN3_DDRREG GPIOA_PDDR +#define CORE_PIN4_DDRREG GPIOA_PDDR +#define CORE_PIN5_DDRREG GPIOD_PDDR +#define CORE_PIN6_DDRREG GPIOD_PDDR +#define CORE_PIN7_DDRREG GPIOD_PDDR +#define CORE_PIN8_DDRREG GPIOD_PDDR +#define CORE_PIN9_DDRREG GPIOC_PDDR +#define CORE_PIN10_DDRREG GPIOC_PDDR +#define CORE_PIN11_DDRREG GPIOC_PDDR +#define CORE_PIN12_DDRREG GPIOC_PDDR +#define CORE_PIN13_DDRREG GPIOC_PDDR +#define CORE_PIN14_DDRREG GPIOD_PDDR +#define CORE_PIN15_DDRREG GPIOC_PDDR +#define CORE_PIN16_DDRREG GPIOB_PDDR +#define CORE_PIN17_DDRREG GPIOB_PDDR +#define CORE_PIN18_DDRREG GPIOB_PDDR +#define CORE_PIN19_DDRREG GPIOB_PDDR +#define CORE_PIN20_DDRREG GPIOD_PDDR +#define CORE_PIN21_DDRREG GPIOD_PDDR +#define CORE_PIN22_DDRREG GPIOC_PDDR +#define CORE_PIN23_DDRREG GPIOC_PDDR +#define CORE_PIN24_DDRREG GPIOA_PDDR +#define CORE_PIN25_DDRREG GPIOB_PDDR +#define CORE_PIN26_DDRREG GPIOE_PDDR +#define CORE_PIN27_DDRREG GPIOC_PDDR +#define CORE_PIN28_DDRREG GPIOC_PDDR +#define CORE_PIN29_DDRREG GPIOC_PDDR +#define CORE_PIN30_DDRREG GPIOC_PDDR +#define CORE_PIN31_DDRREG GPIOE_PDDR +#define CORE_PIN32_DDRREG GPIOB_PDDR +#define CORE_PIN33_DDRREG GPIOA_PDDR + +#define CORE_PIN0_PINREG GPIOB_PDIR +#define CORE_PIN1_PINREG GPIOB_PDIR +#define CORE_PIN2_PINREG GPIOD_PDIR +#define CORE_PIN3_PINREG GPIOA_PDIR +#define CORE_PIN4_PINREG GPIOA_PDIR +#define CORE_PIN5_PINREG GPIOD_PDIR +#define CORE_PIN6_PINREG GPIOD_PDIR +#define CORE_PIN7_PINREG GPIOD_PDIR +#define CORE_PIN8_PINREG GPIOD_PDIR +#define CORE_PIN9_PINREG GPIOC_PDIR +#define CORE_PIN10_PINREG GPIOC_PDIR +#define CORE_PIN11_PINREG GPIOC_PDIR +#define CORE_PIN12_PINREG GPIOC_PDIR +#define CORE_PIN13_PINREG GPIOC_PDIR +#define CORE_PIN14_PINREG GPIOD_PDIR +#define CORE_PIN15_PINREG GPIOC_PDIR +#define CORE_PIN16_PINREG GPIOB_PDIR +#define CORE_PIN17_PINREG GPIOB_PDIR +#define CORE_PIN18_PINREG GPIOB_PDIR +#define CORE_PIN19_PINREG GPIOB_PDIR +#define CORE_PIN20_PINREG GPIOD_PDIR +#define CORE_PIN21_PINREG GPIOD_PDIR +#define CORE_PIN22_PINREG GPIOC_PDIR +#define CORE_PIN23_PINREG GPIOC_PDIR +#define CORE_PIN24_PINREG GPIOA_PDIR +#define CORE_PIN25_PINREG GPIOB_PDIR +#define CORE_PIN26_PINREG GPIOE_PDIR +#define CORE_PIN27_PINREG GPIOC_PDIR +#define CORE_PIN28_PINREG GPIOC_PDIR +#define CORE_PIN29_PINREG GPIOC_PDIR +#define CORE_PIN30_PINREG GPIOC_PDIR +#define CORE_PIN31_PINREG GPIOE_PDIR +#define CORE_PIN32_PINREG GPIOB_PDIR +#define CORE_PIN33_PINREG GPIOA_PDIR + +#define CORE_PIN0_CONFIG PORTB_PCR16 +#define CORE_PIN1_CONFIG PORTB_PCR17 +#define CORE_PIN2_CONFIG PORTD_PCR0 +#define CORE_PIN3_CONFIG PORTA_PCR12 +#define CORE_PIN4_CONFIG PORTA_PCR13 +#define CORE_PIN5_CONFIG PORTD_PCR7 +#define CORE_PIN6_CONFIG PORTD_PCR4 +#define CORE_PIN7_CONFIG PORTD_PCR2 +#define CORE_PIN8_CONFIG PORTD_PCR3 +#define CORE_PIN9_CONFIG PORTC_PCR3 +#define CORE_PIN10_CONFIG PORTC_PCR4 +#define CORE_PIN11_CONFIG PORTC_PCR6 +#define CORE_PIN12_CONFIG PORTC_PCR7 +#define CORE_PIN13_CONFIG PORTC_PCR5 +#define CORE_PIN14_CONFIG PORTD_PCR1 +#define CORE_PIN15_CONFIG PORTC_PCR0 +#define CORE_PIN16_CONFIG PORTB_PCR0 +#define CORE_PIN17_CONFIG PORTB_PCR1 +#define CORE_PIN18_CONFIG PORTB_PCR3 +#define CORE_PIN19_CONFIG PORTB_PCR2 +#define CORE_PIN20_CONFIG PORTD_PCR5 +#define CORE_PIN21_CONFIG PORTD_PCR6 +#define CORE_PIN22_CONFIG PORTC_PCR1 +#define CORE_PIN23_CONFIG PORTC_PCR2 +#define CORE_PIN24_CONFIG PORTA_PCR5 +#define CORE_PIN25_CONFIG PORTB_PCR19 +#define CORE_PIN26_CONFIG PORTE_PCR1 +#define CORE_PIN27_CONFIG PORTC_PCR9 +#define CORE_PIN28_CONFIG PORTC_PCR8 +#define CORE_PIN29_CONFIG PORTC_PCR10 +#define CORE_PIN30_CONFIG PORTC_PCR11 +#define CORE_PIN31_CONFIG PORTE_PCR0 +#define CORE_PIN32_CONFIG PORTB_PCR18 +#define CORE_PIN33_CONFIG PORTA_PCR4 + +#define CORE_ADC0_PIN 14 +#define CORE_ADC1_PIN 15 +#define CORE_ADC2_PIN 16 +#define CORE_ADC3_PIN 17 +#define CORE_ADC4_PIN 18 +#define CORE_ADC5_PIN 19 +#define CORE_ADC6_PIN 20 +#define CORE_ADC7_PIN 21 +#define CORE_ADC8_PIN 22 +#define CORE_ADC9_PIN 23 +#define CORE_ADC10_PIN 34 +#define CORE_ADC11_PIN 35 +#define CORE_ADC12_PIN 36 +#define CORE_ADC13_PIN 37 + +#define CORE_RXD0_PIN 0 +#define CORE_TXD0_PIN 1 +#define CORE_RXD1_PIN 9 +#define CORE_TXD1_PIN 10 +#define CORE_RXD2_PIN 7 +#define CORE_TXD2_PIN 8 + +#define CORE_INT0_PIN 0 +#define CORE_INT1_PIN 1 +#define CORE_INT2_PIN 2 +#define CORE_INT3_PIN 3 +#define CORE_INT4_PIN 4 +#define CORE_INT5_PIN 5 +#define CORE_INT6_PIN 6 +#define CORE_INT7_PIN 7 +#define CORE_INT8_PIN 8 +#define CORE_INT9_PIN 9 +#define CORE_INT10_PIN 10 +#define CORE_INT11_PIN 11 +#define CORE_INT12_PIN 12 +#define CORE_INT13_PIN 13 +#define CORE_INT14_PIN 14 +#define CORE_INT15_PIN 15 +#define CORE_INT16_PIN 16 +#define CORE_INT17_PIN 17 +#define CORE_INT18_PIN 18 +#define CORE_INT19_PIN 19 +#define CORE_INT20_PIN 20 +#define CORE_INT21_PIN 21 +#define CORE_INT22_PIN 22 +#define CORE_INT23_PIN 23 +#define CORE_INT24_PIN 24 +#define CORE_INT25_PIN 25 +#define CORE_INT26_PIN 26 +#define CORE_INT27_PIN 27 +#define CORE_INT28_PIN 28 +#define CORE_INT29_PIN 29 +#define CORE_INT30_PIN 30 +#define CORE_INT31_PIN 31 +#define CORE_INT32_PIN 32 +#define CORE_INT33_PIN 33 +#define CORE_INT_EVERY_PIN 1 + + +#elif defined(__MKL26Z64__) + +#define CORE_PIN0_BIT 16 +#define CORE_PIN1_BIT 17 +#define CORE_PIN2_BIT 0 +#define CORE_PIN3_BIT 1 +#define CORE_PIN4_BIT 2 +#define CORE_PIN5_BIT 7 +#define CORE_PIN6_BIT 4 +#define CORE_PIN7_BIT 2 +#define CORE_PIN8_BIT 3 +#define CORE_PIN9_BIT 3 +#define CORE_PIN10_BIT 4 +#define CORE_PIN11_BIT 6 +#define CORE_PIN12_BIT 7 +#define CORE_PIN13_BIT 5 +#define CORE_PIN14_BIT 1 +#define CORE_PIN15_BIT 0 +#define CORE_PIN16_BIT 0 +#define CORE_PIN17_BIT 1 +#define CORE_PIN18_BIT 3 +#define CORE_PIN19_BIT 2 +#define CORE_PIN20_BIT 5 +#define CORE_PIN21_BIT 6 +#define CORE_PIN22_BIT 1 +#define CORE_PIN23_BIT 2 +#define CORE_PIN24_BIT 20 +#define CORE_PIN25_BIT 21 +#define CORE_PIN26_BIT 30 + +#define CORE_PIN0_BITMASK (1<<(CORE_PIN0_BIT)) +#define CORE_PIN1_BITMASK (1<<(CORE_PIN1_BIT)) +#define CORE_PIN2_BITMASK (1<<(CORE_PIN2_BIT)) +#define CORE_PIN3_BITMASK (1<<(CORE_PIN3_BIT)) +#define CORE_PIN4_BITMASK (1<<(CORE_PIN4_BIT)) +#define CORE_PIN5_BITMASK (1<<(CORE_PIN5_BIT)) +#define CORE_PIN6_BITMASK (1<<(CORE_PIN6_BIT)) +#define CORE_PIN7_BITMASK (1<<(CORE_PIN7_BIT)) +#define CORE_PIN8_BITMASK (1<<(CORE_PIN8_BIT)) +#define CORE_PIN9_BITMASK (1<<(CORE_PIN9_BIT)) +#define CORE_PIN10_BITMASK (1<<(CORE_PIN10_BIT)) +#define CORE_PIN11_BITMASK (1<<(CORE_PIN11_BIT)) +#define CORE_PIN12_BITMASK (1<<(CORE_PIN12_BIT)) +#define CORE_PIN13_BITMASK (1<<(CORE_PIN13_BIT)) +#define CORE_PIN14_BITMASK (1<<(CORE_PIN14_BIT)) +#define CORE_PIN15_BITMASK (1<<(CORE_PIN15_BIT)) +#define CORE_PIN16_BITMASK (1<<(CORE_PIN16_BIT)) +#define CORE_PIN17_BITMASK (1<<(CORE_PIN17_BIT)) +#define CORE_PIN18_BITMASK (1<<(CORE_PIN18_BIT)) +#define CORE_PIN19_BITMASK (1<<(CORE_PIN19_BIT)) +#define CORE_PIN20_BITMASK (1<<(CORE_PIN20_BIT)) +#define CORE_PIN21_BITMASK (1<<(CORE_PIN21_BIT)) +#define CORE_PIN22_BITMASK (1<<(CORE_PIN22_BIT)) +#define CORE_PIN23_BITMASK (1<<(CORE_PIN23_BIT)) +#define CORE_PIN24_BITMASK (1<<(CORE_PIN24_BIT)) +#define CORE_PIN25_BITMASK (1<<(CORE_PIN25_BIT)) +#define CORE_PIN26_BITMASK (1<<(CORE_PIN26_BIT)) + +#define CORE_PIN0_PORTREG FGPIOB_PDOR +#define CORE_PIN1_PORTREG FGPIOB_PDOR +#define CORE_PIN2_PORTREG FGPIOD_PDOR +#define CORE_PIN3_PORTREG FGPIOA_PDOR +#define CORE_PIN4_PORTREG FGPIOA_PDOR +#define CORE_PIN5_PORTREG FGPIOD_PDOR +#define CORE_PIN6_PORTREG FGPIOD_PDOR +#define CORE_PIN7_PORTREG FGPIOD_PDOR +#define CORE_PIN8_PORTREG FGPIOD_PDOR +#define CORE_PIN9_PORTREG FGPIOC_PDOR +#define CORE_PIN10_PORTREG FGPIOC_PDOR +#define CORE_PIN11_PORTREG FGPIOC_PDOR +#define CORE_PIN12_PORTREG FGPIOC_PDOR +#define CORE_PIN13_PORTREG FGPIOC_PDOR +#define CORE_PIN14_PORTREG FGPIOD_PDOR +#define CORE_PIN15_PORTREG FGPIOC_PDOR +#define CORE_PIN16_PORTREG FGPIOB_PDOR +#define CORE_PIN17_PORTREG FGPIOB_PDOR +#define CORE_PIN18_PORTREG FGPIOB_PDOR +#define CORE_PIN19_PORTREG FGPIOB_PDOR +#define CORE_PIN20_PORTREG FGPIOD_PDOR +#define CORE_PIN21_PORTREG FGPIOD_PDOR +#define CORE_PIN22_PORTREG FGPIOC_PDOR +#define CORE_PIN23_PORTREG FGPIOC_PDOR +#define CORE_PIN24_PORTREG FGPIOE_PDOR +#define CORE_PIN25_PORTREG FGPIOE_PDOR +#define CORE_PIN26_PORTREG FGPIOE_PDOR + +#define CORE_PIN0_PORTSET FGPIOB_PSOR +#define CORE_PIN1_PORTSET FGPIOB_PSOR +#define CORE_PIN2_PORTSET FGPIOD_PSOR +#define CORE_PIN3_PORTSET FGPIOA_PSOR +#define CORE_PIN4_PORTSET FGPIOA_PSOR +#define CORE_PIN5_PORTSET FGPIOD_PSOR +#define CORE_PIN6_PORTSET FGPIOD_PSOR +#define CORE_PIN7_PORTSET FGPIOD_PSOR +#define CORE_PIN8_PORTSET FGPIOD_PSOR +#define CORE_PIN9_PORTSET FGPIOC_PSOR +#define CORE_PIN10_PORTSET FGPIOC_PSOR +#define CORE_PIN11_PORTSET FGPIOC_PSOR +#define CORE_PIN12_PORTSET FGPIOC_PSOR +#define CORE_PIN13_PORTSET FGPIOC_PSOR +#define CORE_PIN14_PORTSET FGPIOD_PSOR +#define CORE_PIN15_PORTSET FGPIOC_PSOR +#define CORE_PIN16_PORTSET FGPIOB_PSOR +#define CORE_PIN17_PORTSET FGPIOB_PSOR +#define CORE_PIN18_PORTSET FGPIOB_PSOR +#define CORE_PIN19_PORTSET FGPIOB_PSOR +#define CORE_PIN20_PORTSET FGPIOD_PSOR +#define CORE_PIN21_PORTSET FGPIOD_PSOR +#define CORE_PIN22_PORTSET FGPIOC_PSOR +#define CORE_PIN23_PORTSET FGPIOC_PSOR +#define CORE_PIN24_PORTSET FGPIOE_PSOR +#define CORE_PIN25_PORTSET FGPIOE_PSOR +#define CORE_PIN26_PORTSET FGPIOE_PSOR + +#define CORE_PIN0_PORTCLEAR FGPIOB_PCOR +#define CORE_PIN1_PORTCLEAR FGPIOB_PCOR +#define CORE_PIN2_PORTCLEAR FGPIOD_PCOR +#define CORE_PIN3_PORTCLEAR FGPIOA_PCOR +#define CORE_PIN4_PORTCLEAR FGPIOA_PCOR +#define CORE_PIN5_PORTCLEAR FGPIOD_PCOR +#define CORE_PIN6_PORTCLEAR FGPIOD_PCOR +#define CORE_PIN7_PORTCLEAR FGPIOD_PCOR +#define CORE_PIN8_PORTCLEAR FGPIOD_PCOR +#define CORE_PIN9_PORTCLEAR FGPIOC_PCOR +#define CORE_PIN10_PORTCLEAR FGPIOC_PCOR +#define CORE_PIN11_PORTCLEAR FGPIOC_PCOR +#define CORE_PIN12_PORTCLEAR FGPIOC_PCOR +#define CORE_PIN13_PORTCLEAR FGPIOC_PCOR +#define CORE_PIN14_PORTCLEAR FGPIOD_PCOR +#define CORE_PIN15_PORTCLEAR FGPIOC_PCOR +#define CORE_PIN16_PORTCLEAR FGPIOB_PCOR +#define CORE_PIN17_PORTCLEAR FGPIOB_PCOR +#define CORE_PIN18_PORTCLEAR FGPIOB_PCOR +#define CORE_PIN19_PORTCLEAR FGPIOB_PCOR +#define CORE_PIN20_PORTCLEAR FGPIOD_PCOR +#define CORE_PIN21_PORTCLEAR FGPIOD_PCOR +#define CORE_PIN22_PORTCLEAR FGPIOC_PCOR +#define CORE_PIN23_PORTCLEAR FGPIOC_PCOR +#define CORE_PIN24_PORTCLEAR FGPIOE_PCOR +#define CORE_PIN25_PORTCLEAR FGPIOE_PCOR +#define CORE_PIN26_PORTCLEAR FGPIOE_PCOR + +#define CORE_PIN0_DDRREG FGPIOB_PDDR +#define CORE_PIN1_DDRREG FGPIOB_PDDR +#define CORE_PIN2_DDRREG FGPIOD_PDDR +#define CORE_PIN3_DDRREG FGPIOA_PDDR +#define CORE_PIN4_DDRREG FGPIOA_PDDR +#define CORE_PIN5_DDRREG FGPIOD_PDDR +#define CORE_PIN6_DDRREG FGPIOD_PDDR +#define CORE_PIN7_DDRREG FGPIOD_PDDR +#define CORE_PIN8_DDRREG FGPIOD_PDDR +#define CORE_PIN9_DDRREG FGPIOC_PDDR +#define CORE_PIN10_DDRREG FGPIOC_PDDR +#define CORE_PIN11_DDRREG FGPIOC_PDDR +#define CORE_PIN12_DDRREG FGPIOC_PDDR +#define CORE_PIN13_DDRREG FGPIOC_PDDR +#define CORE_PIN14_DDRREG FGPIOD_PDDR +#define CORE_PIN15_DDRREG FGPIOC_PDDR +#define CORE_PIN16_DDRREG FGPIOB_PDDR +#define CORE_PIN17_DDRREG FGPIOB_PDDR +#define CORE_PIN18_DDRREG FGPIOB_PDDR +#define CORE_PIN19_DDRREG FGPIOB_PDDR +#define CORE_PIN20_DDRREG FGPIOD_PDDR +#define CORE_PIN21_DDRREG FGPIOD_PDDR +#define CORE_PIN22_DDRREG FGPIOC_PDDR +#define CORE_PIN23_DDRREG FGPIOC_PDDR +#define CORE_PIN24_DDRREG FGPIOE_PDDR +#define CORE_PIN25_DDRREG FGPIOE_PDDR +#define CORE_PIN26_DDRREG FGPIOE_PDDR + +#define CORE_PIN0_PINREG FGPIOB_PDIR +#define CORE_PIN1_PINREG FGPIOB_PDIR +#define CORE_PIN2_PINREG FGPIOD_PDIR +#define CORE_PIN3_PINREG FGPIOA_PDIR +#define CORE_PIN4_PINREG FGPIOA_PDIR +#define CORE_PIN5_PINREG FGPIOD_PDIR +#define CORE_PIN6_PINREG FGPIOD_PDIR +#define CORE_PIN7_PINREG FGPIOD_PDIR +#define CORE_PIN8_PINREG FGPIOD_PDIR +#define CORE_PIN9_PINREG FGPIOC_PDIR +#define CORE_PIN10_PINREG FGPIOC_PDIR +#define CORE_PIN11_PINREG FGPIOC_PDIR +#define CORE_PIN12_PINREG FGPIOC_PDIR +#define CORE_PIN13_PINREG FGPIOC_PDIR +#define CORE_PIN14_PINREG FGPIOD_PDIR +#define CORE_PIN15_PINREG FGPIOC_PDIR +#define CORE_PIN16_PINREG FGPIOB_PDIR +#define CORE_PIN17_PINREG FGPIOB_PDIR +#define CORE_PIN18_PINREG FGPIOB_PDIR +#define CORE_PIN19_PINREG FGPIOB_PDIR +#define CORE_PIN20_PINREG FGPIOD_PDIR +#define CORE_PIN21_PINREG FGPIOD_PDIR +#define CORE_PIN22_PINREG FGPIOC_PDIR +#define CORE_PIN23_PINREG FGPIOC_PDIR +#define CORE_PIN24_PINREG FGPIOE_PDIR +#define CORE_PIN25_PINREG FGPIOE_PDIR +#define CORE_PIN26_PINREG FGPIOE_PDIR + +#define CORE_PIN0_CONFIG PORTB_PCR16 +#define CORE_PIN1_CONFIG PORTB_PCR17 +#define CORE_PIN2_CONFIG PORTD_PCR0 +#define CORE_PIN3_CONFIG PORTA_PCR1 +#define CORE_PIN4_CONFIG PORTA_PCR2 +#define CORE_PIN5_CONFIG PORTD_PCR7 +#define CORE_PIN6_CONFIG PORTD_PCR4 +#define CORE_PIN7_CONFIG PORTD_PCR2 +#define CORE_PIN8_CONFIG PORTD_PCR3 +#define CORE_PIN9_CONFIG PORTC_PCR3 +#define CORE_PIN10_CONFIG PORTC_PCR4 +#define CORE_PIN11_CONFIG PORTC_PCR6 +#define CORE_PIN12_CONFIG PORTC_PCR7 +#define CORE_PIN13_CONFIG PORTC_PCR5 +#define CORE_PIN14_CONFIG PORTD_PCR1 +#define CORE_PIN15_CONFIG PORTC_PCR0 +#define CORE_PIN16_CONFIG PORTB_PCR0 +#define CORE_PIN17_CONFIG PORTB_PCR1 +#define CORE_PIN18_CONFIG PORTB_PCR3 +#define CORE_PIN19_CONFIG PORTB_PCR2 +#define CORE_PIN20_CONFIG PORTD_PCR5 +#define CORE_PIN21_CONFIG PORTD_PCR6 +#define CORE_PIN22_CONFIG PORTC_PCR1 +#define CORE_PIN23_CONFIG PORTC_PCR2 +#define CORE_PIN24_CONFIG PORTE_PCR20 +#define CORE_PIN25_CONFIG PORTE_PCR21 +#define CORE_PIN26_CONFIG PORTE_PCR30 + +#define CORE_ADC0_PIN 14 +#define CORE_ADC1_PIN 15 +#define CORE_ADC2_PIN 16 +#define CORE_ADC3_PIN 17 +#define CORE_ADC4_PIN 18 +#define CORE_ADC5_PIN 19 +#define CORE_ADC6_PIN 20 +#define CORE_ADC7_PIN 21 +#define CORE_ADC8_PIN 22 +#define CORE_ADC9_PIN 23 +#define CORE_ADC10_PIN 24 +#define CORE_ADC11_PIN 25 +#define CORE_ADC12_PIN 26 + +#define CORE_RXD0_PIN 0 +#define CORE_TXD0_PIN 1 +#define CORE_RXD1_PIN 9 +#define CORE_TXD1_PIN 10 +#define CORE_RXD2_PIN 7 +#define CORE_TXD2_PIN 8 + +#define CORE_INT2_PIN 2 +#define CORE_INT3_PIN 3 +#define CORE_INT4_PIN 4 +#define CORE_INT5_PIN 5 +#define CORE_INT6_PIN 6 +#define CORE_INT7_PIN 7 +#define CORE_INT8_PIN 8 +#define CORE_INT9_PIN 9 +#define CORE_INT10_PIN 10 +#define CORE_INT11_PIN 11 +#define CORE_INT12_PIN 12 +#define CORE_INT13_PIN 13 +#define CORE_INT14_PIN 14 +#define CORE_INT15_PIN 15 +#define CORE_INT20_PIN 20 +#define CORE_INT21_PIN 21 +#define CORE_INT22_PIN 22 +#define CORE_INT23_PIN 23 + + +#elif defined(__MK64FX512__) || defined(__MK66FX1M0__) + +#define CORE_PIN0_BIT 16 +#define CORE_PIN1_BIT 17 +#define CORE_PIN2_BIT 0 +#define CORE_PIN3_BIT 12 +#define CORE_PIN4_BIT 13 +#define CORE_PIN5_BIT 7 +#define CORE_PIN6_BIT 4 +#define CORE_PIN7_BIT 2 +#define CORE_PIN8_BIT 3 +#define CORE_PIN9_BIT 3 +#define CORE_PIN10_BIT 4 +#define CORE_PIN11_BIT 6 +#define CORE_PIN12_BIT 7 +#define CORE_PIN13_BIT 5 +#define CORE_PIN14_BIT 1 +#define CORE_PIN15_BIT 0 +#define CORE_PIN16_BIT 0 +#define CORE_PIN17_BIT 1 +#define CORE_PIN18_BIT 3 +#define CORE_PIN19_BIT 2 +#define CORE_PIN20_BIT 5 +#define CORE_PIN21_BIT 6 +#define CORE_PIN22_BIT 1 +#define CORE_PIN23_BIT 2 +#define CORE_PIN24_BIT 26 +#define CORE_PIN25_BIT 5 +#define CORE_PIN26_BIT 14 +#define CORE_PIN27_BIT 15 +#define CORE_PIN28_BIT 16 +#define CORE_PIN29_BIT 18 +#define CORE_PIN30_BIT 19 +#define CORE_PIN31_BIT 10 +#define CORE_PIN32_BIT 11 +#define CORE_PIN33_BIT 24 +#define CORE_PIN34_BIT 25 +#define CORE_PIN35_BIT 8 +#define CORE_PIN36_BIT 9 +#define CORE_PIN37_BIT 10 +#define CORE_PIN38_BIT 11 +#define CORE_PIN39_BIT 17 +#define CORE_PIN40_BIT 28 +#define CORE_PIN41_BIT 29 +#define CORE_PIN42_BIT 26 +#define CORE_PIN43_BIT 20 +#define CORE_PIN44_BIT 22 +#define CORE_PIN45_BIT 23 +#define CORE_PIN46_BIT 21 +#define CORE_PIN47_BIT 8 +#define CORE_PIN48_BIT 9 +#define CORE_PIN49_BIT 4 +#define CORE_PIN50_BIT 5 +#define CORE_PIN51_BIT 14 +#define CORE_PIN52_BIT 13 +#define CORE_PIN53_BIT 12 +#define CORE_PIN54_BIT 15 +#define CORE_PIN55_BIT 11 +#define CORE_PIN56_BIT 10 +#define CORE_PIN57_BIT 11 +#define CORE_PIN58_BIT 0 +#define CORE_PIN59_BIT 1 +#define CORE_PIN60_BIT 2 +#define CORE_PIN61_BIT 3 +#define CORE_PIN62_BIT 4 +#define CORE_PIN63_BIT 5 + +#define CORE_PIN0_BITMASK (1<<(CORE_PIN0_BIT)) +#define CORE_PIN1_BITMASK (1<<(CORE_PIN1_BIT)) +#define CORE_PIN2_BITMASK (1<<(CORE_PIN2_BIT)) +#define CORE_PIN3_BITMASK (1<<(CORE_PIN3_BIT)) +#define CORE_PIN4_BITMASK (1<<(CORE_PIN4_BIT)) +#define CORE_PIN5_BITMASK (1<<(CORE_PIN5_BIT)) +#define CORE_PIN6_BITMASK (1<<(CORE_PIN6_BIT)) +#define CORE_PIN7_BITMASK (1<<(CORE_PIN7_BIT)) +#define CORE_PIN8_BITMASK (1<<(CORE_PIN8_BIT)) +#define CORE_PIN9_BITMASK (1<<(CORE_PIN9_BIT)) +#define CORE_PIN10_BITMASK (1<<(CORE_PIN10_BIT)) +#define CORE_PIN11_BITMASK (1<<(CORE_PIN11_BIT)) +#define CORE_PIN12_BITMASK (1<<(CORE_PIN12_BIT)) +#define CORE_PIN13_BITMASK (1<<(CORE_PIN13_BIT)) +#define CORE_PIN14_BITMASK (1<<(CORE_PIN14_BIT)) +#define CORE_PIN15_BITMASK (1<<(CORE_PIN15_BIT)) +#define CORE_PIN16_BITMASK (1<<(CORE_PIN16_BIT)) +#define CORE_PIN17_BITMASK (1<<(CORE_PIN17_BIT)) +#define CORE_PIN18_BITMASK (1<<(CORE_PIN18_BIT)) +#define CORE_PIN19_BITMASK (1<<(CORE_PIN19_BIT)) +#define CORE_PIN20_BITMASK (1<<(CORE_PIN20_BIT)) +#define CORE_PIN21_BITMASK (1<<(CORE_PIN21_BIT)) +#define CORE_PIN22_BITMASK (1<<(CORE_PIN22_BIT)) +#define CORE_PIN23_BITMASK (1<<(CORE_PIN23_BIT)) +#define CORE_PIN24_BITMASK (1<<(CORE_PIN24_BIT)) +#define CORE_PIN25_BITMASK (1<<(CORE_PIN25_BIT)) +#define CORE_PIN26_BITMASK (1<<(CORE_PIN26_BIT)) +#define CORE_PIN27_BITMASK (1<<(CORE_PIN27_BIT)) +#define CORE_PIN28_BITMASK (1<<(CORE_PIN28_BIT)) +#define CORE_PIN29_BITMASK (1<<(CORE_PIN29_BIT)) +#define CORE_PIN30_BITMASK (1<<(CORE_PIN30_BIT)) +#define CORE_PIN31_BITMASK (1<<(CORE_PIN31_BIT)) +#define CORE_PIN32_BITMASK (1<<(CORE_PIN32_BIT)) +#define CORE_PIN33_BITMASK (1<<(CORE_PIN33_BIT)) +#define CORE_PIN34_BITMASK (1<<(CORE_PIN34_BIT)) +#define CORE_PIN35_BITMASK (1<<(CORE_PIN35_BIT)) +#define CORE_PIN36_BITMASK (1<<(CORE_PIN36_BIT)) +#define CORE_PIN37_BITMASK (1<<(CORE_PIN37_BIT)) +#define CORE_PIN38_BITMASK (1<<(CORE_PIN38_BIT)) +#define CORE_PIN39_BITMASK (1<<(CORE_PIN39_BIT)) +#define CORE_PIN40_BITMASK (1<<(CORE_PIN40_BIT)) +#define CORE_PIN41_BITMASK (1<<(CORE_PIN41_BIT)) +#define CORE_PIN42_BITMASK (1<<(CORE_PIN42_BIT)) +#define CORE_PIN43_BITMASK (1<<(CORE_PIN43_BIT)) +#define CORE_PIN44_BITMASK (1<<(CORE_PIN44_BIT)) +#define CORE_PIN45_BITMASK (1<<(CORE_PIN45_BIT)) +#define CORE_PIN46_BITMASK (1<<(CORE_PIN46_BIT)) +#define CORE_PIN47_BITMASK (1<<(CORE_PIN47_BIT)) +#define CORE_PIN48_BITMASK (1<<(CORE_PIN48_BIT)) +#define CORE_PIN49_BITMASK (1<<(CORE_PIN49_BIT)) +#define CORE_PIN50_BITMASK (1<<(CORE_PIN50_BIT)) +#define CORE_PIN51_BITMASK (1<<(CORE_PIN51_BIT)) +#define CORE_PIN52_BITMASK (1<<(CORE_PIN52_BIT)) +#define CORE_PIN53_BITMASK (1<<(CORE_PIN53_BIT)) +#define CORE_PIN54_BITMASK (1<<(CORE_PIN54_BIT)) +#define CORE_PIN55_BITMASK (1<<(CORE_PIN55_BIT)) +#define CORE_PIN56_BITMASK (1<<(CORE_PIN56_BIT)) +#define CORE_PIN57_BITMASK (1<<(CORE_PIN57_BIT)) +#define CORE_PIN58_BITMASK (1<<(CORE_PIN58_BIT)) +#define CORE_PIN59_BITMASK (1<<(CORE_PIN59_BIT)) +#define CORE_PIN60_BITMASK (1<<(CORE_PIN60_BIT)) +#define CORE_PIN61_BITMASK (1<<(CORE_PIN61_BIT)) +#define CORE_PIN62_BITMASK (1<<(CORE_PIN62_BIT)) +#define CORE_PIN63_BITMASK (1<<(CORE_PIN63_BIT)) + + +#define CORE_PIN0_PORTREG GPIOB_PDOR +#define CORE_PIN1_PORTREG GPIOB_PDOR +#define CORE_PIN2_PORTREG GPIOD_PDOR +#define CORE_PIN3_PORTREG GPIOA_PDOR +#define CORE_PIN4_PORTREG GPIOA_PDOR +#define CORE_PIN5_PORTREG GPIOD_PDOR +#define CORE_PIN6_PORTREG GPIOD_PDOR +#define CORE_PIN7_PORTREG GPIOD_PDOR +#define CORE_PIN8_PORTREG GPIOD_PDOR +#define CORE_PIN9_PORTREG GPIOC_PDOR +#define CORE_PIN10_PORTREG GPIOC_PDOR +#define CORE_PIN11_PORTREG GPIOC_PDOR +#define CORE_PIN12_PORTREG GPIOC_PDOR +#define CORE_PIN13_PORTREG GPIOC_PDOR +#define CORE_PIN14_PORTREG GPIOD_PDOR +#define CORE_PIN15_PORTREG GPIOC_PDOR +#define CORE_PIN16_PORTREG GPIOB_PDOR +#define CORE_PIN17_PORTREG GPIOB_PDOR +#define CORE_PIN18_PORTREG GPIOB_PDOR +#define CORE_PIN19_PORTREG GPIOB_PDOR +#define CORE_PIN20_PORTREG GPIOD_PDOR +#define CORE_PIN21_PORTREG GPIOD_PDOR +#define CORE_PIN22_PORTREG GPIOC_PDOR +#define CORE_PIN23_PORTREG GPIOC_PDOR +#define CORE_PIN24_PORTREG GPIOE_PDOR +#define CORE_PIN25_PORTREG GPIOA_PDOR +#define CORE_PIN26_PORTREG GPIOA_PDOR +#define CORE_PIN27_PORTREG GPIOA_PDOR +#define CORE_PIN28_PORTREG GPIOA_PDOR +#define CORE_PIN29_PORTREG GPIOB_PDOR +#define CORE_PIN30_PORTREG GPIOB_PDOR +#define CORE_PIN31_PORTREG GPIOB_PDOR +#define CORE_PIN32_PORTREG GPIOB_PDOR +#define CORE_PIN33_PORTREG GPIOE_PDOR +#define CORE_PIN34_PORTREG GPIOE_PDOR +#define CORE_PIN35_PORTREG GPIOC_PDOR +#define CORE_PIN36_PORTREG GPIOC_PDOR +#define CORE_PIN37_PORTREG GPIOC_PDOR +#define CORE_PIN38_PORTREG GPIOC_PDOR +#define CORE_PIN39_PORTREG GPIOA_PDOR +#define CORE_PIN40_PORTREG GPIOA_PDOR +#define CORE_PIN41_PORTREG GPIOA_PDOR +#define CORE_PIN42_PORTREG GPIOA_PDOR +#define CORE_PIN43_PORTREG GPIOB_PDOR +#define CORE_PIN44_PORTREG GPIOB_PDOR +#define CORE_PIN45_PORTREG GPIOB_PDOR +#define CORE_PIN46_PORTREG GPIOB_PDOR +#define CORE_PIN47_PORTREG GPIOD_PDOR +#define CORE_PIN48_PORTREG GPIOD_PDOR +#define CORE_PIN49_PORTREG GPIOB_PDOR +#define CORE_PIN50_PORTREG GPIOB_PDOR +#define CORE_PIN51_PORTREG GPIOD_PDOR +#define CORE_PIN52_PORTREG GPIOD_PDOR +#define CORE_PIN53_PORTREG GPIOD_PDOR +#define CORE_PIN54_PORTREG GPIOD_PDOR +#define CORE_PIN55_PORTREG GPIOD_PDOR +#define CORE_PIN56_PORTREG GPIOE_PDOR +#define CORE_PIN57_PORTREG GPIOE_PDOR +#define CORE_PIN58_PORTREG GPIOE_PDOR +#define CORE_PIN59_PORTREG GPIOE_PDOR +#define CORE_PIN60_PORTREG GPIOE_PDOR +#define CORE_PIN61_PORTREG GPIOE_PDOR +#define CORE_PIN62_PORTREG GPIOE_PDOR +#define CORE_PIN63_PORTREG GPIOE_PDOR + +#define CORE_PIN0_PORTSET GPIOB_PSOR +#define CORE_PIN1_PORTSET GPIOB_PSOR +#define CORE_PIN2_PORTSET GPIOD_PSOR +#define CORE_PIN3_PORTSET GPIOA_PSOR +#define CORE_PIN4_PORTSET GPIOA_PSOR +#define CORE_PIN5_PORTSET GPIOD_PSOR +#define CORE_PIN6_PORTSET GPIOD_PSOR +#define CORE_PIN7_PORTSET GPIOD_PSOR +#define CORE_PIN8_PORTSET GPIOD_PSOR +#define CORE_PIN9_PORTSET GPIOC_PSOR +#define CORE_PIN10_PORTSET GPIOC_PSOR +#define CORE_PIN11_PORTSET GPIOC_PSOR +#define CORE_PIN12_PORTSET GPIOC_PSOR +#define CORE_PIN13_PORTSET GPIOC_PSOR +#define CORE_PIN14_PORTSET GPIOD_PSOR +#define CORE_PIN15_PORTSET GPIOC_PSOR +#define CORE_PIN16_PORTSET GPIOB_PSOR +#define CORE_PIN17_PORTSET GPIOB_PSOR +#define CORE_PIN18_PORTSET GPIOB_PSOR +#define CORE_PIN19_PORTSET GPIOB_PSOR +#define CORE_PIN20_PORTSET GPIOD_PSOR +#define CORE_PIN21_PORTSET GPIOD_PSOR +#define CORE_PIN22_PORTSET GPIOC_PSOR +#define CORE_PIN23_PORTSET GPIOC_PSOR +#define CORE_PIN24_PORTSET GPIOE_PSOR +#define CORE_PIN25_PORTSET GPIOA_PSOR +#define CORE_PIN26_PORTSET GPIOA_PSOR +#define CORE_PIN27_PORTSET GPIOA_PSOR +#define CORE_PIN28_PORTSET GPIOA_PSOR +#define CORE_PIN29_PORTSET GPIOB_PSOR +#define CORE_PIN30_PORTSET GPIOB_PSOR +#define CORE_PIN31_PORTSET GPIOB_PSOR +#define CORE_PIN32_PORTSET GPIOB_PSOR +#define CORE_PIN33_PORTSET GPIOE_PSOR +#define CORE_PIN34_PORTSET GPIOE_PSOR +#define CORE_PIN35_PORTSET GPIOC_PSOR +#define CORE_PIN36_PORTSET GPIOC_PSOR +#define CORE_PIN37_PORTSET GPIOC_PSOR +#define CORE_PIN38_PORTSET GPIOC_PSOR +#define CORE_PIN39_PORTSET GPIOA_PSOR +#define CORE_PIN40_PORTSET GPIOA_PSOR +#define CORE_PIN41_PORTSET GPIOA_PSOR +#define CORE_PIN42_PORTSET GPIOA_PSOR +#define CORE_PIN43_PORTSET GPIOB_PSOR +#define CORE_PIN44_PORTSET GPIOB_PSOR +#define CORE_PIN45_PORTSET GPIOB_PSOR +#define CORE_PIN46_PORTSET GPIOB_PSOR +#define CORE_PIN47_PORTSET GPIOD_PSOR +#define CORE_PIN48_PORTSET GPIOD_PSOR +#define CORE_PIN49_PORTSET GPIOB_PSOR +#define CORE_PIN50_PORTSET GPIOB_PSOR +#define CORE_PIN51_PORTSET GPIOD_PSOR +#define CORE_PIN52_PORTSET GPIOD_PSOR +#define CORE_PIN53_PORTSET GPIOD_PSOR +#define CORE_PIN54_PORTSET GPIOD_PSOR +#define CORE_PIN55_PORTSET GPIOD_PSOR +#define CORE_PIN56_PORTSET GPIOE_PSOR +#define CORE_PIN57_PORTSET GPIOE_PSOR +#define CORE_PIN58_PORTSET GPIOE_PSOR +#define CORE_PIN59_PORTSET GPIOE_PSOR +#define CORE_PIN60_PORTSET GPIOE_PSOR +#define CORE_PIN61_PORTSET GPIOE_PSOR +#define CORE_PIN62_PORTSET GPIOE_PSOR +#define CORE_PIN63_PORTSET GPIOE_PSOR + +#define CORE_PIN0_PORTCLEAR GPIOB_PCOR +#define CORE_PIN1_PORTCLEAR GPIOB_PCOR +#define CORE_PIN2_PORTCLEAR GPIOD_PCOR +#define CORE_PIN3_PORTCLEAR GPIOA_PCOR +#define CORE_PIN4_PORTCLEAR GPIOA_PCOR +#define CORE_PIN5_PORTCLEAR GPIOD_PCOR +#define CORE_PIN6_PORTCLEAR GPIOD_PCOR +#define CORE_PIN7_PORTCLEAR GPIOD_PCOR +#define CORE_PIN8_PORTCLEAR GPIOD_PCOR +#define CORE_PIN9_PORTCLEAR GPIOC_PCOR +#define CORE_PIN10_PORTCLEAR GPIOC_PCOR +#define CORE_PIN11_PORTCLEAR GPIOC_PCOR +#define CORE_PIN12_PORTCLEAR GPIOC_PCOR +#define CORE_PIN13_PORTCLEAR GPIOC_PCOR +#define CORE_PIN14_PORTCLEAR GPIOD_PCOR +#define CORE_PIN15_PORTCLEAR GPIOC_PCOR +#define CORE_PIN16_PORTCLEAR GPIOB_PCOR +#define CORE_PIN17_PORTCLEAR GPIOB_PCOR +#define CORE_PIN18_PORTCLEAR GPIOB_PCOR +#define CORE_PIN19_PORTCLEAR GPIOB_PCOR +#define CORE_PIN20_PORTCLEAR GPIOD_PCOR +#define CORE_PIN21_PORTCLEAR GPIOD_PCOR +#define CORE_PIN22_PORTCLEAR GPIOC_PCOR +#define CORE_PIN23_PORTCLEAR GPIOC_PCOR +#define CORE_PIN24_PORTCLEAR GPIOE_PCOR +#define CORE_PIN25_PORTCLEAR GPIOA_PCOR +#define CORE_PIN26_PORTCLEAR GPIOA_PCOR +#define CORE_PIN27_PORTCLEAR GPIOA_PCOR +#define CORE_PIN28_PORTCLEAR GPIOA_PCOR +#define CORE_PIN29_PORTCLEAR GPIOB_PCOR +#define CORE_PIN30_PORTCLEAR GPIOB_PCOR +#define CORE_PIN31_PORTCLEAR GPIOB_PCOR +#define CORE_PIN32_PORTCLEAR GPIOB_PCOR +#define CORE_PIN33_PORTCLEAR GPIOE_PCOR +#define CORE_PIN34_PORTCLEAR GPIOE_PCOR +#define CORE_PIN35_PORTCLEAR GPIOC_PCOR +#define CORE_PIN36_PORTCLEAR GPIOC_PCOR +#define CORE_PIN37_PORTCLEAR GPIOC_PCOR +#define CORE_PIN38_PORTCLEAR GPIOC_PCOR +#define CORE_PIN39_PORTCLEAR GPIOA_PCOR +#define CORE_PIN40_PORTCLEAR GPIOA_PCOR +#define CORE_PIN41_PORTCLEAR GPIOA_PCOR +#define CORE_PIN42_PORTCLEAR GPIOA_PCOR +#define CORE_PIN43_PORTCLEAR GPIOB_PCOR +#define CORE_PIN44_PORTCLEAR GPIOB_PCOR +#define CORE_PIN45_PORTCLEAR GPIOB_PCOR +#define CORE_PIN46_PORTCLEAR GPIOB_PCOR +#define CORE_PIN47_PORTCLEAR GPIOD_PCOR +#define CORE_PIN48_PORTCLEAR GPIOD_PCOR +#define CORE_PIN49_PORTCLEAR GPIOB_PCOR +#define CORE_PIN50_PORTCLEAR GPIOB_PCOR +#define CORE_PIN51_PORTCLEAR GPIOD_PCOR +#define CORE_PIN52_PORTCLEAR GPIOD_PCOR +#define CORE_PIN53_PORTCLEAR GPIOD_PCOR +#define CORE_PIN54_PORTCLEAR GPIOD_PCOR +#define CORE_PIN55_PORTCLEAR GPIOD_PCOR +#define CORE_PIN56_PORTCLEAR GPIOE_PCOR +#define CORE_PIN57_PORTCLEAR GPIOE_PCOR +#define CORE_PIN58_PORTCLEAR GPIOE_PCOR +#define CORE_PIN59_PORTCLEAR GPIOE_PCOR +#define CORE_PIN60_PORTCLEAR GPIOE_PCOR +#define CORE_PIN61_PORTCLEAR GPIOE_PCOR +#define CORE_PIN62_PORTCLEAR GPIOE_PCOR +#define CORE_PIN63_PORTCLEAR GPIOE_PCOR + +#define CORE_PIN0_DDRREG GPIOB_PDDR +#define CORE_PIN1_DDRREG GPIOB_PDDR +#define CORE_PIN2_DDRREG GPIOD_PDDR +#define CORE_PIN3_DDRREG GPIOA_PDDR +#define CORE_PIN4_DDRREG GPIOA_PDDR +#define CORE_PIN5_DDRREG GPIOD_PDDR +#define CORE_PIN6_DDRREG GPIOD_PDDR +#define CORE_PIN7_DDRREG GPIOD_PDDR +#define CORE_PIN8_DDRREG GPIOD_PDDR +#define CORE_PIN9_DDRREG GPIOC_PDDR +#define CORE_PIN10_DDRREG GPIOC_PDDR +#define CORE_PIN11_DDRREG GPIOC_PDDR +#define CORE_PIN12_DDRREG GPIOC_PDDR +#define CORE_PIN13_DDRREG GPIOC_PDDR +#define CORE_PIN14_DDRREG GPIOD_PDDR +#define CORE_PIN15_DDRREG GPIOC_PDDR +#define CORE_PIN16_DDRREG GPIOB_PDDR +#define CORE_PIN17_DDRREG GPIOB_PDDR +#define CORE_PIN18_DDRREG GPIOB_PDDR +#define CORE_PIN19_DDRREG GPIOB_PDDR +#define CORE_PIN20_DDRREG GPIOD_PDDR +#define CORE_PIN21_DDRREG GPIOD_PDDR +#define CORE_PIN22_DDRREG GPIOC_PDDR +#define CORE_PIN23_DDRREG GPIOC_PDDR +#define CORE_PIN24_DDRREG GPIOE_PDDR +#define CORE_PIN25_DDRREG GPIOA_PDDR +#define CORE_PIN26_DDRREG GPIOA_PDDR +#define CORE_PIN27_DDRREG GPIOA_PDDR +#define CORE_PIN28_DDRREG GPIOA_PDDR +#define CORE_PIN29_DDRREG GPIOB_PDDR +#define CORE_PIN30_DDRREG GPIOB_PDDR +#define CORE_PIN31_DDRREG GPIOB_PDDR +#define CORE_PIN32_DDRREG GPIOB_PDDR +#define CORE_PIN33_DDRREG GPIOE_PDDR +#define CORE_PIN34_DDRREG GPIOE_PDDR +#define CORE_PIN35_DDRREG GPIOC_PDDR +#define CORE_PIN36_DDRREG GPIOC_PDDR +#define CORE_PIN37_DDRREG GPIOC_PDDR +#define CORE_PIN38_DDRREG GPIOC_PDDR +#define CORE_PIN39_DDRREG GPIOA_PDDR +#define CORE_PIN40_DDRREG GPIOA_PDDR +#define CORE_PIN41_DDRREG GPIOA_PDDR +#define CORE_PIN42_DDRREG GPIOA_PDDR +#define CORE_PIN43_DDRREG GPIOB_PDDR +#define CORE_PIN44_DDRREG GPIOB_PDDR +#define CORE_PIN45_DDRREG GPIOB_PDDR +#define CORE_PIN46_DDRREG GPIOB_PDDR +#define CORE_PIN47_DDRREG GPIOD_PDDR +#define CORE_PIN48_DDRREG GPIOD_PDDR +#define CORE_PIN49_DDRREG GPIOB_PDDR +#define CORE_PIN50_DDRREG GPIOB_PDDR +#define CORE_PIN51_DDRREG GPIOD_PDDR +#define CORE_PIN52_DDRREG GPIOD_PDDR +#define CORE_PIN53_DDRREG GPIOD_PDDR +#define CORE_PIN54_DDRREG GPIOD_PDDR +#define CORE_PIN55_DDRREG GPIOD_PDDR +#define CORE_PIN56_DDRREG GPIOE_PDDR +#define CORE_PIN57_DDRREG GPIOE_PDDR +#define CORE_PIN58_DDRREG GPIOE_PDDR +#define CORE_PIN59_DDRREG GPIOE_PDDR +#define CORE_PIN60_DDRREG GPIOE_PDDR +#define CORE_PIN61_DDRREG GPIOE_PDDR +#define CORE_PIN62_DDRREG GPIOE_PDDR +#define CORE_PIN63_DDRREG GPIOE_PDDR + +#define CORE_PIN0_PINREG GPIOB_PDIR +#define CORE_PIN1_PINREG GPIOB_PDIR +#define CORE_PIN2_PINREG GPIOD_PDIR +#define CORE_PIN3_PINREG GPIOA_PDIR +#define CORE_PIN4_PINREG GPIOA_PDIR +#define CORE_PIN5_PINREG GPIOD_PDIR +#define CORE_PIN6_PINREG GPIOD_PDIR +#define CORE_PIN7_PINREG GPIOD_PDIR +#define CORE_PIN8_PINREG GPIOD_PDIR +#define CORE_PIN9_PINREG GPIOC_PDIR +#define CORE_PIN10_PINREG GPIOC_PDIR +#define CORE_PIN11_PINREG GPIOC_PDIR +#define CORE_PIN12_PINREG GPIOC_PDIR +#define CORE_PIN13_PINREG GPIOC_PDIR +#define CORE_PIN14_PINREG GPIOD_PDIR +#define CORE_PIN15_PINREG GPIOC_PDIR +#define CORE_PIN16_PINREG GPIOB_PDIR +#define CORE_PIN17_PINREG GPIOB_PDIR +#define CORE_PIN18_PINREG GPIOB_PDIR +#define CORE_PIN19_PINREG GPIOB_PDIR +#define CORE_PIN20_PINREG GPIOD_PDIR +#define CORE_PIN21_PINREG GPIOD_PDIR +#define CORE_PIN22_PINREG GPIOC_PDIR +#define CORE_PIN23_PINREG GPIOC_PDIR +#define CORE_PIN24_PINREG GPIOE_PDIR +#define CORE_PIN25_PINREG GPIOA_PDIR +#define CORE_PIN26_PINREG GPIOA_PDIR +#define CORE_PIN27_PINREG GPIOA_PDIR +#define CORE_PIN28_PINREG GPIOA_PDIR +#define CORE_PIN29_PINREG GPIOB_PDIR +#define CORE_PIN30_PINREG GPIOB_PDIR +#define CORE_PIN31_PINREG GPIOB_PDIR +#define CORE_PIN32_PINREG GPIOB_PDIR +#define CORE_PIN33_PINREG GPIOE_PDIR +#define CORE_PIN34_PINREG GPIOE_PDIR +#define CORE_PIN35_PINREG GPIOC_PDIR +#define CORE_PIN36_PINREG GPIOC_PDIR +#define CORE_PIN37_PINREG GPIOC_PDIR +#define CORE_PIN38_PINREG GPIOC_PDIR +#define CORE_PIN39_PINREG GPIOA_PDIR +#define CORE_PIN40_PINREG GPIOA_PDIR +#define CORE_PIN41_PINREG GPIOA_PDIR +#define CORE_PIN42_PINREG GPIOA_PDIR +#define CORE_PIN43_PINREG GPIOB_PDIR +#define CORE_PIN44_PINREG GPIOB_PDIR +#define CORE_PIN45_PINREG GPIOB_PDIR +#define CORE_PIN46_PINREG GPIOB_PDIR +#define CORE_PIN47_PINREG GPIOD_PDIR +#define CORE_PIN48_PINREG GPIOD_PDIR +#define CORE_PIN49_PINREG GPIOB_PDIR +#define CORE_PIN50_PINREG GPIOB_PDIR +#define CORE_PIN51_PINREG GPIOD_PDIR +#define CORE_PIN52_PINREG GPIOD_PDIR +#define CORE_PIN53_PINREG GPIOD_PDIR +#define CORE_PIN54_PINREG GPIOD_PDIR +#define CORE_PIN55_PINREG GPIOD_PDIR +#define CORE_PIN56_PINREG GPIOE_PDIR +#define CORE_PIN57_PINREG GPIOE_PDIR +#define CORE_PIN58_PINREG GPIOE_PDIR +#define CORE_PIN59_PINREG GPIOE_PDIR +#define CORE_PIN60_PINREG GPIOE_PDIR +#define CORE_PIN61_PINREG GPIOE_PDIR +#define CORE_PIN62_PINREG GPIOE_PDIR +#define CORE_PIN63_PINREG GPIOE_PDIR + +#define CORE_PIN0_CONFIG PORTB_PCR16 +#define CORE_PIN1_CONFIG PORTB_PCR17 +#define CORE_PIN2_CONFIG PORTD_PCR0 +#define CORE_PIN3_CONFIG PORTA_PCR12 +#define CORE_PIN4_CONFIG PORTA_PCR13 +#define CORE_PIN5_CONFIG PORTD_PCR7 +#define CORE_PIN6_CONFIG PORTD_PCR4 +#define CORE_PIN7_CONFIG PORTD_PCR2 +#define CORE_PIN8_CONFIG PORTD_PCR3 +#define CORE_PIN9_CONFIG PORTC_PCR3 +#define CORE_PIN10_CONFIG PORTC_PCR4 +#define CORE_PIN11_CONFIG PORTC_PCR6 +#define CORE_PIN12_CONFIG PORTC_PCR7 +#define CORE_PIN13_CONFIG PORTC_PCR5 +#define CORE_PIN14_CONFIG PORTD_PCR1 +#define CORE_PIN15_CONFIG PORTC_PCR0 +#define CORE_PIN16_CONFIG PORTB_PCR0 +#define CORE_PIN17_CONFIG PORTB_PCR1 +#define CORE_PIN18_CONFIG PORTB_PCR3 +#define CORE_PIN19_CONFIG PORTB_PCR2 +#define CORE_PIN20_CONFIG PORTD_PCR5 +#define CORE_PIN21_CONFIG PORTD_PCR6 +#define CORE_PIN22_CONFIG PORTC_PCR1 +#define CORE_PIN23_CONFIG PORTC_PCR2 +#define CORE_PIN24_CONFIG PORTE_PCR26 +#define CORE_PIN25_CONFIG PORTA_PCR5 +#define CORE_PIN26_CONFIG PORTA_PCR14 +#define CORE_PIN27_CONFIG PORTA_PCR15 +#define CORE_PIN28_CONFIG PORTA_PCR16 +#define CORE_PIN29_CONFIG PORTB_PCR18 +#define CORE_PIN30_CONFIG PORTB_PCR19 +#define CORE_PIN31_CONFIG PORTB_PCR10 +#define CORE_PIN32_CONFIG PORTB_PCR11 +#define CORE_PIN33_CONFIG PORTE_PCR24 +#define CORE_PIN34_CONFIG PORTE_PCR25 +#define CORE_PIN35_CONFIG PORTC_PCR8 +#define CORE_PIN36_CONFIG PORTC_PCR9 +#define CORE_PIN37_CONFIG PORTC_PCR10 +#define CORE_PIN38_CONFIG PORTC_PCR11 +#define CORE_PIN39_CONFIG PORTA_PCR17 +#define CORE_PIN40_CONFIG PORTA_PCR28 +#define CORE_PIN41_CONFIG PORTA_PCR29 +#define CORE_PIN42_CONFIG PORTA_PCR26 +#define CORE_PIN43_CONFIG PORTB_PCR20 +#define CORE_PIN44_CONFIG PORTB_PCR22 +#define CORE_PIN45_CONFIG PORTB_PCR23 +#define CORE_PIN46_CONFIG PORTB_PCR21 +#define CORE_PIN47_CONFIG PORTD_PCR8 +#define CORE_PIN48_CONFIG PORTD_PCR9 +#define CORE_PIN49_CONFIG PORTB_PCR4 +#define CORE_PIN50_CONFIG PORTB_PCR5 +#define CORE_PIN51_CONFIG PORTD_PCR14 +#define CORE_PIN52_CONFIG PORTD_PCR13 +#define CORE_PIN53_CONFIG PORTD_PCR12 +#define CORE_PIN54_CONFIG PORTD_PCR15 +#define CORE_PIN55_CONFIG PORTD_PCR11 +#define CORE_PIN56_CONFIG PORTE_PCR10 +#define CORE_PIN57_CONFIG PORTE_PCR11 +#define CORE_PIN58_CONFIG PORTE_PCR0 +#define CORE_PIN59_CONFIG PORTE_PCR1 +#define CORE_PIN60_CONFIG PORTE_PCR2 +#define CORE_PIN61_CONFIG PORTE_PCR3 +#define CORE_PIN62_CONFIG PORTE_PCR4 +#define CORE_PIN63_CONFIG PORTE_PCR5 + +#define CORE_ADC0_PIN 14 +#define CORE_ADC1_PIN 15 +#define CORE_ADC2_PIN 16 +#define CORE_ADC3_PIN 17 +#define CORE_ADC4_PIN 18 +#define CORE_ADC5_PIN 19 +#define CORE_ADC6_PIN 20 +#define CORE_ADC7_PIN 21 +#define CORE_ADC8_PIN 22 +#define CORE_ADC9_PIN 23 +#define CORE_ADC10_PIN 64 +#define CORE_ADC11_PIN 65 +#define CORE_ADC12_PIN 31 +#define CORE_ADC13_PIN 32 +#define CORE_ADC14_PIN 33 +#define CORE_ADC15_PIN 34 +#define CORE_ADC16_PIN 35 +#define CORE_ADC17_PIN 36 +#define CORE_ADC18_PIN 37 +#define CORE_ADC19_PIN 38 +#define CORE_ADC20_PIN 39 +#define CORE_ADC21_PIN 66 +#define CORE_ADC22_PIN 67 +#define CORE_ADC23_PIN 49 +#define CORE_ADC24_PIN 50 +#define CORE_ADC25_PIN 68 +#define CORE_ADC26_PIN 69 + +#define CORE_RXD0_PIN 0 +#define CORE_TXD0_PIN 1 +#define CORE_RXD1_PIN 9 +#define CORE_TXD1_PIN 10 +#define CORE_RXD2_PIN 7 +#define CORE_TXD2_PIN 8 +#define CORE_RXD3_PIN 31 +#define CORE_TXD3_PIN 32 +#define CORE_RXD4_PIN 34 +#define CORE_TXD4_PIN 33 + +#define CORE_INT0_PIN 0 +#define CORE_INT1_PIN 1 +#define CORE_INT2_PIN 2 +#define CORE_INT3_PIN 3 +#define CORE_INT4_PIN 4 +#define CORE_INT5_PIN 5 +#define CORE_INT6_PIN 6 +#define CORE_INT7_PIN 7 +#define CORE_INT8_PIN 8 +#define CORE_INT9_PIN 9 +#define CORE_INT10_PIN 10 +#define CORE_INT11_PIN 11 +#define CORE_INT12_PIN 12 +#define CORE_INT13_PIN 13 +#define CORE_INT14_PIN 14 +#define CORE_INT15_PIN 15 +#define CORE_INT16_PIN 16 +#define CORE_INT17_PIN 17 +#define CORE_INT18_PIN 18 +#define CORE_INT19_PIN 19 +#define CORE_INT20_PIN 20 +#define CORE_INT21_PIN 21 +#define CORE_INT22_PIN 22 +#define CORE_INT23_PIN 23 +#define CORE_INT24_PIN 24 +#define CORE_INT25_PIN 25 +#define CORE_INT26_PIN 26 +#define CORE_INT27_PIN 27 +#define CORE_INT28_PIN 28 +#define CORE_INT29_PIN 29 +#define CORE_INT30_PIN 30 +#define CORE_INT31_PIN 31 +#define CORE_INT32_PIN 32 +#define CORE_INT33_PIN 33 +#define CORE_INT34_PIN 34 +#define CORE_INT35_PIN 35 +#define CORE_INT36_PIN 36 +#define CORE_INT37_PIN 37 +#define CORE_INT38_PIN 38 +#define CORE_INT39_PIN 39 +#define CORE_INT40_PIN 40 +#define CORE_INT41_PIN 41 +#define CORE_INT42_PIN 42 +#define CORE_INT43_PIN 43 +#define CORE_INT44_PIN 44 +#define CORE_INT45_PIN 45 +#define CORE_INT46_PIN 46 +#define CORE_INT47_PIN 47 +#define CORE_INT48_PIN 48 +#define CORE_INT49_PIN 49 +#define CORE_INT50_PIN 50 +#define CORE_INT51_PIN 51 +#define CORE_INT52_PIN 52 +#define CORE_INT53_PIN 53 +#define CORE_INT54_PIN 54 +#define CORE_INT55_PIN 55 +#define CORE_INT56_PIN 56 +#define CORE_INT57_PIN 57 +#define CORE_INT58_PIN 58 +#define CORE_INT59_PIN 59 +#define CORE_INT60_PIN 60 +#define CORE_INT61_PIN 61 +#define CORE_INT62_PIN 62 +#define CORE_INT63_PIN 63 +#define CORE_INT_EVERY_PIN 1 + +#endif + + +#if defined(__MK20DX128__) +#define CORE_FTM0_CH0_PIN 22 +#define CORE_FTM0_CH1_PIN 23 +#define CORE_FTM0_CH2_PIN 9 +#define CORE_FTM0_CH3_PIN 10 +#define CORE_FTM0_CH4_PIN 6 +#define CORE_FTM0_CH5_PIN 20 +#define CORE_FTM0_CH6_PIN 21 +#define CORE_FTM0_CH7_PIN 5 +#define CORE_FTM1_CH0_PIN 3 +#define CORE_FTM1_CH1_PIN 4 +#elif defined(__MK20DX256__) +#define CORE_FTM0_CH0_PIN 22 +#define CORE_FTM0_CH1_PIN 23 +#define CORE_FTM0_CH2_PIN 9 +#define CORE_FTM0_CH3_PIN 10 +#define CORE_FTM0_CH4_PIN 6 +#define CORE_FTM0_CH5_PIN 20 +#define CORE_FTM0_CH6_PIN 21 +#define CORE_FTM0_CH7_PIN 5 +#define CORE_FTM1_CH0_PIN 3 +#define CORE_FTM1_CH1_PIN 4 +#define CORE_FTM2_CH0_PIN 32 +#define CORE_FTM2_CH1_PIN 25 +#elif defined(__MKL26Z64__) +#define CORE_TPM0_CH0_PIN 22 +#define CORE_TPM0_CH1_PIN 23 +#define CORE_TPM0_CH2_PIN 9 +#define CORE_TPM0_CH3_PIN 10 +#define CORE_TPM0_CH4_PIN 6 +#define CORE_TPM0_CH5_PIN 20 +#define CORE_TPM1_CH0_PIN 16 +#define CORE_TPM1_CH1_PIN 17 +#define CORE_TPM2_CH0_PIN 3 +#define CORE_TPM2_CH1_PIN 4 +#elif defined(__MK64FX512__) +#define CORE_FTM0_CH0_PIN 22 +#define CORE_FTM0_CH1_PIN 23 +#define CORE_FTM0_CH2_PIN 9 +#define CORE_FTM0_CH3_PIN 10 +#define CORE_FTM0_CH4_PIN 6 +#define CORE_FTM0_CH5_PIN 20 +#define CORE_FTM0_CH6_PIN 21 +#define CORE_FTM0_CH7_PIN 5 +#define CORE_FTM1_CH0_PIN 3 +#define CORE_FTM1_CH1_PIN 4 +#define CORE_FTM2_CH0_PIN 29 +#define CORE_FTM2_CH1_PIN 30 +#define CORE_FTM3_CH0_PIN 2 +#define CORE_FTM3_CH1_PIN 14 +#define CORE_FTM3_CH2_PIN 7 +#define CORE_FTM3_CH3_PIN 8 +#define CORE_FTM3_CH4_PIN 35 +#define CORE_FTM3_CH5_PIN 36 +#define CORE_FTM3_CH6_PIN 37 +#define CORE_FTM3_CH7_PIN 38 +#elif defined(__MK66FX1M0__) +#define CORE_FTM0_CH0_PIN 22 +#define CORE_FTM0_CH1_PIN 23 +#define CORE_FTM0_CH2_PIN 9 +#define CORE_FTM0_CH3_PIN 10 +#define CORE_FTM0_CH4_PIN 6 +#define CORE_FTM0_CH5_PIN 20 +#define CORE_FTM0_CH6_PIN 21 +#define CORE_FTM0_CH7_PIN 5 +#define CORE_FTM1_CH0_PIN 3 +#define CORE_FTM1_CH1_PIN 4 +#define CORE_FTM2_CH0_PIN 29 +#define CORE_FTM2_CH1_PIN 30 +#define CORE_FTM3_CH0_PIN 2 +#define CORE_FTM3_CH1_PIN 14 +#define CORE_FTM3_CH2_PIN 7 +#define CORE_FTM3_CH3_PIN 8 +#define CORE_FTM3_CH4_PIN 35 +#define CORE_FTM3_CH5_PIN 36 +#define CORE_FTM3_CH6_PIN 37 +#define CORE_FTM3_CH7_PIN 38 +#define CORE_TPM1_CH0_PIN 16 +#define CORE_TPM1_CH1_PIN 17 +#endif + + +#ifdef __cplusplus +extern "C" { +#endif + +void digitalWrite(uint8_t pin, uint8_t val); +// static inline void digitalWriteFast(uint8_t pin, uint8_t val) __attribute__((always_inline, unused)); +// static inline void digitalWriteFast(uint8_t pin, uint8_t val) +// { +// if (__builtin_constant_p(pin)) { +// if (val) { +// if (pin == 0) { +// CORE_PIN0_PORTSET = CORE_PIN0_BITMASK; +// } else if (pin == 1) { +// CORE_PIN1_PORTSET = CORE_PIN1_BITMASK; +// } else if (pin == 2) { +// CORE_PIN2_PORTSET = CORE_PIN2_BITMASK; +// } else if (pin == 3) { +// CORE_PIN3_PORTSET = CORE_PIN3_BITMASK; +// } else if (pin == 4) { +// CORE_PIN4_PORTSET = CORE_PIN4_BITMASK; +// } else if (pin == 5) { +// CORE_PIN5_PORTSET = CORE_PIN5_BITMASK; +// } else if (pin == 6) { +// CORE_PIN6_PORTSET = CORE_PIN6_BITMASK; +// } else if (pin == 7) { +// CORE_PIN7_PORTSET = CORE_PIN7_BITMASK; +// } else if (pin == 8) { +// CORE_PIN8_PORTSET = CORE_PIN8_BITMASK; +// } else if (pin == 9) { +// CORE_PIN9_PORTSET = CORE_PIN9_BITMASK; +// } else if (pin == 10) { +// CORE_PIN10_PORTSET = CORE_PIN10_BITMASK; +// } else if (pin == 11) { +// CORE_PIN11_PORTSET = CORE_PIN11_BITMASK; +// } else if (pin == 12) { +// CORE_PIN12_PORTSET = CORE_PIN12_BITMASK; +// } else if (pin == 13) { +// CORE_PIN13_PORTSET = CORE_PIN13_BITMASK; +// } else if (pin == 14) { +// CORE_PIN14_PORTSET = CORE_PIN14_BITMASK; +// } else if (pin == 15) { +// CORE_PIN15_PORTSET = CORE_PIN15_BITMASK; +// } else if (pin == 16) { +// CORE_PIN16_PORTSET = CORE_PIN16_BITMASK; +// } else if (pin == 17) { +// CORE_PIN17_PORTSET = CORE_PIN17_BITMASK; +// } else if (pin == 18) { +// CORE_PIN18_PORTSET = CORE_PIN18_BITMASK; +// } else if (pin == 19) { +// CORE_PIN19_PORTSET = CORE_PIN19_BITMASK; +// } else if (pin == 20) { +// CORE_PIN20_PORTSET = CORE_PIN20_BITMASK; +// } else if (pin == 21) { +// CORE_PIN21_PORTSET = CORE_PIN21_BITMASK; +// } else if (pin == 22) { +// CORE_PIN22_PORTSET = CORE_PIN22_BITMASK; +// } else if (pin == 23) { +// CORE_PIN23_PORTSET = CORE_PIN23_BITMASK; +// } else if (pin == 24) { +// CORE_PIN24_PORTSET = CORE_PIN24_BITMASK; +// } else if (pin == 25) { +// CORE_PIN25_PORTSET = CORE_PIN25_BITMASK; +// } else if (pin == 26) { +// CORE_PIN26_PORTSET = CORE_PIN26_BITMASK; +// } +// #if defined(CORE_PIN27_PORTSET) +// else if (pin == 27) { +// CORE_PIN27_PORTSET = CORE_PIN27_BITMASK; +// } else if (pin == 28) { +// CORE_PIN28_PORTSET = CORE_PIN28_BITMASK; +// } else if (pin == 29) { +// CORE_PIN29_PORTSET = CORE_PIN29_BITMASK; +// } else if (pin == 30) { +// CORE_PIN30_PORTSET = CORE_PIN30_BITMASK; +// } else if (pin == 31) { +// CORE_PIN31_PORTSET = CORE_PIN31_BITMASK; +// } else if (pin == 32) { +// CORE_PIN32_PORTSET = CORE_PIN32_BITMASK; +// } else if (pin == 33) { +// CORE_PIN33_PORTSET = CORE_PIN33_BITMASK; +// } +// #endif +// #if defined(CORE_PIN34_PORTSET) +// else if (pin == 34) { +// CORE_PIN34_PORTSET = CORE_PIN34_BITMASK; +// } else if (pin == 35) { +// CORE_PIN35_PORTSET = CORE_PIN35_BITMASK; +// } else if (pin == 36) { +// CORE_PIN36_PORTSET = CORE_PIN36_BITMASK; +// } else if (pin == 37) { +// CORE_PIN37_PORTSET = CORE_PIN37_BITMASK; +// } else if (pin == 38) { +// CORE_PIN38_PORTSET = CORE_PIN38_BITMASK; +// } else if (pin == 39) { +// CORE_PIN39_PORTSET = CORE_PIN39_BITMASK; +// } else if (pin == 40) { +// CORE_PIN40_PORTSET = CORE_PIN40_BITMASK; +// } else if (pin == 41) { +// CORE_PIN41_PORTSET = CORE_PIN41_BITMASK; +// } else if (pin == 42) { +// CORE_PIN42_PORTSET = CORE_PIN42_BITMASK; +// } else if (pin == 43) { +// CORE_PIN43_PORTSET = CORE_PIN43_BITMASK; +// } else if (pin == 44) { +// CORE_PIN44_PORTSET = CORE_PIN44_BITMASK; +// } else if (pin == 45) { +// CORE_PIN45_PORTSET = CORE_PIN45_BITMASK; +// } else if (pin == 46) { +// CORE_PIN46_PORTSET = CORE_PIN46_BITMASK; +// } else if (pin == 47) { +// CORE_PIN47_PORTSET = CORE_PIN47_BITMASK; +// } else if (pin == 48) { +// CORE_PIN48_PORTSET = CORE_PIN48_BITMASK; +// } else if (pin == 49) { +// CORE_PIN49_PORTSET = CORE_PIN49_BITMASK; +// } else if (pin == 50) { +// CORE_PIN50_PORTSET = CORE_PIN50_BITMASK; +// } else if (pin == 51) { +// CORE_PIN51_PORTSET = CORE_PIN51_BITMASK; +// } else if (pin == 52) { +// CORE_PIN52_PORTSET = CORE_PIN52_BITMASK; +// } else if (pin == 53) { +// CORE_PIN53_PORTSET = CORE_PIN53_BITMASK; +// } else if (pin == 54) { +// CORE_PIN54_PORTSET = CORE_PIN54_BITMASK; +// } else if (pin == 55) { +// CORE_PIN55_PORTSET = CORE_PIN55_BITMASK; +// } else if (pin == 56) { +// CORE_PIN56_PORTSET = CORE_PIN56_BITMASK; +// } else if (pin == 57) { +// CORE_PIN57_PORTSET = CORE_PIN57_BITMASK; +// } else if (pin == 58) { +// CORE_PIN58_PORTSET = CORE_PIN58_BITMASK; +// } else if (pin == 59) { +// CORE_PIN59_PORTSET = CORE_PIN59_BITMASK; +// } else if (pin == 60) { +// CORE_PIN60_PORTSET = CORE_PIN60_BITMASK; +// } else if (pin == 61) { +// CORE_PIN61_PORTSET = CORE_PIN61_BITMASK; +// } else if (pin == 62) { +// CORE_PIN62_PORTSET = CORE_PIN62_BITMASK; +// } else if (pin == 63) { +// CORE_PIN63_PORTSET = CORE_PIN63_BITMASK; +// } +// #endif +// } else { +// if (pin == 0) { +// CORE_PIN0_PORTCLEAR = CORE_PIN0_BITMASK; +// } else if (pin == 1) { +// CORE_PIN1_PORTCLEAR = CORE_PIN1_BITMASK; +// } else if (pin == 2) { +// CORE_PIN2_PORTCLEAR = CORE_PIN2_BITMASK; +// } else if (pin == 3) { +// CORE_PIN3_PORTCLEAR = CORE_PIN3_BITMASK; +// } else if (pin == 4) { +// CORE_PIN4_PORTCLEAR = CORE_PIN4_BITMASK; +// } else if (pin == 5) { +// CORE_PIN5_PORTCLEAR = CORE_PIN5_BITMASK; +// } else if (pin == 6) { +// CORE_PIN6_PORTCLEAR = CORE_PIN6_BITMASK; +// } else if (pin == 7) { +// CORE_PIN7_PORTCLEAR = CORE_PIN7_BITMASK; +// } else if (pin == 8) { +// CORE_PIN8_PORTCLEAR = CORE_PIN8_BITMASK; +// } else if (pin == 9) { +// CORE_PIN9_PORTCLEAR = CORE_PIN9_BITMASK; +// } else if (pin == 10) { +// CORE_PIN10_PORTCLEAR = CORE_PIN10_BITMASK; +// } else if (pin == 11) { +// CORE_PIN11_PORTCLEAR = CORE_PIN11_BITMASK; +// } else if (pin == 12) { +// CORE_PIN12_PORTCLEAR = CORE_PIN12_BITMASK; +// } else if (pin == 13) { +// CORE_PIN13_PORTCLEAR = CORE_PIN13_BITMASK; +// } else if (pin == 14) { +// CORE_PIN14_PORTCLEAR = CORE_PIN14_BITMASK; +// } else if (pin == 15) { +// CORE_PIN15_PORTCLEAR = CORE_PIN15_BITMASK; +// } else if (pin == 16) { +// CORE_PIN16_PORTCLEAR = CORE_PIN16_BITMASK; +// } else if (pin == 17) { +// CORE_PIN17_PORTCLEAR = CORE_PIN17_BITMASK; +// } else if (pin == 18) { +// CORE_PIN18_PORTCLEAR = CORE_PIN18_BITMASK; +// } else if (pin == 19) { +// CORE_PIN19_PORTCLEAR = CORE_PIN19_BITMASK; +// } else if (pin == 20) { +// CORE_PIN20_PORTCLEAR = CORE_PIN20_BITMASK; +// } else if (pin == 21) { +// CORE_PIN21_PORTCLEAR = CORE_PIN21_BITMASK; +// } else if (pin == 22) { +// CORE_PIN22_PORTCLEAR = CORE_PIN22_BITMASK; +// } else if (pin == 23) { +// CORE_PIN23_PORTCLEAR = CORE_PIN23_BITMASK; +// } else if (pin == 24) { +// CORE_PIN24_PORTCLEAR = CORE_PIN24_BITMASK; +// } else if (pin == 25) { +// CORE_PIN25_PORTCLEAR = CORE_PIN25_BITMASK; +// } else if (pin == 26) { +// CORE_PIN26_PORTCLEAR = CORE_PIN26_BITMASK; +// } +// #if defined(CORE_PIN27_PORTCLEAR) +// else if (pin == 27) { +// CORE_PIN27_PORTCLEAR = CORE_PIN27_BITMASK; +// } else if (pin == 28) { +// CORE_PIN28_PORTCLEAR = CORE_PIN28_BITMASK; +// } else if (pin == 29) { +// CORE_PIN29_PORTCLEAR = CORE_PIN29_BITMASK; +// } else if (pin == 30) { +// CORE_PIN30_PORTCLEAR = CORE_PIN30_BITMASK; +// } else if (pin == 31) { +// CORE_PIN31_PORTCLEAR = CORE_PIN31_BITMASK; +// } else if (pin == 32) { +// CORE_PIN32_PORTCLEAR = CORE_PIN32_BITMASK; +// } else if (pin == 33) { +// CORE_PIN33_PORTCLEAR = CORE_PIN33_BITMASK; +// } +// #endif +// #if defined(CORE_PIN34_PORTCLEAR) +// else if (pin == 34) { +// CORE_PIN34_PORTCLEAR = CORE_PIN34_BITMASK; +// } else if (pin == 35) { +// CORE_PIN35_PORTCLEAR = CORE_PIN35_BITMASK; +// } else if (pin == 36) { +// CORE_PIN36_PORTCLEAR = CORE_PIN36_BITMASK; +// } else if (pin == 37) { +// CORE_PIN37_PORTCLEAR = CORE_PIN37_BITMASK; +// } else if (pin == 38) { +// CORE_PIN38_PORTCLEAR = CORE_PIN38_BITMASK; +// } else if (pin == 39) { +// CORE_PIN39_PORTCLEAR = CORE_PIN39_BITMASK; +// } else if (pin == 40) { +// CORE_PIN40_PORTCLEAR = CORE_PIN40_BITMASK; +// } else if (pin == 41) { +// CORE_PIN41_PORTCLEAR = CORE_PIN41_BITMASK; +// } else if (pin == 42) { +// CORE_PIN42_PORTCLEAR = CORE_PIN42_BITMASK; +// } else if (pin == 43) { +// CORE_PIN43_PORTCLEAR = CORE_PIN43_BITMASK; +// } else if (pin == 44) { +// CORE_PIN44_PORTCLEAR = CORE_PIN44_BITMASK; +// } else if (pin == 45) { +// CORE_PIN45_PORTCLEAR = CORE_PIN45_BITMASK; +// } else if (pin == 46) { +// CORE_PIN46_PORTCLEAR = CORE_PIN46_BITMASK; +// } else if (pin == 47) { +// CORE_PIN47_PORTCLEAR = CORE_PIN47_BITMASK; +// } else if (pin == 48) { +// CORE_PIN48_PORTCLEAR = CORE_PIN48_BITMASK; +// } else if (pin == 49) { +// CORE_PIN49_PORTCLEAR = CORE_PIN49_BITMASK; +// } else if (pin == 50) { +// CORE_PIN50_PORTCLEAR = CORE_PIN50_BITMASK; +// } else if (pin == 51) { +// CORE_PIN51_PORTCLEAR = CORE_PIN51_BITMASK; +// } else if (pin == 52) { +// CORE_PIN52_PORTCLEAR = CORE_PIN52_BITMASK; +// } else if (pin == 53) { +// CORE_PIN53_PORTCLEAR = CORE_PIN53_BITMASK; +// } else if (pin == 54) { +// CORE_PIN54_PORTCLEAR = CORE_PIN54_BITMASK; +// } else if (pin == 55) { +// CORE_PIN55_PORTCLEAR = CORE_PIN55_BITMASK; +// } else if (pin == 56) { +// CORE_PIN56_PORTCLEAR = CORE_PIN56_BITMASK; +// } else if (pin == 57) { +// CORE_PIN57_PORTCLEAR = CORE_PIN57_BITMASK; +// } else if (pin == 58) { +// CORE_PIN58_PORTCLEAR = CORE_PIN58_BITMASK; +// } else if (pin == 59) { +// CORE_PIN59_PORTCLEAR = CORE_PIN59_BITMASK; +// } else if (pin == 60) { +// CORE_PIN60_PORTCLEAR = CORE_PIN60_BITMASK; +// } else if (pin == 61) { +// CORE_PIN61_PORTCLEAR = CORE_PIN61_BITMASK; +// } else if (pin == 62) { +// CORE_PIN62_PORTCLEAR = CORE_PIN62_BITMASK; +// } else if (pin == 63) { +// CORE_PIN63_PORTCLEAR = CORE_PIN63_BITMASK; +// } +// #endif +// } +// } else { +// if (val) { +// *portSetRegister(pin) = digitalPinToBitMask(pin); +// } else { +// *portClearRegister(pin) = digitalPinToBitMask(pin); +// } +// } +// } + +uint8_t digitalRead(uint8_t pin); +// static inline uint8_t digitalReadFast(uint8_t pin) __attribute__((always_inline, unused)); +// static inline uint8_t digitalReadFast(uint8_t pin) +// { +// if (__builtin_constant_p(pin)) { +// if (pin == 0) { +// return (CORE_PIN0_PINREG & CORE_PIN0_BITMASK) ? 1 : 0; +// } else if (pin == 1) { +// return (CORE_PIN1_PINREG & CORE_PIN1_BITMASK) ? 1 : 0; +// } else if (pin == 2) { +// return (CORE_PIN2_PINREG & CORE_PIN2_BITMASK) ? 1 : 0; +// } else if (pin == 3) { +// return (CORE_PIN3_PINREG & CORE_PIN3_BITMASK) ? 1 : 0; +// } else if (pin == 4) { +// return (CORE_PIN4_PINREG & CORE_PIN4_BITMASK) ? 1 : 0; +// } else if (pin == 5) { +// return (CORE_PIN5_PINREG & CORE_PIN5_BITMASK) ? 1 : 0; +// } else if (pin == 6) { +// return (CORE_PIN6_PINREG & CORE_PIN6_BITMASK) ? 1 : 0; +// } else if (pin == 7) { +// return (CORE_PIN7_PINREG & CORE_PIN7_BITMASK) ? 1 : 0; +// } else if (pin == 8) { +// return (CORE_PIN8_PINREG & CORE_PIN8_BITMASK) ? 1 : 0; +// } else if (pin == 9) { +// return (CORE_PIN9_PINREG & CORE_PIN9_BITMASK) ? 1 : 0; +// } else if (pin == 10) { +// return (CORE_PIN10_PINREG & CORE_PIN10_BITMASK) ? 1 : 0; +// } else if (pin == 11) { +// return (CORE_PIN11_PINREG & CORE_PIN11_BITMASK) ? 1 : 0; +// } else if (pin == 12) { +// return (CORE_PIN12_PINREG & CORE_PIN12_BITMASK) ? 1 : 0; +// } else if (pin == 13) { +// return (CORE_PIN13_PINREG & CORE_PIN13_BITMASK) ? 1 : 0; +// } else if (pin == 14) { +// return (CORE_PIN14_PINREG & CORE_PIN14_BITMASK) ? 1 : 0; +// } else if (pin == 15) { +// return (CORE_PIN15_PINREG & CORE_PIN15_BITMASK) ? 1 : 0; +// } else if (pin == 16) { +// return (CORE_PIN16_PINREG & CORE_PIN16_BITMASK) ? 1 : 0; +// } else if (pin == 17) { +// return (CORE_PIN17_PINREG & CORE_PIN17_BITMASK) ? 1 : 0; +// } else if (pin == 18) { +// return (CORE_PIN18_PINREG & CORE_PIN18_BITMASK) ? 1 : 0; +// } else if (pin == 19) { +// return (CORE_PIN19_PINREG & CORE_PIN19_BITMASK) ? 1 : 0; +// } else if (pin == 20) { +// return (CORE_PIN20_PINREG & CORE_PIN20_BITMASK) ? 1 : 0; +// } else if (pin == 21) { +// return (CORE_PIN21_PINREG & CORE_PIN21_BITMASK) ? 1 : 0; +// } else if (pin == 22) { +// return (CORE_PIN22_PINREG & CORE_PIN22_BITMASK) ? 1 : 0; +// } else if (pin == 23) { +// return (CORE_PIN23_PINREG & CORE_PIN23_BITMASK) ? 1 : 0; +// } else if (pin == 24) { +// return (CORE_PIN24_PINREG & CORE_PIN24_BITMASK) ? 1 : 0; +// } else if (pin == 25) { +// return (CORE_PIN25_PINREG & CORE_PIN25_BITMASK) ? 1 : 0; +// } else if (pin == 26) { +// return (CORE_PIN26_PINREG & CORE_PIN26_BITMASK) ? 1 : 0; +// } +// #if defined(CORE_PIN27_PINREG) +// else if (pin == 27) { +// return (CORE_PIN27_PINREG & CORE_PIN27_BITMASK) ? 1 : 0; +// } else if (pin == 28) { +// return (CORE_PIN28_PINREG & CORE_PIN28_BITMASK) ? 1 : 0; +// } else if (pin == 29) { +// return (CORE_PIN29_PINREG & CORE_PIN29_BITMASK) ? 1 : 0; +// } else if (pin == 30) { +// return (CORE_PIN30_PINREG & CORE_PIN30_BITMASK) ? 1 : 0; +// } else if (pin == 31) { +// return (CORE_PIN31_PINREG & CORE_PIN31_BITMASK) ? 1 : 0; +// } else if (pin == 32) { +// return (CORE_PIN32_PINREG & CORE_PIN32_BITMASK) ? 1 : 0; +// } else if (pin == 33) { +// return (CORE_PIN33_PINREG & CORE_PIN33_BITMASK) ? 1 : 0; +// } +// #endif +// #if defined(CORE_PIN34_PINREG) +// else if (pin == 34) { +// return (CORE_PIN34_PINREG & CORE_PIN34_BITMASK) ? 1 : 0; +// } else if (pin == 35) { +// return (CORE_PIN35_PINREG & CORE_PIN35_BITMASK) ? 1 : 0; +// } else if (pin == 36) { +// return (CORE_PIN36_PINREG & CORE_PIN36_BITMASK) ? 1 : 0; +// } else if (pin == 37) { +// return (CORE_PIN37_PINREG & CORE_PIN37_BITMASK) ? 1 : 0; +// } else if (pin == 38) { +// return (CORE_PIN38_PINREG & CORE_PIN38_BITMASK) ? 1 : 0; +// } else if (pin == 39) { +// return (CORE_PIN39_PINREG & CORE_PIN39_BITMASK) ? 1 : 0; +// } else if (pin == 40) { +// return (CORE_PIN40_PINREG & CORE_PIN40_BITMASK) ? 1 : 0; +// } else if (pin == 41) { +// return (CORE_PIN41_PINREG & CORE_PIN41_BITMASK) ? 1 : 0; +// } else if (pin == 42) { +// return (CORE_PIN42_PINREG & CORE_PIN42_BITMASK) ? 1 : 0; +// } else if (pin == 43) { +// return (CORE_PIN43_PINREG & CORE_PIN43_BITMASK) ? 1 : 0; +// } else if (pin == 44) { +// return (CORE_PIN44_PINREG & CORE_PIN44_BITMASK) ? 1 : 0; +// } else if (pin == 45) { +// return (CORE_PIN45_PINREG & CORE_PIN45_BITMASK) ? 1 : 0; +// } else if (pin == 46) { +// return (CORE_PIN46_PINREG & CORE_PIN46_BITMASK) ? 1 : 0; +// } else if (pin == 47) { +// return (CORE_PIN47_PINREG & CORE_PIN47_BITMASK) ? 1 : 0; +// } else if (pin == 48) { +// return (CORE_PIN48_PINREG & CORE_PIN48_BITMASK) ? 1 : 0; +// } else if (pin == 49) { +// return (CORE_PIN49_PINREG & CORE_PIN49_BITMASK) ? 1 : 0; +// } else if (pin == 50) { +// return (CORE_PIN50_PINREG & CORE_PIN50_BITMASK) ? 1 : 0; +// } else if (pin == 51) { +// return (CORE_PIN51_PINREG & CORE_PIN51_BITMASK) ? 1 : 0; +// } else if (pin == 52) { +// return (CORE_PIN52_PINREG & CORE_PIN52_BITMASK) ? 1 : 0; +// } else if (pin == 53) { +// return (CORE_PIN53_PINREG & CORE_PIN53_BITMASK) ? 1 : 0; +// } else if (pin == 54) { +// return (CORE_PIN54_PINREG & CORE_PIN54_BITMASK) ? 1 : 0; +// } else if (pin == 55) { +// return (CORE_PIN55_PINREG & CORE_PIN55_BITMASK) ? 1 : 0; +// } else if (pin == 56) { +// return (CORE_PIN56_PINREG & CORE_PIN56_BITMASK) ? 1 : 0; +// } else if (pin == 57) { +// return (CORE_PIN57_PINREG & CORE_PIN57_BITMASK) ? 1 : 0; +// } else if (pin == 58) { +// return (CORE_PIN58_PINREG & CORE_PIN58_BITMASK) ? 1 : 0; +// } else if (pin == 59) { +// return (CORE_PIN59_PINREG & CORE_PIN59_BITMASK) ? 1 : 0; +// } else if (pin == 60) { +// return (CORE_PIN60_PINREG & CORE_PIN60_BITMASK) ? 1 : 0; +// } else if (pin == 61) { +// return (CORE_PIN61_PINREG & CORE_PIN61_BITMASK) ? 1 : 0; +// } else if (pin == 62) { +// return (CORE_PIN62_PINREG & CORE_PIN62_BITMASK) ? 1 : 0; +// } else if (pin == 63) { +// return (CORE_PIN63_PINREG & CORE_PIN63_BITMASK) ? 1 : 0; +// } +// #endif +// else { +// return 0; +// } +// } else { +// #if defined(KINETISK) +// return *portInputRegister(pin); +// #else +// return (*portInputRegister(pin) & digitalPinToBitMask(pin)) ? 1 : 0; +// #endif +// } +// } + + +void pinMode(uint8_t pin, uint8_t mode); +void init_pins(void); +void analogWrite(uint8_t pin, int val); + +uint32_t analogWriteRes(uint32_t bits); +static inline uint32_t analogWriteResolution(uint32_t bits) { return analogWriteRes(bits); } +void analogWriteFrequency(uint8_t pin, float frequency); +// void analogWriteDAC0(int val); +// void analogWriteDAC1(int val); +// #ifdef __cplusplus +// void attachInterruptVector(IRQ_NUMBER_t irq, void (*function)(void)); +// #else +// void attachInterruptVector(enum IRQ_NUMBER_t irq, void (*function)(void)); +// #endif +// void attachInterrupt(uint8_t pin, void (*function)(void), int mode); +// void detachInterrupt(uint8_t pin); +// void _init_Teensyduino_internal_(void); + +int analogRead(uint8_t pin); +void analogReference(uint8_t type); +void analogReadRes(unsigned int bits); +static inline void analogReadResolution(unsigned int bits) { analogReadRes(bits); } +void analogReadAveraging(unsigned int num); +void analog_init(void); + + +#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) +#define DEFAULT 0 +#define INTERNAL 2 +#define INTERNAL1V2 2 +#define INTERNAL1V1 2 +#define EXTERNAL 0 + +#elif defined(__MKL26Z64__) +#define DEFAULT 0 +#define INTERNAL 0 +#define EXTERNAL 1 +#endif + + +int touchRead(uint8_t pin); + + +// static inline void shiftOut(uint8_t, uint8_t, uint8_t, uint8_t) __attribute__((always_inline, unused)); +extern void _shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value) __attribute__((noinline)); +extern void shiftOut_lsbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value) __attribute__((noinline)); +extern void shiftOut_msbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value) __attribute__((noinline)); + +// static inline void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value) +// { +// if (__builtin_constant_p(bitOrder)) { +// if (bitOrder == LSBFIRST) { +// shiftOut_lsbFirst(dataPin, clockPin, value); +// } else { +// shiftOut_msbFirst(dataPin, clockPin, value); +// } +// } else { +// _shiftOut(dataPin, clockPin, bitOrder, value); +// } +// } + +static inline uint8_t shiftIn(uint8_t, uint8_t, uint8_t) __attribute__((always_inline, unused)); +extern uint8_t _shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) __attribute__((noinline)); +extern uint8_t shiftIn_lsbFirst(uint8_t dataPin, uint8_t clockPin) __attribute__((noinline)); +extern uint8_t shiftIn_msbFirst(uint8_t dataPin, uint8_t clockPin) __attribute__((noinline)); + +static inline uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) +{ + if (__builtin_constant_p(bitOrder)) { + if (bitOrder == LSBFIRST) { + return shiftIn_lsbFirst(dataPin, clockPin); + } else { + return shiftIn_msbFirst(dataPin, clockPin); + } + } else { + return _shiftIn(dataPin, clockPin, bitOrder); + } +} + +void _reboot_Teensyduino_(void); +void _restart_Teensyduino_(void); + +void yield(void); + +void delay(uint32_t msec); + +// extern volatile uint32_t systick_millis_count; + +// static inline uint32_t millis(void) __attribute__((always_inline, unused)); +// static inline uint32_t millis(void) +// { +// // Reading a volatile variable to another volatile +// // seems redundant, but isn't for some cases. +// // Eventually this should probably be replaced by a +// // proper memory barrier or other technique. Please +// // do not remove this "redundant" code without +// // carefully verifying the case mentioned here: +// // +// // https://forum.pjrc.com/threads/17469-millis%28%29-on-teensy-3?p=104924&viewfull=1#post104924 +// // +// volatile uint32_t ret = systick_millis_count; // single aligned 32 bit is atomic +// return ret; +// } + +// uint32_t micros(void); +/* +static inline void delayMicroseconds(uint32_t) __attribute__((always_inline, unused)); +static inline void delayMicroseconds(uint32_t usec) +{ +#if F_CPU == 256000000 + uint32_t n = usec * 85; +#elif F_CPU == 240000000 + uint32_t n = usec * 80; +#elif F_CPU == 216000000 + uint32_t n = usec * 72; +#elif F_CPU == 192000000 + uint32_t n = usec * 64; +#elif F_CPU == 180000000 + uint32_t n = usec * 60; +#elif F_CPU == 168000000 + uint32_t n = usec * 56; +#elif F_CPU == 144000000 + uint32_t n = usec * 48; +#elif F_CPU == 120000000 + uint32_t n = usec * 40; +#elif F_CPU == 96000000 + uint32_t n = usec << 5; +#elif F_CPU == 72000000 + uint32_t n = usec * 24; +#elif F_CPU == 48000000 + uint32_t n = usec << 4; +#elif F_CPU == 24000000 + uint32_t n = usec << 3; +#elif F_CPU == 16000000 + uint32_t n = usec << 2; +#elif F_CPU == 8000000 + uint32_t n = usec << 1; +#elif F_CPU == 4000000 + uint32_t n = usec; +#elif F_CPU == 2000000 + uint32_t n = usec >> 1; +#endif + // changed because a delay of 1 micro Sec @ 2MHz will be 0 + if (n == 0) return; + __asm__ volatile( + "L_%=_delayMicroseconds:" "\n\t" +#if F_CPU < 24000000 + "nop" "\n\t" +#endif +#ifdef KINETISL + "sub %0, #1" "\n\t" + "bne L_%=_delayMicroseconds" "\n" + : "+l" (n) : +#else + "subs %0, #1" "\n\t" + "bne L_%=_delayMicroseconds" "\n" + : "+r" (n) : +#endif + ); +} +*/ +#ifdef __cplusplus +} +#endif + + + + + + + + +#ifdef __cplusplus +extern "C" { +#endif +unsigned long rtc_get(void); +void rtc_set(unsigned long t); +void rtc_compensate(int adjust); +#ifdef __cplusplus +} +class teensy3_clock_class +{ +public: + static unsigned long get(void) __attribute__((always_inline)) { return rtc_get(); } + static void set(unsigned long t) __attribute__((always_inline)) { rtc_set(t); } + static void compensate(int adj) __attribute__((always_inline)) { rtc_compensate(adj); } +}; +extern teensy3_clock_class Teensy3Clock; +#endif + + + + +#endif diff --git a/simulation/include/gfxfont.h b/simulation/include/gfxfont.h new file mode 100644 index 0000000..c691de6 --- /dev/null +++ b/simulation/include/gfxfont.h @@ -0,0 +1,29 @@ +// Font structures for newer Adafruit_GFX (1.1 and later). +// Example fonts are included in 'Fonts' directory. +// To use a font in your Arduino sketch, #include the corresponding .h +// file and pass address of GFXfont struct to setFont(). Pass NULL to +// revert to 'classic' fixed-space bitmap font. + +#ifndef _GFXFONT_H_ +#define _GFXFONT_H_ + +/// Font data stored PER GLYPH +typedef struct { + uint16_t bitmapOffset; ///< Pointer into GFXfont->bitmap + uint8_t width; ///< Bitmap dimensions in pixels + uint8_t height; ///< Bitmap dimensions in pixels + uint8_t xAdvance; ///< Distance to advance cursor (x axis) + int8_t xOffset; ///< X dist from cursor pos to UL corner + int8_t yOffset; ///< Y dist from cursor pos to UL corner +} GFXglyph; + +/// Data stored for FONT AS A WHOLE +typedef struct { + uint8_t *bitmap; ///< Glyph bitmaps, concatenated + GFXglyph *glyph; ///< Glyph array + uint8_t first; ///< ASCII extents (first char) + uint8_t last; ///< ASCII extents (last char) + uint8_t yAdvance; ///< Newline distance (y axis) +} GFXfont; + +#endif // _GFXFONT_H_ diff --git a/simulation/include/pins_arduino.h b/simulation/include/pins_arduino.h new file mode 100644 index 0000000..56d31e4 --- /dev/null +++ b/simulation/include/pins_arduino.h @@ -0,0 +1,283 @@ +/* Teensyduino Core Library + * http://www.pjrc.com/teensy/ + * Copyright (c) 2017 PJRC.COM, LLC. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * 1. The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * 2. If the Software is incorporated into a build system that allows + * selection among a list of target devices, then similar target + * devices manufactured by PJRC.COM must be included in the list of + * target devices and selectable in the same manner. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef pins_macros_for_arduino_compatibility_h +#define pins_macros_for_arduino_compatibility_h + +#include + +// A0-A9 are always digital 14-23, for Arduino compatibility +#define PIN_A0 (14) +#define PIN_A1 (15) +#define PIN_A2 (16) +#define PIN_A3 (17) +#define PIN_A4 (18) +#define PIN_A5 (19) +#define PIN_A6 (20) +#define PIN_A7 (21) +#define PIN_A8 (22) +#define PIN_A9 (23) +const static uint8_t A0 = PIN_A0; +const static uint8_t A1 = PIN_A1; +const static uint8_t A2 = PIN_A2; +const static uint8_t A3 = PIN_A3; +const static uint8_t A4 = PIN_A4; +const static uint8_t A5 = PIN_A5; +const static uint8_t A6 = PIN_A6; +const static uint8_t A7 = PIN_A7; +const static uint8_t A8 = PIN_A8; +const static uint8_t A9 = PIN_A9; + +#if defined(__MK20DX128__) +#define PIN_A10 (34) +#define PIN_A11 (35) +#define PIN_A12 (36) +#define PIN_A13 (37) +const static uint8_t A10 = PIN_A10; +const static uint8_t A11 = PIN_A11; +const static uint8_t A12 = PIN_A12; +const static uint8_t A13 = PIN_A13; + +#elif defined(__MK20DX256__) +#define PIN_A10 (34) +#define PIN_A11 (35) +#define PIN_A12 (36) +#define PIN_A13 (37) +#define PIN_A14 (40) +#define PIN_A15 (26) +#define PIN_A16 (27) +#define PIN_A17 (28) +#define PIN_A18 (29) +#define PIN_A19 (30) +#define PIN_A20 (31) +const static uint8_t A10 = PIN_A10; +const static uint8_t A11 = PIN_A11; +const static uint8_t A12 = PIN_A12; +const static uint8_t A13 = PIN_A13; +const static uint8_t A14 = PIN_A14; +const static uint8_t A15 = PIN_A15; +const static uint8_t A16 = PIN_A16; +const static uint8_t A17 = PIN_A17; +const static uint8_t A18 = PIN_A18; +const static uint8_t A19 = PIN_A19; +const static uint8_t A20 = PIN_A20; + +#elif defined(__MKL26Z64__) +#define PIN_A10 (24) +#define PIN_A11 (25) +#define PIN_A12 (26) +const static uint8_t A10 = PIN_A10; +const static uint8_t A11 = PIN_A11; +const static uint8_t A12 = PIN_A12; + +#elif defined(__MK64FX512__) || defined(__MK66FX1M0__) +#define PIN_A10 (64) +#define PIN_A11 (65) +#define PIN_A12 (31) +#define PIN_A13 (32) +#define PIN_A14 (33) +#define PIN_A15 (34) +#define PIN_A16 (35) +#define PIN_A17 (36) +#define PIN_A18 (37) +#define PIN_A19 (38) +#define PIN_A20 (39) +#define PIN_A21 (66) +#define PIN_A22 (67) +#define PIN_A23 (49) +#define PIN_A24 (50) +#define PIN_A25 (68) +#define PIN_A26 (69) +const static uint8_t A10 = PIN_A10; +const static uint8_t A11 = PIN_A11; +const static uint8_t A12 = PIN_A12; +const static uint8_t A13 = PIN_A13; +const static uint8_t A14 = PIN_A14; +const static uint8_t A15 = PIN_A15; +const static uint8_t A16 = PIN_A16; +const static uint8_t A17 = PIN_A17; +const static uint8_t A18 = PIN_A18; +const static uint8_t A19 = PIN_A19; +const static uint8_t A20 = PIN_A20; +const static uint8_t A21 = PIN_A21; +const static uint8_t A22 = PIN_A22; +const static uint8_t A23 = PIN_A23; +const static uint8_t A24 = PIN_A24; +const static uint8_t A25 = PIN_A25; +const static uint8_t A26 = PIN_A26; +#endif + +#define LED_BUILTIN (13) + +#define PIN_SPI_SS (10) +#define PIN_SPI_MOSI (11) +#define PIN_SPI_MISO (12) +#define PIN_SPI_SCK (13) +const static uint8_t SS = 10; +const static uint8_t MOSI = 11; +const static uint8_t MISO = 12; +const static uint8_t SCK = 13; + +#define PIN_WIRE_SDA (18) +#define PIN_WIRE_SCL (19) +const static uint8_t SDA = 18; +const static uint8_t SCL = 19; + +#define PIN_SERIAL_RX (0) +#define PIN_SERIAL_TX (1) + + +#define NUM_DIGITAL_PINS CORE_NUM_DIGITAL +#define NUM_ANALOG_INPUTS CORE_NUM_ANALOG + + +#define NOT_AN_INTERRUPT -1 + + +#if defined(__MK20DX128__) + #define analogInputToDigitalPin(p) (((p) <= 9) ? (p) + 14 : (((p) <= 13) ? (p) + 24 : -1)) + #define digitalPinHasPWM(p) (((p) >= 3 && (p) <= 6) || (p) == 9 || (p) == 10 || ((p) >= 20 && (p) <= 23)) + #define digitalPinToInterrupt(p) ((p) < NUM_DIGITAL_PINS ? (p) : -1) +#elif defined(__MK20DX256__) + #define analogInputToDigitalPin(p) (((p) <= 9) ? (p) + 14 : (((p) <= 13) ? (p) + 24 : (((p) == 14) ? 40 : (((p) <= 20) ? (p) + 11 : -1)))) + #define digitalPinHasPWM(p) (((p) >= 3 && (p) <= 6) || (p) == 9 || (p) == 10 || ((p) >= 20 && (p) <= 23) || (p) == 25 || (p) == 32) + #define digitalPinToInterrupt(p) ((p) < NUM_DIGITAL_PINS ? (p) : -1) +#elif defined(__MKL26Z64__) + #define analogInputToDigitalPin(p) (((p) <= 9) ? (p) + 14 : (((p) <= 12) ? (p) + 14 : -1)) + #define digitalPinHasPWM(p) ((p) == 3 || (p) == 4 || (p) == 6 || (p) == 9 || (p) == 10 || (p) == 16 || (p) == 17 || (p) == 20 || (p) == 22 || (p) == 23) + #define digitalPinToInterrupt(p) ((((p) >= 2 && (p) <= 15) || ((p) >= 20 && (p) <= 23)) ? (p) : -1) +#elif defined(__MK64FX512__) || defined(__MK66FX1M0__) + // TODO analogInputToDigitalPin needs update... + #define analogInputToDigitalPin(p) (((p) <= 9) ? (p) + 14 : (((p) >= 12 && (p) <= 20) ? (p) + 19 : -1)) + #define digitalPinHasPWM(p) (((p) >= 2 && (p) <= 10) || (p) == 14 || ((p) >= 20 && (p) <= 23) || (p) == 29 || (p) == 30 || ((p) >= 35 && (p) <= 38)) + #define digitalPinToInterrupt(p) ((p) < NUM_DIGITAL_PINS ? (p) : -1) +#endif + +#define digitalPinToPCICR(p) ((volatile uint8_t *)0) +#define digitalPinToPCICRbit(p) (0) +#define digitalPinToPCIFR(p) ((volatile uint8_t *)0) +#define digitalPinToPCIFRbit(p) (0) +#define digitalPinToPCMSK(p) ((volatile uint8_t *)0) +#define digitalPinToPCMSKbit(p) (0) + + +#if defined(KINETISK) +struct digital_pin_bitband_and_config_table_struct { + volatile uint32_t *reg; + volatile uint32_t *config; +}; +extern const struct digital_pin_bitband_and_config_table_struct digital_pin_to_info_PGM[]; +// compatibility macros +#define digitalPinToPort(pin) (pin) +#define digitalPinToBitMask(pin) (1) +#define portOutputRegister(pin) ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 0)) +#define portSetRegister(pin) ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 32)) +#define portClearRegister(pin) ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 64)) +#define portToggleRegister(pin) ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 96)) +#define portInputRegister(pin) ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 128)) +#define portModeRegister(pin) ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 160)) +#define portConfigRegister(pin) ((volatile uint32_t *)(digital_pin_to_info_PGM[(pin)].config)) +#define digitalPinToPortReg(pin) (portOutputRegister(pin)) +#define digitalPinToBit(pin) (1) + +#elif defined(KINETISL) +struct digital_pin_bitband_and_config_table_struct { + volatile uint8_t *reg; + volatile uint32_t *config; + uint8_t mask; +}; +extern const struct digital_pin_bitband_and_config_table_struct digital_pin_to_info_PGM[]; +// compatibility macros +#define digitalPinToPort(pin) (pin) +#define digitalPinToBitMask(pin) (digital_pin_to_info_PGM[(pin)].mask) +#define portOutputRegister(pin) ((digital_pin_to_info_PGM[(pin)].reg + 0)) +#define portSetRegister(pin) ((digital_pin_to_info_PGM[(pin)].reg + 4)) +#define portClearRegister(pin) ((digital_pin_to_info_PGM[(pin)].reg + 8)) +#define portToggleRegister(pin) ((digital_pin_to_info_PGM[(pin)].reg + 12)) +#define portInputRegister(pin) ((digital_pin_to_info_PGM[(pin)].reg + 16)) +#define portModeRegister(pin) ((digital_pin_to_info_PGM[(pin)].reg + 20)) +#define portConfigRegister(pin) ((digital_pin_to_info_PGM[(pin)].config)) +#define digitalPinToPortReg(pin) (portOutputRegister(pin)) +//#define digitalPinToBit(pin) (1) + +#endif + + + +#define NOT_ON_TIMER 0 +static inline uint8_t digitalPinToTimer(uint8_t) __attribute__((always_inline, unused)); +static inline uint8_t digitalPinToTimer(uint8_t pin) +{ + if (pin >= 3 && pin <= 6) return pin - 2; + if (pin >= 9 && pin <= 10) return pin - 4; + if (pin >= 20 && pin <= 23) return pin - 13; + return NOT_ON_TIMER; +} + +// These serial port names are intended to allow libraries and architecture-neutral +// sketches to automatically default to the correct port name for a particular type +// of use. For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN, +// the first hardware serial port whose RX/TX pins are not dedicated to another use. +// +// SERIAL_PORT_MONITOR Port which normally prints to the Arduino Serial Monitor +// +// SERIAL_PORT_USBVIRTUAL Port which is USB virtual serial +// +// SERIAL_PORT_LINUXBRIDGE Port which connects to a Linux system via Bridge library +// +// SERIAL_PORT_HARDWARE Hardware serial port, physical RX & TX pins. +// +// SERIAL_PORT_HARDWARE_OPEN Hardware serial ports which are open for use. Their RX & TX +// pins are NOT connected to anything by default. +// +#if F_CPU >= 20000000 && !defined(USB_DISABLED) +#define SERIAL_PORT_MONITOR Serial +#else +#define SERIAL_PORT_MONITOR Serial1 +#endif +#define SERIAL_PORT_USBVIRTUAL Serial +#define SERIAL_PORT_HARDWARE Serial1 +#define SERIAL_PORT_HARDWARE1 Serial2 +#define SERIAL_PORT_HARDWARE2 Serial3 +#define SERIAL_PORT_HARDWARE_OPEN Serial1 +#define SERIAL_PORT_HARDWARE_OPEN1 Serial2 +#define SERIAL_PORT_HARDWARE_OPEN2 Serial3 +#if defined(__MK64FX512__) || defined(__MK66FX1M0__) +#define SERIAL_PORT_HARDWARE3 Serial4 +#define SERIAL_PORT_HARDWARE4 Serial5 +#define SERIAL_PORT_HARDWARE5 Serial6 +#define SERIAL_PORT_HARDWARE_OPEN3 Serial4 +#define SERIAL_PORT_HARDWARE_OPEN4 Serial5 +#define SERIAL_PORT_HARDWARE_OPEN5 Serial6 +#endif + +#define SerialUSB Serial + +#endif diff --git a/simulation/include/wiring_private.h b/simulation/include/wiring_private.h new file mode 100644 index 0000000..e69de29 diff --git a/simulation/readme.md b/simulation/readme.md new file mode 100644 index 0000000..ec60418 --- /dev/null +++ b/simulation/readme.md @@ -0,0 +1,19 @@ +## Simulator for NuEVI + +This is a simple SDL2 based simulator that runs the NuEVI firmware compiled for MacOS. This is for testing the menu and is not supposed to produce any midi events. + +### Requirements + + * The Filters library must be installed in ~/Documents/Arduino/libraries/Filters. + * SDL2 must be installed on your machine. + * You probably need to have XCode and XCodes command line tools installed as well. + + +### Know limitations + +Currently the only input simulated are the menu buttons (use the arrow keys). This means that all menu functions cannot be tested, since the rotator menu cannot be opened. There is also a limitation on some keyboard on how many buttons can be pressed at the same time. + + +### Future plans + + * Add a Dear ImGUI based UI for simulating all other inputs. \ No newline at end of file diff --git a/simulation/src/Adafruit_GFX_sim.cpp b/simulation/src/Adafruit_GFX_sim.cpp new file mode 100644 index 0000000..2958831 --- /dev/null +++ b/simulation/src/Adafruit_GFX_sim.cpp @@ -0,0 +1,1982 @@ +/* +This is the core graphics library for all our displays, providing a common +set of graphics primitives (points, lines, circles, etc.). It needs to be +paired with a hardware-specific library for each display device we carry +(to handle the lower-level functions). + +Adafruit invests time and resources providing this open source code, please +support Adafruit & open-source hardware by purchasing products from Adafruit! + +Copyright (c) 2013 Adafruit Industries. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +- Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + */ + +#include "Adafruit_GFX.h" +#include "glcdfont.c" +#ifdef __AVR__ + #include +#elif defined(ESP8266) || defined(ESP32) + #include +#endif + +// Many (but maybe not all) non-AVR board installs define macros +// for compatibility with existing PROGMEM-reading AVR code. +// Do our own checks and defines here for good measure... + +#ifndef pgm_read_byte + #define pgm_read_byte(addr) (*(const unsigned char *)(addr)) +#endif +#ifndef pgm_read_word + #define pgm_read_word(addr) (*(const unsigned short *)(addr)) +#endif +#ifndef pgm_read_dword + #define pgm_read_dword(addr) (*(const unsigned long *)(addr)) +#endif + +// Pointers are a peculiar case...typically 16-bit on AVR boards, +// 32 bits elsewhere. Try to accommodate both... + +#if !defined(__INT_MAX__) || (__INT_MAX__ > 0xFFFF) + #define pgm_read_pointer(addr) ((void *)pgm_read_dword(addr)) +#else + #define pgm_read_pointer(addr) ((void *)pgm_read_word(addr)) +#endif + +#ifndef min +#define min(a,b) (((a) < (b)) ? (a) : (b)) +#endif + +#ifndef _swap_int16_t +#define _swap_int16_t(a, b) { int16_t t = a; a = b; b = t; } +#endif + +/**************************************************************************/ +/*! + @brief Instatiate a GFX context for graphics! Can only be done by a superclass + @param w Display width, in pixels + @param h Display height, in pixels +*/ +/**************************************************************************/ +Adafruit_GFX::Adafruit_GFX(int16_t w, int16_t h): +WIDTH(w), HEIGHT(h) +{ + _width = WIDTH; + _height = HEIGHT; + rotation = 0; + cursor_y = cursor_x = 0; + textsize = 1; + textcolor = textbgcolor = 0xFFFF; + wrap = true; + _cp437 = false; + gfxFont = NULL; + + dimmed_ = false;; + enabled_ = true; + inverted_ = false; +} + +/**************************************************************************/ +/*! + @brief Write a line. Bresenham's algorithm - thx wikpedia + @param x0 Start point x coordinate + @param y0 Start point y coordinate + @param x1 End point x coordinate + @param y1 End point y coordinate + @param color 16-bit 5-6-5 Color to draw with +*/ +/**************************************************************************/ +void Adafruit_GFX::writeLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, + uint16_t color) { +#if defined(ESP8266) + yield(); +#endif + int16_t steep = abs(y1 - y0) > abs(x1 - x0); + if (steep) { + _swap_int16_t(x0, y0); + _swap_int16_t(x1, y1); + } + + if (x0 > x1) { + _swap_int16_t(x0, x1); + _swap_int16_t(y0, y1); + } + + int16_t dx, dy; + dx = x1 - x0; + dy = abs(y1 - y0); + + int16_t err = dx / 2; + int16_t ystep; + + if (y0 < y1) { + ystep = 1; + } else { + ystep = -1; + } + + for (; x0<=x1; x0++) { + if (steep) { + writePixel(y0, x0, color); + } else { + writePixel(x0, y0, color); + } + err -= dy; + if (err < 0) { + y0 += ystep; + err += dx; + } + } +} + +/**************************************************************************/ +/*! + @brief Start a display-writing routine, overwrite in subclasses. +*/ +/**************************************************************************/ +void Adafruit_GFX::startWrite(){ +} + +/**************************************************************************/ +/*! + @brief Write a pixel, overwrite in subclasses if startWrite is defined! + @param x x coordinate + @param y y coordinate + @param color 16-bit 5-6-5 Color to fill with +*/ +/**************************************************************************/ +void Adafruit_GFX::writePixel(int16_t x, int16_t y, uint16_t color){ + drawPixel(x, y, color); +} + +/**************************************************************************/ +/*! + @brief Write a perfectly vertical line, overwrite in subclasses if startWrite is defined! + @param x Top-most x coordinate + @param y Top-most y coordinate + @param h Height in pixels + @param color 16-bit 5-6-5 Color to fill with +*/ +/**************************************************************************/ +void Adafruit_GFX::writeFastVLine(int16_t x, int16_t y, + int16_t h, uint16_t color) { + // Overwrite in subclasses if startWrite is defined! + // Can be just writeLine(x, y, x, y+h-1, color); + // or writeFillRect(x, y, 1, h, color); + drawFastVLine(x, y, h, color); +} + +/**************************************************************************/ +/*! + @brief Write a perfectly horizontal line, overwrite in subclasses if startWrite is defined! + @param x Left-most x coordinate + @param y Left-most y coordinate + @param w Width in pixels + @param color 16-bit 5-6-5 Color to fill with +*/ +/**************************************************************************/ +void Adafruit_GFX::writeFastHLine(int16_t x, int16_t y, + int16_t w, uint16_t color) { + // Overwrite in subclasses if startWrite is defined! + // Example: writeLine(x, y, x+w-1, y, color); + // or writeFillRect(x, y, w, 1, color); + drawFastHLine(x, y, w, color); +} + +/**************************************************************************/ +/*! + @brief Write a rectangle completely with one color, overwrite in subclasses if startWrite is defined! + @param x Top left corner x coordinate + @param y Top left corner y coordinate + @param w Width in pixels + @param h Height in pixels + @param color 16-bit 5-6-5 Color to fill with +*/ +/**************************************************************************/ +void Adafruit_GFX::writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h, + uint16_t color) { + // Overwrite in subclasses if desired! + fillRect(x,y,w,h,color); +} + +/**************************************************************************/ +/*! + @brief End a display-writing routine, overwrite in subclasses if startWrite is defined! +*/ +/**************************************************************************/ +void Adafruit_GFX::endWrite(){ +} + +/**************************************************************************/ +/*! + @brief Draw a perfectly vertical line (this is often optimized in a subclass!) + @param x Top-most x coordinate + @param y Top-most y coordinate + @param h Height in pixels + @param color 16-bit 5-6-5 Color to fill with +*/ +/**************************************************************************/ +void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y, + int16_t h, uint16_t color) { + startWrite(); + writeLine(x, y, x, y+h-1, color); + endWrite(); +} + +/**************************************************************************/ +/*! + @brief Draw a perfectly horizontal line (this is often optimized in a subclass!) + @param x Left-most x coordinate + @param y Left-most y coordinate + @param w Width in pixels + @param color 16-bit 5-6-5 Color to fill with +*/ +/**************************************************************************/ +void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y, + int16_t w, uint16_t color) { + startWrite(); + writeLine(x, y, x+w-1, y, color); + endWrite(); +} + +/**************************************************************************/ +/*! + @brief Fill a rectangle completely with one color. Update in subclasses if desired! + @param x Top left corner x coordinate + @param y Top left corner y coordinate + @param w Width in pixels + @param h Height in pixels + @param color 16-bit 5-6-5 Color to fill with +*/ +/**************************************************************************/ +void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, + uint16_t color) { + startWrite(); + for (int16_t i=x; i y1) _swap_int16_t(y0, y1); + drawFastVLine(x0, y0, y1 - y0 + 1, color); + } else if(y0 == y1){ + if(x0 > x1) _swap_int16_t(x0, x1); + drawFastHLine(x0, y0, x1 - x0 + 1, color); + } else { + startWrite(); + writeLine(x0, y0, x1, y1, color); + endWrite(); + } +} + +/**************************************************************************/ +/*! + @brief Draw a circle outline + @param x0 Center-point x coordinate + @param y0 Center-point y coordinate + @param r Radius of circle + @param color 16-bit 5-6-5 Color to draw with +*/ +/**************************************************************************/ +void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r, + uint16_t color) { +#if defined(ESP8266) + yield(); +#endif + int16_t f = 1 - r; + int16_t ddF_x = 1; + int16_t ddF_y = -2 * r; + int16_t x = 0; + int16_t y = r; + + startWrite(); + writePixel(x0 , y0+r, color); + writePixel(x0 , y0-r, color); + writePixel(x0+r, y0 , color); + writePixel(x0-r, y0 , color); + + while (x= 0) { + y--; + ddF_y += 2; + f += ddF_y; + } + x++; + ddF_x += 2; + f += ddF_x; + + writePixel(x0 + x, y0 + y, color); + writePixel(x0 - x, y0 + y, color); + writePixel(x0 + x, y0 - y, color); + writePixel(x0 - x, y0 - y, color); + writePixel(x0 + y, y0 + x, color); + writePixel(x0 - y, y0 + x, color); + writePixel(x0 + y, y0 - x, color); + writePixel(x0 - y, y0 - x, color); + } + endWrite(); +} + +/**************************************************************************/ +/*! + @brief Quarter-circle drawer, used to do circles and roundrects + @param x0 Center-point x coordinate + @param y0 Center-point y coordinate + @param r Radius of circle + @param cornername Mask bit #1 or bit #2 to indicate which quarters of the circle we're doing + @param color 16-bit 5-6-5 Color to draw with +*/ +/**************************************************************************/ +void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0, + int16_t r, uint8_t cornername, uint16_t color) { + int16_t f = 1 - r; + int16_t ddF_x = 1; + int16_t ddF_y = -2 * r; + int16_t x = 0; + int16_t y = r; + + while (x= 0) { + y--; + ddF_y += 2; + f += ddF_y; + } + x++; + ddF_x += 2; + f += ddF_x; + if (cornername & 0x4) { + writePixel(x0 + x, y0 + y, color); + writePixel(x0 + y, y0 + x, color); + } + if (cornername & 0x2) { + writePixel(x0 + x, y0 - y, color); + writePixel(x0 + y, y0 - x, color); + } + if (cornername & 0x8) { + writePixel(x0 - y, y0 + x, color); + writePixel(x0 - x, y0 + y, color); + } + if (cornername & 0x1) { + writePixel(x0 - y, y0 - x, color); + writePixel(x0 - x, y0 - y, color); + } + } +} + +/**************************************************************************/ +/*! + @brief Draw a circle with filled color + @param x0 Center-point x coordinate + @param y0 Center-point y coordinate + @param r Radius of circle + @param color 16-bit 5-6-5 Color to fill with +*/ +/**************************************************************************/ +void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r, + uint16_t color) { + startWrite(); + writeFastVLine(x0, y0-r, 2*r+1, color); + fillCircleHelper(x0, y0, r, 3, 0, color); + endWrite(); +} + + +/**************************************************************************/ +/*! + @brief Quarter-circle drawer with fill, used for circles and roundrects + @param x0 Center-point x coordinate + @param y0 Center-point y coordinate + @param r Radius of circle + @param corners Mask bits indicating which quarters we're doing + @param delta Offset from center-point, used for round-rects + @param color 16-bit 5-6-5 Color to fill with +*/ +/**************************************************************************/ +void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r, + uint8_t corners, int16_t delta, uint16_t color) { + + int16_t f = 1 - r; + int16_t ddF_x = 1; + int16_t ddF_y = -2 * r; + int16_t x = 0; + int16_t y = r; + int16_t px = x; + int16_t py = y; + + delta++; // Avoid some +1's in the loop + + while(x < y) { + if (f >= 0) { + y--; + ddF_y += 2; + f += ddF_y; + } + x++; + ddF_x += 2; + f += ddF_x; + // These checks avoid double-drawing certain lines, important + // for the SSD1306 library which has an INVERT drawing mode. + if(x < (y + 1)) { + if(corners & 1) writeFastVLine(x0+x, y0-y, 2*y+delta, color); + if(corners & 2) writeFastVLine(x0-x, y0-y, 2*y+delta, color); + } + if(y != py) { + if(corners & 1) writeFastVLine(x0+py, y0-px, 2*px+delta, color); + if(corners & 2) writeFastVLine(x0-py, y0-px, 2*px+delta, color); + py = y; + } + px = x; + } +} + +/**************************************************************************/ +/*! + @brief Draw a rectangle with no fill color + @param x Top left corner x coordinate + @param y Top left corner y coordinate + @param w Width in pixels + @param h Height in pixels + @param color 16-bit 5-6-5 Color to draw with +*/ +/**************************************************************************/ +void Adafruit_GFX::drawRect(int16_t x, int16_t y, int16_t w, int16_t h, + uint16_t color) { + startWrite(); + writeFastHLine(x, y, w, color); + writeFastHLine(x, y+h-1, w, color); + writeFastVLine(x, y, h, color); + writeFastVLine(x+w-1, y, h, color); + endWrite(); +} + +/**************************************************************************/ +/*! + @brief Draw a rounded rectangle with no fill color + @param x Top left corner x coordinate + @param y Top left corner y coordinate + @param w Width in pixels + @param h Height in pixels + @param r Radius of corner rounding + @param color 16-bit 5-6-5 Color to draw with +*/ +/**************************************************************************/ +void Adafruit_GFX::drawRoundRect(int16_t x, int16_t y, int16_t w, + int16_t h, int16_t r, uint16_t color) { + int16_t max_radius = ((w < h) ? w : h) / 2; // 1/2 minor axis + if(r > max_radius) r = max_radius; + // smarter version + startWrite(); + writeFastHLine(x+r , y , w-2*r, color); // Top + writeFastHLine(x+r , y+h-1, w-2*r, color); // Bottom + writeFastVLine(x , y+r , h-2*r, color); // Left + writeFastVLine(x+w-1, y+r , h-2*r, color); // Right + // draw four corners + drawCircleHelper(x+r , y+r , r, 1, color); + drawCircleHelper(x+w-r-1, y+r , r, 2, color); + drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color); + drawCircleHelper(x+r , y+h-r-1, r, 8, color); + endWrite(); +} + +/**************************************************************************/ +/*! + @brief Draw a rounded rectangle with fill color + @param x Top left corner x coordinate + @param y Top left corner y coordinate + @param w Width in pixels + @param h Height in pixels + @param r Radius of corner rounding + @param color 16-bit 5-6-5 Color to draw/fill with +*/ +/**************************************************************************/ +void Adafruit_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w, + int16_t h, int16_t r, uint16_t color) { + int16_t max_radius = ((w < h) ? w : h) / 2; // 1/2 minor axis + if(r > max_radius) r = max_radius; + // smarter version + startWrite(); + writeFillRect(x+r, y, w-2*r, h, color); + // draw four corners + fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color); + fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color); + endWrite(); +} + +/**************************************************************************/ +/*! + @brief Draw a triangle with no fill color + @param x0 Vertex #0 x coordinate + @param y0 Vertex #0 y coordinate + @param x1 Vertex #1 x coordinate + @param y1 Vertex #1 y coordinate + @param x2 Vertex #2 x coordinate + @param y2 Vertex #2 y coordinate + @param color 16-bit 5-6-5 Color to draw with +*/ +/**************************************************************************/ +void Adafruit_GFX::drawTriangle(int16_t x0, int16_t y0, + int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) { + drawLine(x0, y0, x1, y1, color); + drawLine(x1, y1, x2, y2, color); + drawLine(x2, y2, x0, y0, color); +} + +/**************************************************************************/ +/*! + @brief Draw a triangle with color-fill + @param x0 Vertex #0 x coordinate + @param y0 Vertex #0 y coordinate + @param x1 Vertex #1 x coordinate + @param y1 Vertex #1 y coordinate + @param x2 Vertex #2 x coordinate + @param y2 Vertex #2 y coordinate + @param color 16-bit 5-6-5 Color to fill/draw with +*/ +/**************************************************************************/ +void Adafruit_GFX::fillTriangle(int16_t x0, int16_t y0, + int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) { + + int16_t a, b, y, last; + + // Sort coordinates by Y order (y2 >= y1 >= y0) + if (y0 > y1) { + _swap_int16_t(y0, y1); _swap_int16_t(x0, x1); + } + if (y1 > y2) { + _swap_int16_t(y2, y1); _swap_int16_t(x2, x1); + } + if (y0 > y1) { + _swap_int16_t(y0, y1); _swap_int16_t(x0, x1); + } + + startWrite(); + if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing + a = b = x0; + if(x1 < a) a = x1; + else if(x1 > b) b = x1; + if(x2 < a) a = x2; + else if(x2 > b) b = x2; + writeFastHLine(a, y0, b-a+1, color); + endWrite(); + return; + } + + int16_t + dx01 = x1 - x0, + dy01 = y1 - y0, + dx02 = x2 - x0, + dy02 = y2 - y0, + dx12 = x2 - x1, + dy12 = y2 - y1; + int32_t + sa = 0, + sb = 0; + + // For upper part of triangle, find scanline crossings for segments + // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1 + // is included here (and second loop will be skipped, avoiding a /0 + // error there), otherwise scanline y1 is skipped here and handled + // in the second loop...which also avoids a /0 error here if y0=y1 + // (flat-topped triangle). + if(y1 == y2) last = y1; // Include y1 scanline + else last = y1-1; // Skip it + + for(y=y0; y<=last; y++) { + a = x0 + sa / dy01; + b = x0 + sb / dy02; + sa += dx01; + sb += dx02; + /* longhand: + a = x0 + (x1 - x0) * (y - y0) / (y1 - y0); + b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); + */ + if(a > b) _swap_int16_t(a,b); + writeFastHLine(a, y, b-a+1, color); + } + + // For lower part of triangle, find scanline crossings for segments + // 0-2 and 1-2. This loop is skipped if y1=y2. + sa = dx12 * (y - y1); + sb = dx02 * (y - y0); + for(; y<=y2; y++) { + a = x1 + sa / dy12; + b = x0 + sb / dy02; + sa += dx12; + sb += dx02; + /* longhand: + a = x1 + (x2 - x1) * (y - y1) / (y2 - y1); + b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); + */ + if(a > b) _swap_int16_t(a,b); + writeFastHLine(a, y, b-a+1, color); + } + endWrite(); +} + +// BITMAP / XBITMAP / GRAYSCALE / RGB BITMAP FUNCTIONS --------------------- + +/**************************************************************************/ +/*! + @brief Draw a PROGMEM-resident 1-bit image at the specified (x,y) position, using the specified foreground color (unset bits are transparent). + @param x Top left corner x coordinate + @param y Top left corner y coordinate + @param bitmap byte array with monochrome bitmap + @param w Width of bitmap in pixels + @param h Hieght of bitmap in pixels + @param color 16-bit 5-6-5 Color to draw with +*/ +/**************************************************************************/ +void Adafruit_GFX::drawBitmap(int16_t x, int16_t y, + const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color) { + + int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte + uint8_t byte = 0; + + startWrite(); + for(int16_t j=0; j>= 1; + else byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]); + // Nearly identical to drawBitmap(), only the bit order + // is reversed here (left-to-right = LSB to MSB): + if(byte & 0x01) writePixel(x+i, y, color); + } + } + endWrite(); +} + + +/**************************************************************************/ +/*! + @brief Draw a PROGMEM-resident 8-bit image (grayscale) at the specified (x,y) pos. + Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed. + @param x Top left corner x coordinate + @param y Top left corner y coordinate + @param bitmap byte array with grayscale bitmap + @param w Width of bitmap in pixels + @param h Hieght of bitmap in pixels +*/ +/**************************************************************************/ +void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y, + const uint8_t bitmap[], int16_t w, int16_t h) { + startWrite(); + for(int16_t j=0; j= _width) || // Clip right + (y >= _height) || // Clip bottom + ((x + 6 * size - 1) < 0) || // Clip left + ((y + 8 * size - 1) < 0)) // Clip top + return; + + if(!_cp437 && (c >= 176)) c++; // Handle 'classic' charset behavior + + startWrite(); + for(int8_t i=0; i<5; i++ ) { // Char bitmap = 5 columns + uint8_t line = pgm_read_byte(&font[c * 5 + i]); + for(int8_t j=0; j<8; j++, line >>= 1) { + if(line & 1) { + if(size == 1) + writePixel(x+i, y+j, color); + else + writeFillRect(x+i*size, y+j*size, size, size, color); + } else if(bg != color) { + if(size == 1) + writePixel(x+i, y+j, bg); + else + writeFillRect(x+i*size, y+j*size, size, size, bg); + } + } + } + if(bg != color) { // If opaque, draw vertical line for last column + if(size == 1) writeFastVLine(x+5, y, 8, bg); + else writeFillRect(x+5*size, y, size, 8*size, bg); + } + endWrite(); + + } else { // Custom font + + // Character is assumed previously filtered by write() to eliminate + // newlines, returns, non-printable characters, etc. Calling + // drawChar() directly with 'bad' characters of font may cause mayhem! + + c -= (uint8_t)pgm_read_byte(&gfxFont->first); + GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c]); + uint8_t *bitmap = (uint8_t *)pgm_read_pointer(&gfxFont->bitmap); + + uint16_t bo = pgm_read_word(&glyph->bitmapOffset); + uint8_t w = pgm_read_byte(&glyph->width), + h = pgm_read_byte(&glyph->height); + int8_t xo = pgm_read_byte(&glyph->xOffset), + yo = pgm_read_byte(&glyph->yOffset); + uint8_t xx, yy, bits = 0, bit = 0; + int16_t xo16 = 0, yo16 = 0; + + if(size > 1) { + xo16 = xo; + yo16 = yo; + } + + // Todo: Add character clipping here + + // NOTE: THERE IS NO 'BACKGROUND' COLOR OPTION ON CUSTOM FONTS. + // THIS IS ON PURPOSE AND BY DESIGN. The background color feature + // has typically been used with the 'classic' font to overwrite old + // screen contents with new data. This ONLY works because the + // characters are a uniform size; it's not a sensible thing to do with + // proportionally-spaced fonts with glyphs of varying sizes (and that + // may overlap). To replace previously-drawn text when using a custom + // font, use the getTextBounds() function to determine the smallest + // rectangle encompassing a string, erase the area with fillRect(), + // then draw new text. This WILL infortunately 'blink' the text, but + // is unavoidable. Drawing 'background' pixels will NOT fix this, + // only creates a new set of problems. Have an idea to work around + // this (a canvas object type for MCUs that can afford the RAM and + // displays supporting setAddrWindow() and pushColors()), but haven't + // implemented this yet. + + startWrite(); + for(yy=0; yy _width)) { // Off right? + cursor_x = 0; // Reset x to zero, + cursor_y += textsize * 8; // advance y one line + } + drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize); + cursor_x += textsize * 6; // Advance x one char + } + + } else { // Custom font + + if(c == '\n') { + cursor_x = 0; + cursor_y += (int16_t)textsize * + (uint8_t)pgm_read_byte(&gfxFont->yAdvance); + } else if(c != '\r') { + uint8_t first = pgm_read_byte(&gfxFont->first); + if((c >= first) && (c <= (uint8_t)pgm_read_byte(&gfxFont->last))) { + GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer( + &gfxFont->glyph))[c - first]); + uint8_t w = pgm_read_byte(&glyph->width), + h = pgm_read_byte(&glyph->height); + if((w > 0) && (h > 0)) { // Is there an associated bitmap? + int16_t xo = (int8_t)pgm_read_byte(&glyph->xOffset); // sic + if(wrap && ((cursor_x + textsize * (xo + w)) > _width)) { + cursor_x = 0; + cursor_y += (int16_t)textsize * + (uint8_t)pgm_read_byte(&gfxFont->yAdvance); + } + drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize); + } + cursor_x += (uint8_t)pgm_read_byte(&glyph->xAdvance) * (int16_t)textsize; + } + } + + } + return 1; +} + +/**************************************************************************/ +/*! + @brief Set text cursor location + @param x X coordinate in pixels + @param y Y coordinate in pixels +*/ +/**************************************************************************/ +void Adafruit_GFX::setCursor(int16_t x, int16_t y) { + cursor_x = x; + cursor_y = y; +} + +/**************************************************************************/ +/*! + @brief Get text cursor X location + @returns X coordinate in pixels +*/ +/**************************************************************************/ +int16_t Adafruit_GFX::getCursorX(void) const { + return cursor_x; +} + +/**************************************************************************/ +/*! + @brief Get text cursor Y location + @returns Y coordinate in pixels +*/ +/**************************************************************************/ +int16_t Adafruit_GFX::getCursorY(void) const { + return cursor_y; +} + +/**************************************************************************/ +/*! + @brief Set text 'magnification' size. Each increase in s makes 1 pixel that much bigger. + @param s Desired text size. 1 is default 6x8, 2 is 12x16, 3 is 18x24, etc +*/ +/**************************************************************************/ +void Adafruit_GFX::setTextSize(uint8_t s) { + textsize = (s > 0) ? s : 1; +} + +/**************************************************************************/ +/*! + @brief Set text font color with transparant background + @param c 16-bit 5-6-5 Color to draw text with +*/ +/**************************************************************************/ +void Adafruit_GFX::setTextColor(uint16_t c) { + // For 'transparent' background, we'll set the bg + // to the same as fg instead of using a flag + textcolor = textbgcolor = c; +} + +/**************************************************************************/ +/*! + @brief Set text font color with custom background color + @param c 16-bit 5-6-5 Color to draw text with + @param b 16-bit 5-6-5 Color to draw background/fill with +*/ +/**************************************************************************/ +void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) { + textcolor = c; + textbgcolor = b; +} + +/**************************************************************************/ +/*! + @brief Whether text that is too long should 'wrap' around to the next line. + @param w Set true for wrapping, false for clipping +*/ +/**************************************************************************/ +void Adafruit_GFX::setTextWrap(boolean w) { + wrap = w; +} + +/**************************************************************************/ +/*! + @brief Get rotation setting for display + @returns 0 thru 3 corresponding to 4 cardinal rotations +*/ +/**************************************************************************/ +uint8_t Adafruit_GFX::getRotation(void) const { + return rotation; +} + +/**************************************************************************/ +/*! + @brief Set rotation setting for display + @param x 0 thru 3 corresponding to 4 cardinal rotations +*/ +/**************************************************************************/ +void Adafruit_GFX::setRotation(uint8_t x) { + rotation = (x & 3); + switch(rotation) { + case 0: + case 2: + _width = WIDTH; + _height = HEIGHT; + break; + case 1: + case 3: + _width = HEIGHT; + _height = WIDTH; + break; + } +} + +/**************************************************************************/ +/*! + @brief Enable (or disable) Code Page 437-compatible charset. + There was an error in glcdfont.c for the longest time -- one character + (#176, the 'light shade' block) was missing -- this threw off the index + of every character that followed it. But a TON of code has been written + with the erroneous character indices. By default, the library uses the + original 'wrong' behavior and old sketches will still work. Pass 'true' + to this function to use correct CP437 character values in your code. + @param x Whether to enable (True) or not (False) +*/ +/**************************************************************************/ +void Adafruit_GFX::cp437(boolean x) { + _cp437 = x; +} + +/**************************************************************************/ +/*! + @brief Set the font to display when print()ing, either custom or default + @param f The GFXfont object, if NULL use built in 6x8 font +*/ +/**************************************************************************/ +void Adafruit_GFX::setFont(const GFXfont *f) { + if(f) { // Font struct pointer passed in? + if(!gfxFont) { // And no current font struct? + // Switching from classic to new font behavior. + // Move cursor pos down 6 pixels so it's on baseline. + cursor_y += 6; + } + } else if(gfxFont) { // NULL passed. Current font struct defined? + // Switching from new to classic font behavior. + // Move cursor pos up 6 pixels so it's at top-left of char. + cursor_y -= 6; + } + gfxFont = (GFXfont *)f; +} + + +/**************************************************************************/ +/*! + @brief Helper to determine size of a character with current font/size. + Broke this out as it's used by both the PROGMEM- and RAM-resident getTextBounds() functions. + @param c The ascii character in question + @param x Pointer to x location of character + @param y Pointer to y location of character + @param minx Minimum clipping value for X + @param miny Minimum clipping value for Y + @param maxx Maximum clipping value for X + @param maxy Maximum clipping value for Y +*/ +/**************************************************************************/ +void Adafruit_GFX::charBounds(char c, int16_t *x, int16_t *y, + int16_t *minx, int16_t *miny, int16_t *maxx, int16_t *maxy) { + + if(gfxFont) { + + if(c == '\n') { // Newline? + *x = 0; // Reset x to zero, advance y by one line + *y += textsize * (uint8_t)pgm_read_byte(&gfxFont->yAdvance); + } else if(c != '\r') { // Not a carriage return; is normal char + uint8_t first = pgm_read_byte(&gfxFont->first), + last = pgm_read_byte(&gfxFont->last); + if((c >= first) && (c <= last)) { // Char present in this font? + GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer( + &gfxFont->glyph))[c - first]); + uint8_t gw = pgm_read_byte(&glyph->width), + gh = pgm_read_byte(&glyph->height), + xa = pgm_read_byte(&glyph->xAdvance); + int8_t xo = pgm_read_byte(&glyph->xOffset), + yo = pgm_read_byte(&glyph->yOffset); + if(wrap && ((*x+(((int16_t)xo+gw)*textsize)) > _width)) { + *x = 0; // Reset x to zero, advance y by one line + *y += textsize * (uint8_t)pgm_read_byte(&gfxFont->yAdvance); + } + int16_t ts = (int16_t)textsize, + x1 = *x + xo * ts, + y1 = *y + yo * ts, + x2 = x1 + gw * ts - 1, + y2 = y1 + gh * ts - 1; + if(x1 < *minx) *minx = x1; + if(y1 < *miny) *miny = y1; + if(x2 > *maxx) *maxx = x2; + if(y2 > *maxy) *maxy = y2; + *x += xa * ts; + } + } + + } else { // Default font + + if(c == '\n') { // Newline? + *x = 0; // Reset x to zero, + *y += textsize * 8; // advance y one line + // min/max x/y unchaged -- that waits for next 'normal' character + } else if(c != '\r') { // Normal char; ignore carriage returns + if(wrap && ((*x + textsize * 6) > _width)) { // Off right? + *x = 0; // Reset x to zero, + *y += textsize * 8; // advance y one line + } + int x2 = *x + textsize * 6 - 1, // Lower-right pixel of char + y2 = *y + textsize * 8 - 1; + if(x2 > *maxx) *maxx = x2; // Track max x, y + if(y2 > *maxy) *maxy = y2; + if(*x < *minx) *minx = *x; // Track min x, y + if(*y < *miny) *miny = *y; + *x += textsize * 6; // Advance x one char + } + } +} + +/**************************************************************************/ +/*! + @brief Helper to determine size of a string with current font/size. Pass string and a cursor position, returns UL corner and W,H. + @param str The ascii string to measure + @param x The current cursor X + @param y The current cursor Y + @param x1 The boundary X coordinate, set by function + @param y1 The boundary Y coordinate, set by function + @param w The boundary width, set by function + @param h The boundary height, set by function +*/ +/**************************************************************************/ +void Adafruit_GFX::getTextBounds(const char *str, int16_t x, int16_t y, + int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) { + uint8_t c; // Current character + + *x1 = x; + *y1 = y; + *w = *h = 0; + + int16_t minx = _width, miny = _height, maxx = -1, maxy = -1; + + while((c = *str++)) + charBounds(c, &x, &y, &minx, &miny, &maxx, &maxy); + + if(maxx >= minx) { + *x1 = minx; + *w = maxx - minx + 1; + } + if(maxy >= miny) { + *y1 = miny; + *h = maxy - miny + 1; + } +} + +/**************************************************************************/ +/*! + @brief Helper to determine size of a string with current font/size. Pass string and a cursor position, returns UL corner and W,H. + @param str The ascii string to measure (as an arduino String() class) + @param x The current cursor X + @param y The current cursor Y + @param x1 The boundary X coordinate, set by function + @param y1 The boundary Y coordinate, set by function + @param w The boundary width, set by function + @param h The boundary height, set by function +*/ +/**************************************************************************/ +void Adafruit_GFX::getTextBounds(const String &str, int16_t x, int16_t y, + int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) { + if (str.length() != 0) { + getTextBounds(const_cast(str.c_str()), x, y, x1, y1, w, h); + } +} + + +/**************************************************************************/ +/*! + @brief Helper to determine size of a PROGMEM string with current font/size. Pass string and a cursor position, returns UL corner and W,H. + @param str The flash-memory ascii string to measure + @param x The current cursor X + @param y The current cursor Y + @param x1 The boundary X coordinate, set by function + @param y1 The boundary Y coordinate, set by function + @param w The boundary width, set by function + @param h The boundary height, set by function +*/ +/**************************************************************************/ +void Adafruit_GFX::getTextBounds(const __FlashStringHelper *str, + int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) { + uint8_t *s = (uint8_t *)str, c; + + *x1 = x; + *y1 = y; + *w = *h = 0; + + int16_t minx = _width, miny = _height, maxx = -1, maxy = -1; + + while((c = pgm_read_byte(s++))) + charBounds(c, &x, &y, &minx, &miny, &maxx, &maxy); + + if(maxx >= minx) { + *x1 = minx; + *w = maxx - minx + 1; + } + if(maxy >= miny) { + *y1 = miny; + *h = maxy - miny + 1; + } +} + +/**************************************************************************/ +/*! + @brief Get width of the display, accounting for the current rotation + @returns Width in pixels +*/ +/**************************************************************************/ +int16_t Adafruit_GFX::width(void) const { + return _width; +} + +/**************************************************************************/ +/*! + @brief Get height of the display, accounting for the current rotation + @returns Height in pixels +*/ +/**************************************************************************/ +int16_t Adafruit_GFX::height(void) const { + return _height; +} + +/**************************************************************************/ +/*! + @brief Invert the display (ideally using built-in hardware command) + @param i True if you want to invert, false to make 'normal' +*/ +/**************************************************************************/ +void Adafruit_GFX::invertDisplay(boolean __attribute__((unused)) i) { + // Do nothing, must be subclassed if supported by hardware +} + +/***************************************************************************/ + +/**************************************************************************/ +/*! + @brief Create a simple drawn button UI element +*/ +/**************************************************************************/ +Adafruit_GFX_Button::Adafruit_GFX_Button(void) { + _gfx = 0; +} + +/**************************************************************************/ +/*! + @brief Initialize button with our desired color/size/settings + @param gfx Pointer to our display so we can draw to it! + @param x The X coordinate of the center of the button + @param y The Y coordinate of the center of the button + @param w Width of the buttton + @param h Height of the buttton + @param outline Color of the outline (16-bit 5-6-5 standard) + @param fill Color of the button fill (16-bit 5-6-5 standard) + @param textcolor Color of the button label (16-bit 5-6-5 standard) + @param label Ascii string of the text inside the button + @param textsize The font magnification of the label text +*/ +/**************************************************************************/ +// Classic initButton() function: pass center & size +void Adafruit_GFX_Button::initButton( + Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t w, uint16_t h, + uint16_t outline, uint16_t fill, uint16_t textcolor, + char *label, uint8_t textsize) +{ + // Tweak arguments and pass to the newer initButtonUL() function... + initButtonUL(gfx, x - (w / 2), y - (h / 2), w, h, outline, fill, + textcolor, label, textsize); +} + +/**************************************************************************/ +/*! + @brief Initialize button with our desired color/size/settings, with upper-left coordinates + @param gfx Pointer to our display so we can draw to it! + @param x1 The X coordinate of the Upper-Left corner of the button + @param y1 The Y coordinate of the Upper-Left corner of the button + @param w Width of the buttton + @param h Height of the buttton + @param outline Color of the outline (16-bit 5-6-5 standard) + @param fill Color of the button fill (16-bit 5-6-5 standard) + @param textcolor Color of the button label (16-bit 5-6-5 standard) + @param label Ascii string of the text inside the button + @param textsize The font magnification of the label text +*/ +/**************************************************************************/ +void Adafruit_GFX_Button::initButtonUL( + Adafruit_GFX *gfx, int16_t x1, int16_t y1, uint16_t w, uint16_t h, + uint16_t outline, uint16_t fill, uint16_t textcolor, + char *label, uint8_t textsize) +{ + _x1 = x1; + _y1 = y1; + _w = w; + _h = h; + _outlinecolor = outline; + _fillcolor = fill; + _textcolor = textcolor; + _textsize = textsize; + _gfx = gfx; + strncpy(_label, label, 9); +} + +/**************************************************************************/ +/*! + @brief Draw the button on the screen + @param inverted Whether to draw with fill/text swapped to indicate 'pressed' +*/ +/**************************************************************************/ +void Adafruit_GFX_Button::drawButton(boolean inverted) { + uint16_t fill, outline, text; + + if(!inverted) { + fill = _fillcolor; + outline = _outlinecolor; + text = _textcolor; + } else { + fill = _textcolor; + outline = _outlinecolor; + text = _fillcolor; + } + + uint8_t r = min(_w, _h) / 4; // Corner radius + _gfx->fillRoundRect(_x1, _y1, _w, _h, r, fill); + _gfx->drawRoundRect(_x1, _y1, _w, _h, r, outline); + + _gfx->setCursor(_x1 + (_w/2) - (strlen(_label) * 3 * _textsize), + _y1 + (_h/2) - (4 * _textsize)); + _gfx->setTextColor(text); + _gfx->setTextSize(_textsize); + _gfx->print(_label); +} + +/**************************************************************************/ +/*! + @brief Helper to let us know if a coordinate is within the bounds of the button + @param x The X coordinate to check + @param y The Y coordinate to check + @returns True if within button graphics outline +*/ +/**************************************************************************/ +boolean Adafruit_GFX_Button::contains(int16_t x, int16_t y) { + return ((x >= _x1) && (x < (int16_t) (_x1 + _w)) && + (y >= _y1) && (y < (int16_t) (_y1 + _h))); +} + +/**************************************************************************/ +/*! + @brief Sets the state of the button, should be done by some touch function + @param p True for pressed, false for not. +*/ +/**************************************************************************/ +void Adafruit_GFX_Button::press(boolean p) { + laststate = currstate; + currstate = p; +} + +/**************************************************************************/ +/*! + @brief Query whether the button is currently pressed + @returns True if pressed +*/ +/**************************************************************************/ +boolean Adafruit_GFX_Button::isPressed() { return currstate; } + +/**************************************************************************/ +/*! + @brief Query whether the button was pressed since we last checked state + @returns True if was not-pressed before, now is. +*/ +/**************************************************************************/ +boolean Adafruit_GFX_Button::justPressed() { return (currstate && !laststate); } + +/**************************************************************************/ +/*! + @brief Query whether the button was released since we last checked state + @returns True if was pressed before, now is not. +*/ +/**************************************************************************/ +boolean Adafruit_GFX_Button::justReleased() { return (!currstate && laststate); } + +// ------------------------------------------------------------------------- + +// GFXcanvas1, GFXcanvas8 and GFXcanvas16 (currently a WIP, don't get too +// comfy with the implementation) provide 1-, 8- and 16-bit offscreen +// canvases, the address of which can be passed to drawBitmap() or +// pushColors() (the latter appears only in a couple of GFX-subclassed TFT +// libraries at this time). This is here mostly to help with the recently- +// added proportionally-spaced fonts; adds a way to refresh a section of the +// screen without a massive flickering clear-and-redraw...but maybe you'll +// find other uses too. VERY RAM-intensive, since the buffer is in MCU +// memory and not the display driver...GXFcanvas1 might be minimally useful +// on an Uno-class board, but this and the others are much more likely to +// require at least a Mega or various recent ARM-type boards (recommended, +// as the text+bitmap draw can be pokey). GFXcanvas1 requires 1 bit per +// pixel (rounded up to nearest byte per scanline), GFXcanvas8 is 1 byte +// per pixel (no scanline pad), and GFXcanvas16 uses 2 bytes per pixel (no +// scanline pad). +// NOT EXTENSIVELY TESTED YET. MAY CONTAIN WORST BUGS KNOWN TO HUMANKIND. + +/**************************************************************************/ +/*! + @brief Instatiate a GFX 1-bit canvas context for graphics + @param w Display width, in pixels + @param h Display height, in pixels +*/ +/**************************************************************************/ +GFXcanvas1::GFXcanvas1(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) { + uint16_t bytes = ((w + 7) / 8) * h; + if((buffer = (uint8_t *)malloc(bytes))) { + memset(buffer, 0, bytes); + } +} + +/**************************************************************************/ +/*! + @brief Delete the canvas, free memory +*/ +/**************************************************************************/ +GFXcanvas1::~GFXcanvas1(void) { + if(buffer) free(buffer); +} + +/**************************************************************************/ +/*! + @brief Get a pointer to the internal buffer memory + @returns A pointer to the allocated buffer +*/ +/**************************************************************************/ +uint8_t* GFXcanvas1::getBuffer(void) { + return buffer; +} + +/**************************************************************************/ +/*! + @brief Draw a pixel to the canvas framebuffer + @param x x coordinate + @param y y coordinate + @param color 16-bit 5-6-5 Color to fill with +*/ +/**************************************************************************/ +void GFXcanvas1::drawPixel(int16_t x, int16_t y, uint16_t color) { +#ifdef __AVR__ + // Bitmask tables of 0x80>>X and ~(0x80>>X), because X>>Y is slow on AVR + static const uint8_t PROGMEM + GFXsetBit[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, + GFXclrBit[] = { 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE }; +#endif + + if(buffer) { + if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return; + + int16_t t; + switch(rotation) { + case 1: + t = x; + x = WIDTH - 1 - y; + y = t; + break; + case 2: + x = WIDTH - 1 - x; + y = HEIGHT - 1 - y; + break; + case 3: + t = x; + x = y; + y = HEIGHT - 1 - t; + break; + } + + uint8_t *ptr = &buffer[(x / 8) + y * ((WIDTH + 7) / 8)]; +#ifdef __AVR__ + if(color) *ptr |= pgm_read_byte(&GFXsetBit[x & 7]); + else *ptr &= pgm_read_byte(&GFXclrBit[x & 7]); +#else + if(color) *ptr |= 0x80 >> (x & 7); + else *ptr &= ~(0x80 >> (x & 7)); +#endif + } +} + +/**************************************************************************/ +/*! + @brief Fill the framebuffer completely with one color + @param color 16-bit 5-6-5 Color to fill with +*/ +/**************************************************************************/ +void GFXcanvas1::fillScreen(uint16_t color) { + if(buffer) { + uint16_t bytes = ((WIDTH + 7) / 8) * HEIGHT; + memset(buffer, color ? 0xFF : 0x00, bytes); + } +} + +/**************************************************************************/ +/*! + @brief Instatiate a GFX 8-bit canvas context for graphics + @param w Display width, in pixels + @param h Display height, in pixels +*/ +/**************************************************************************/ +GFXcanvas8::GFXcanvas8(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) { + uint32_t bytes = w * h; + if((buffer = (uint8_t *)malloc(bytes))) { + memset(buffer, 0, bytes); + } +} + +/**************************************************************************/ +/*! + @brief Delete the canvas, free memory +*/ +/**************************************************************************/ +GFXcanvas8::~GFXcanvas8(void) { + if(buffer) free(buffer); +} + + +/**************************************************************************/ +/*! + @brief Get a pointer to the internal buffer memory + @returns A pointer to the allocated buffer +*/ +/**************************************************************************/ +uint8_t* GFXcanvas8::getBuffer(void) { + return buffer; +} + +/**************************************************************************/ +/*! + @brief Draw a pixel to the canvas framebuffer + @param x x coordinate + @param y y coordinate + @param color 16-bit 5-6-5 Color to fill with +*/ +/**************************************************************************/ +void GFXcanvas8::drawPixel(int16_t x, int16_t y, uint16_t color) { + if(buffer) { + if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return; + + int16_t t; + switch(rotation) { + case 1: + t = x; + x = WIDTH - 1 - y; + y = t; + break; + case 2: + x = WIDTH - 1 - x; + y = HEIGHT - 1 - y; + break; + case 3: + t = x; + x = y; + y = HEIGHT - 1 - t; + break; + } + + buffer[x + y * WIDTH] = color; + } +} + +/**************************************************************************/ +/*! + @brief Fill the framebuffer completely with one color + @param color 16-bit 5-6-5 Color to fill with +*/ +/**************************************************************************/ +void GFXcanvas8::fillScreen(uint16_t color) { + if(buffer) { + memset(buffer, color, WIDTH * HEIGHT); + } +} + +void GFXcanvas8::writeFastHLine(int16_t x, int16_t y, + int16_t w, uint16_t color) { + + if((x >= _width) || (y < 0) || (y >= _height)) return; + int16_t x2 = x + w - 1; + if(x2 < 0) return; + + // Clip left/right + if(x < 0) { + x = 0; + w = x2 + 1; + } + if(x2 >= _width) w = _width - x; + + int16_t t; + switch(rotation) { + case 1: + t = x; + x = WIDTH - 1 - y; + y = t; + break; + case 2: + x = WIDTH - 1 - x; + y = HEIGHT - 1 - y; + break; + case 3: + t = x; + x = y; + y = HEIGHT - 1 - t; + break; + } + + memset(buffer + y * WIDTH + x, color, w); +} + +/**************************************************************************/ +/*! + @brief Instatiate a GFX 16-bit canvas context for graphics + @param w Display width, in pixels + @param h Display height, in pixels +*/ +/**************************************************************************/ +GFXcanvas16::GFXcanvas16(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) { + uint32_t bytes = w * h * 2; + if((buffer = (uint16_t *)malloc(bytes))) { + memset(buffer, 0, bytes); + } +} + +/**************************************************************************/ +/*! + @brief Delete the canvas, free memory +*/ +/**************************************************************************/ +GFXcanvas16::~GFXcanvas16(void) { + if(buffer) free(buffer); +} + +/**************************************************************************/ +/*! + @brief Get a pointer to the internal buffer memory + @returns A pointer to the allocated buffer +*/ +/**************************************************************************/ +uint16_t* GFXcanvas16::getBuffer(void) { + return buffer; +} + +/**************************************************************************/ +/*! + @brief Draw a pixel to the canvas framebuffer + @param x x coordinate + @param y y coordinate + @param color 16-bit 5-6-5 Color to fill with +*/ +/**************************************************************************/ +void GFXcanvas16::drawPixel(int16_t x, int16_t y, uint16_t color) { + if(buffer) { + if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return; + + int16_t t; + switch(rotation) { + case 1: + t = x; + x = WIDTH - 1 - y; + y = t; + break; + case 2: + x = WIDTH - 1 - x; + y = HEIGHT - 1 - y; + break; + case 3: + t = x; + x = y; + y = HEIGHT - 1 - t; + break; + } + + buffer[x + y * WIDTH] = color; + } +} + +/**************************************************************************/ +/*! + @brief Fill the framebuffer completely with one color + @param color 16-bit 5-6-5 Color to fill with +*/ +/**************************************************************************/ +void GFXcanvas16::fillScreen(uint16_t color) { + if(buffer) { + uint8_t hi = color >> 8, lo = color & 0xFF; + if(hi == lo) { + memset(buffer, lo, WIDTH * HEIGHT * 2); + } else { + uint32_t i, pixels = WIDTH * HEIGHT; + for(i=0; i https://www.adafruit.com/products/1982 + + These sensors use I2C to communicate, 2+ pins are required to + interface + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + * + * @section author Author + * + * Written by Limor Fried/Ladyada for Adafruit Industries. + * + * @section license License + * + * BSD license, all text here must be included in any redistribution. + * + */ + +#include "Adafruit_MPR121.h" + +/** + ***************************************************************************************** + * @brief Default constructor + ****************************************************************************************/ +Adafruit_MPR121::Adafruit_MPR121() { +} + +/** + ***************************************************************************************** + * @brief Begin an MPR121 object on a given I2C bus. This function resets the + * device and writes the default settings. + * + * @param i2caddr the i2c address the device can be found on. Defaults to 0x5A. + * @returns true on success, false otherwise + ****************************************************************************************/ +boolean Adafruit_MPR121::begin(uint8_t i2caddr) { + _i2caddr = i2caddr; + + +// writeRegister(MPR121_ECR, 0x0); + +// uint8_t c = readRegister8(MPR121_CONFIG2); + +// if (c != 0x24) return false; + + +// setThreshholds(12, 6); +// writeRegister(MPR121_MHDR, 0x01); +// writeRegister(MPR121_NHDR, 0x01); +// writeRegister(MPR121_NCLR, 0x0E); +// writeRegister(MPR121_FDLR, 0x00); + +// writeRegister(MPR121_MHDF, 0x01); +// writeRegister(MPR121_NHDF, 0x05); +// writeRegister(MPR121_NCLF, 0x01); +// writeRegister(MPR121_FDLF, 0x00); + +// writeRegister(MPR121_NHDT, 0x00); +// writeRegister(MPR121_NCLT, 0x00); +// writeRegister(MPR121_FDLT, 0x00); + +// writeRegister(MPR121_DEBOUNCE, 0); +// writeRegister(MPR121_CONFIG1, 0x10); // default, 16uA charge current +// writeRegister(MPR121_CONFIG2, 0x20); // 0.5uS encoding, 1ms period + +// // writeRegister(MPR121_AUTOCONFIG0, 0x8F); + +// // writeRegister(MPR121_UPLIMIT, 150); +// // writeRegister(MPR121_TARGETLIMIT, 100); // should be ~400 (100 shifted) +// // writeRegister(MPR121_LOWLIMIT, 50); +// // enable all electrodes +// writeRegister(MPR121_ECR, 0x8F); // start with first 5 bits of baseline tracking + + return true; +} + +/** + ***************************************************************************************** + * @brief DEPRECATED. Use Adafruit_MPR121::setThresholds(uint8_t touch, uint8_t release) instead. + * + * @param touch see Adafruit_MPR121::setThresholds(uint8_t touch, uint8_t release) + * @param release see Adafruit_MPR121::setThresholds(uint8_t touch, uint8_t release) + ****************************************************************************************/ +void Adafruit_MPR121::setThreshholds(uint8_t __attribute__((unused)) touch, uint8_t __attribute__((unused)) release) { + // setThresholds(touch, release); +} + +/** + ***************************************************************************************** + * @brief Set the touch and release thresholds for all 13 channels on the device to the + * passed values. The threshold is defined as a deviation value from the baseline value, + * so it remains constant even baseline value changes. Typically the touch + * threshold is a little bigger than the release threshold to touch debounce and hysteresis. + * + * For typical touch application, the value can be in range 0x05~0x30 for example. The setting + * of the threshold is depended on the actual application. For the operation details and how to set the threshold refer to + * application note AN3892 and MPR121 design guidelines. + * + * @param touch the touch threshold value from 0 to 255. + * @param release the release threshold from 0 to 255. + ****************************************************************************************/ +void Adafruit_MPR121::setThresholds(uint8_t __attribute__((unused)) touch, __attribute__((unused)) uint8_t release) { + // for (uint8_t i=0; i<12; i++) { + // writeRegister(MPR121_TOUCHTH_0 + 2*i, touch); + // writeRegister(MPR121_RELEASETH_0 + 2*i, release); + // } +} + +/** + ***************************************************************************************** + * @brief Read the filtered data from channel t. The ADC raw data outputs run through 3 + * levels of digital filtering to filter out the high frequency and low frequency noise + * encountered. For detailed information on this filtering see page 6 of the device datasheet. + * + * @param t the channel to read + * @returns the filtered reading as a 10 bit unsigned value + ****************************************************************************************/ +uint16_t Adafruit_MPR121::filteredData(uint8_t t) { + if (t > 12) return 0; + return readRegister16(MPR121_FILTDATA_0L + t*2); +} + +/** + ***************************************************************************************** + * @brief Read the baseline value for the channel. The 3rd level filtered result is internally 10bit + * but only high 8 bits are readable from registers 0x1E~0x2A as the baseline value output for each channel. + * + * @param t the channel to read. + * @returns the baseline data that was read + ****************************************************************************************/ +uint16_t Adafruit_MPR121::baselineData(uint8_t t) { + if (t > 12) return 0; + uint16_t bl = readRegister8(MPR121_BASELINE_0 + t); + return (bl << 2); +} + +/** + ***************************************************************************************** + * @brief Read the touch status of all 13 channels as bit values in a 12 bit integer. + * + * @returns a 12 bit integer with each bit corresponding to the touch status of a sensor. + * For example, if bit 0 is set then channel 0 of the device is currently deemed to be touched. + ****************************************************************************************/ +uint16_t Adafruit_MPR121::touched(void) { + uint16_t t = readRegister16(MPR121_TOUCHSTATUS_L); + return t & 0x0FFF; +} + +/*********************************************************************/ + +/** + ***************************************************************************************** + * @brief Read the contents of an 8 bit device register. + * + * @param reg the register address to read from + * @returns the 8 bit value that was read. + ****************************************************************************************/ +uint8_t Adafruit_MPR121::readRegister8(uint8_t reg) { + + return this->_registers[reg]; + // Wire.beginTransmission(_i2caddr); + // Wire.write(reg); + // Wire.endTransmission(false); + // Wire.requestFrom(_i2caddr, 1); + // if (Wire.available() < 1) + // return 0; + // return (Wire.read()); +} + +/** + ***************************************************************************************** + * @brief Read the contents of a 16 bit device register. + * + * @param reg the register address to read from + * @returns the 16 bit value that was read. + ****************************************************************************************/ +uint16_t Adafruit_MPR121::readRegister16(uint8_t reg) { + return _registers[reg] | (_registers[reg+1] << 8); + // Wire.beginTransmission(_i2caddr); + // Wire.write(reg); + // Wire.endTransmission(false); + // Wire.requestFrom(_i2caddr, 2); + // if (Wire.available() < 2) + // return 0; + // uint16_t v = Wire.read(); + // v |= ((uint16_t) Wire.read()) << 8; + // return v; +} + +/**************************************************************************/ +/*! + @brief Writes 8-bits to the specified destination register + + @param reg the register address to write to + @param value the value to write +*/ +/**************************************************************************/ +void Adafruit_MPR121::writeRegister(uint8_t reg, uint8_t value) { + + _registers[reg] = value; + // Wire.beginTransmission(_i2caddr); + // Wire.write((uint8_t)reg); + // Wire.write((uint8_t)(value)); + // Wire.endTransmission(); +} diff --git a/simulation/src/Adafruit_SSD1306_sim.cpp b/simulation/src/Adafruit_SSD1306_sim.cpp new file mode 100644 index 0000000..02eb17c --- /dev/null +++ b/simulation/src/Adafruit_SSD1306_sim.cpp @@ -0,0 +1,730 @@ +/*! + * @file Adafruit_SSD1306.cpp + * + * @mainpage Arduino library for monochrome OLEDs based on SSD1306 drivers. + * + * @section intro_sec Introduction + * + * This is documentation for Adafruit's SSD1306 library for monochrome + * OLED displays: http://www.adafruit.com/category/63_98 + * + * These displays use I2C or SPI to communicate. I2C requires 2 pins + * (SCL+SDA) and optionally a RESET pin. SPI requires 4 pins (MOSI, SCK, + * select, data/command) and optionally a reset pin. Hardware SPI or + * 'bitbang' software SPI are both supported. + * + * Adafruit invests time and resources providing this open source code, + * please support Adafruit and open-source hardware by purchasing + * products from Adafruit! + * + * @section dependencies Dependencies + * + * This library depends on + * Adafruit_GFX being present on your system. Please make sure you have + * installed the latest version before using this library. + * + * @section author Author + * + * Written by Limor Fried/Ladyada for Adafruit Industries, with + * contributions from the open source community. + * + * @section license License + * + * BSD license, all text above, and the splash screen included below, + * must be included in any redistribution. + * + */ + +// #ifdef __AVR__ +// #include +// #elif defined(ESP8266) || defined(ESP32) +// #include +// #else + #define pgm_read_byte(addr) \ + (*(const unsigned char *)(addr)) ///< PROGMEM workaround for non-AVR +// #endif + +// #if !defined(__ARM_ARCH) && !defined(ENERGIA) && !defined(ESP8266) && !defined(ESP32) && !defined(__arc__) +// #include +// #endif + +#include +#include "Adafruit_SSD1306.h" + +// SOME DEFINES AND STATIC VARIABLES USED INTERNALLY ----------------------- + +// #if defined(BUFFER_LENGTH) +// #define WIRE_MAX BUFFER_LENGTH ///< AVR or similar Wire lib +// #elif defined(SERIAL_BUFFER_SIZE) +// #define WIRE_MAX (SERIAL_BUFFER_SIZE-1) ///< Newer Wire uses RingBuffer +// #else +// #define WIRE_MAX 32 ///< Use common Arduino core default +// #endif + +#define ssd1306_swap(a, b) \ + (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b))) ///< No-temp-var swap operation + +// #if ARDUINO >= 100 +// #define WIRE_WRITE wire->write ///< Wire write function in recent Arduino lib +// #else +// #define WIRE_WRITE wire->send ///< Wire write function in older Arduino lib +// #endif + +// #ifdef HAVE_PORTREG +// #define SSD1306_SELECT *csPort &= ~csPinMask; ///< Device select +// #define SSD1306_DESELECT *csPort |= csPinMask; ///< Device deselect +// #define SSD1306_MODE_COMMAND *dcPort &= ~dcPinMask; ///< Command mode +// #define SSD1306_MODE_DATA *dcPort |= dcPinMask; ///< Data mode +// #else +// #define SSD1306_SELECT digitalWrite(csPin, LOW); ///< Device select +// #define SSD1306_DESELECT digitalWrite(csPin, HIGH); ///< Device deselect +// #define SSD1306_MODE_COMMAND digitalWrite(dcPin, LOW); ///< Command mode +// #define SSD1306_MODE_DATA digitalWrite(dcPin, HIGH); ///< Data mode +// #endif + +// #if (ARDUINO >= 157) && !defined(ARDUINO_STM32_FEATHER) +// #define SETWIRECLOCK wire->setClock(wireClk) ///< Set before I2C transfer +// #define RESWIRECLOCK wire->setClock(restoreClk) ///< Restore after I2C xfer +// #else // setClock() is not present in older Arduino Wire lib (or WICED) +// #define SETWIRECLOCK ///< Dummy stand-in define +// #define RESWIRECLOCK ///< keeps compiler happy +// #endif + +// #if defined(SPI_HAS_TRANSACTION) +// #define SPI_TRANSACTION_START spi->beginTransaction(spiSettings) ///< Pre-SPI +// #define SPI_TRANSACTION_END spi->endTransaction() ///< Post-SPI +// #else // SPI transactions likewise not present in older Arduino SPI lib +// #define SPI_TRANSACTION_START ///< Dummy stand-in define +// #define SPI_TRANSACTION_END ///< keeps compiler happy +// #endif + +// The definition of 'transaction' is broadened a bit in the context of +// this library -- referring not just to SPI transactions (if supported +// in the version of the SPI library being used), but also chip select +// (if SPI is being used, whether hardware or soft), and also to the +// beginning and end of I2C transfers (the Wire clock may be sped up before +// issuing data to the display, then restored to the default rate afterward +// so other I2C device types still work). All of these are encapsulated +// in the TRANSACTION_* macros. + +// Check first if Wire, then hardware SPI, then soft SPI: +// #define TRANSACTION_START \ +// if(wire) { \ +// SETWIRECLOCK; \ +// } else { \ +// if(spi) { \ +// SPI_TRANSACTION_START; \ +// } \ +// SSD1306_SELECT; \ +// } ///< Wire, SPI or bitbang transfer setup +// #define TRANSACTION_END \ +// if(wire) { \ +// RESWIRECLOCK; \ +// } else { \ +// SSD1306_DESELECT; \ +// if(spi) { \ +// SPI_TRANSACTION_END; \ +// } \ +// } ///< Wire, SPI or bitbang transfer end + +// CONSTRUCTORS, DESTRUCTOR ------------------------------------------------ + +/*! + @brief Constructor for I2C-interfaced SSD1306 displays. + @param w + Display width in pixels + @param h + Display height in pixels + @param twi + Pointer to an existing TwoWire instance (e.g. &Wire, the + microcontroller's primary I2C bus). + @param rst_pin + Reset pin (using Arduino pin numbering), or -1 if not used + (some displays might be wired to share the microcontroller's + reset pin). + @param clkDuring + Speed (in Hz) for Wire transmissions in SSD1306 library calls. + Defaults to 400000 (400 KHz), a known 'safe' value for most + microcontrollers, and meets the SSD1306 datasheet spec. + Some systems can operate I2C faster (800 KHz for ESP32, 1 MHz + for many other 32-bit MCUs), and some (perhaps not all) + SSD1306's can work with this -- so it's optionally be specified + here and is not a default behavior. (Ignored if using pre-1.5.7 + Arduino software, which operates I2C at a fixed 100 KHz.) + @param clkAfter + Speed (in Hz) for Wire transmissions following SSD1306 library + calls. Defaults to 100000 (100 KHz), the default Arduino Wire + speed. This is done rather than leaving it at the 'during' speed + because other devices on the I2C bus might not be compatible + with the faster rate. (Ignored if using pre-1.5.7 Arduino + software, which operates I2C at a fixed 100 KHz.) + @return Adafruit_SSD1306 object. + @note Call the object's begin() function before use -- buffer + allocation is performed there! +*/ +Adafruit_SSD1306::Adafruit_SSD1306(uint8_t w, uint8_t h, TwoWire *twi, + int8_t rst_pin, uint32_t clkDuring, uint32_t clkAfter) : + Adafruit_GFX(w, h), spi(NULL), wire(twi), buffer(NULL), + mosiPin(-1), clkPin(-1), dcPin(-1), csPin(-1), rstPin(rst_pin), + wireClk(clkDuring), restoreClk(clkAfter) +{ +} + +/*! + @brief Constructor for SPI SSD1306 displays, using software (bitbang) + SPI. + @param w + Display width in pixels + @param h + Display height in pixels + @param mosi_pin + MOSI (master out, slave in) pin (using Arduino pin numbering). + This transfers serial data from microcontroller to display. + @param sclk_pin + SCLK (serial clock) pin (using Arduino pin numbering). + This clocks each bit from MOSI. + @param dc_pin + Data/command pin (using Arduino pin numbering), selects whether + display is receiving commands (low) or data (high). + @param rst_pin + Reset pin (using Arduino pin numbering), or -1 if not used + (some displays might be wired to share the microcontroller's + reset pin). + @param cs_pin + Chip-select pin (using Arduino pin numbering) for sharing the + bus with other devices. Active low. + @return Adafruit_SSD1306 object. + @note Call the object's begin() function before use -- buffer + allocation is performed there! +*/ +Adafruit_SSD1306::Adafruit_SSD1306(uint8_t w, uint8_t h, + int8_t mosi_pin, int8_t sclk_pin, int8_t dc_pin, int8_t rst_pin, + int8_t cs_pin) : Adafruit_GFX(w, h), spi(NULL), wire(NULL), buffer(NULL), + mosiPin(mosi_pin), clkPin(sclk_pin), dcPin(dc_pin), csPin(cs_pin), + rstPin(rst_pin) { +} + +/*! + @brief Constructor for SPI SSD1306 displays, using native hardware SPI. + @param w + Display width in pixels + @param h + Display height in pixels + @param spi + Pointer to an existing SPIClass instance (e.g. &SPI, the + microcontroller's primary SPI bus). + @param dc_pin + Data/command pin (using Arduino pin numbering), selects whether + display is receiving commands (low) or data (high). + @param rst_pin + Reset pin (using Arduino pin numbering), or -1 if not used + (some displays might be wired to share the microcontroller's + reset pin). + @param cs_pin + Chip-select pin (using Arduino pin numbering) for sharing the + bus with other devices. Active low. + @param bitrate + SPI clock rate for transfers to this display. Default if + unspecified is 8000000UL (8 MHz). + @return Adafruit_SSD1306 object. + @note Call the object's begin() function before use -- buffer + allocation is performed there! +*/ +Adafruit_SSD1306::Adafruit_SSD1306(uint8_t w, uint8_t h, SPIClass *spi, + int8_t dc_pin, int8_t rst_pin, int8_t cs_pin, uint32_t __attribute__((unused)) bitrate) : + Adafruit_GFX(w, h), spi(spi), wire(NULL), buffer(NULL), + mosiPin(-1), clkPin(-1), dcPin(dc_pin), csPin(cs_pin), rstPin(rst_pin) { +#ifdef SPI_HAS_TRANSACTION + spiSettings = SPISettings(bitrate, MSBFIRST, SPI_MODE0); +#endif +} + +/*! + @brief Destructor for Adafruit_SSD1306 object. +*/ +Adafruit_SSD1306::~Adafruit_SSD1306(void) { + if(buffer) { + free(buffer); + buffer = NULL; + } +} + +// LOW-LEVEL UTILS --------------------------------------------------------- + +// ALLOCATE & INIT DISPLAY ------------------------------------------------- + +/*! + @brief Allocate RAM for image buffer, initialize peripherals and pins. + @param vcs + VCC selection. Pass SSD1306_SWITCHCAPVCC to generate the display + voltage (step up) from the 3.3V source, or SSD1306_EXTERNALVCC + otherwise. Most situations with Adafruit SSD1306 breakouts will + want SSD1306_SWITCHCAPVCC. + @param addr + I2C address of corresponding SSD1306 display (or pass 0 to use + default of 0x3C for 128x32 display, 0x3D for all others). + SPI displays (hardware or software) do not use addresses, but + this argument is still required (pass 0 or any value really, + it will simply be ignored). Default if unspecified is 0. + @param reset + If true, and if the reset pin passed to the constructor is + valid, a hard reset will be performed before initializing the + display. If using multiple SSD1306 displays on the same bus, and + if they all share the same reset pin, you should only pass true + on the first display being initialized, false on all others, + else the already-initialized displays would be reset. Default if + unspecified is true. + @param periphBegin + If true, and if a hardware peripheral is being used (I2C or SPI, + but not software SPI), call that peripheral's begin() function, + else (false) it has already been done in one's sketch code. + Cases where false might be used include multiple displays or + other devices sharing a common bus, or situations on some + platforms where a nonstandard begin() function is available + (e.g. a TwoWire interface on non-default pins, as can be done + on the ESP8266 and perhaps others). + @return true on successful allocation/init, false otherwise. + Well-behaved code should check the return value before + proceeding. + @note MUST call this function before any drawing or updates! +*/ +boolean Adafruit_SSD1306::begin(uint8_t __attribute__((unused)) vcs, uint8_t __attribute__((unused)) addr, boolean __attribute__((unused)) reset, + boolean __attribute__((unused)) periphBegin) { + + if((!buffer) && !(buffer = (uint8_t *)malloc(WIDTH * ((HEIGHT + 7) / 8)))) + return false; + + enabled_ = true; + + clearDisplay(); +// if(HEIGHT > 32) { +// drawBitmap((WIDTH - splash1_width) / 2, (HEIGHT - splash1_height) / 2, +// splash1_data, splash1_width, splash1_height, 1); +// } else { +// drawBitmap((WIDTH - splash2_width) / 2, (HEIGHT - splash2_height) / 2, +// splash2_data, splash2_width, splash2_height, 1); +// } + + return true; // Success +} + +// DRAWING FUNCTIONS ------------------------------------------------------- + +/*! + @brief Set/clear/invert a single pixel. This is also invoked by the + Adafruit_GFX library in generating many higher-level graphics + primitives. + @param x + Column of display -- 0 at left to (screen width - 1) at right. + @param y + Row of display -- 0 at top to (screen height -1) at bottom. + @param color + Pixel color, one of: BLACK, WHITE or INVERT. + @return None (void). + @note Changes buffer contents only, no immediate effect on display. + Follow up with a call to display(), or with other graphics + commands as needed by one's own application. +*/ +void Adafruit_SSD1306::drawPixel(int16_t x, int16_t y, uint16_t color) { + if((x >= 0) && (x < width()) && (y >= 0) && (y < height())) { + // Pixel is in-bounds. Rotate coordinates if needed. + switch(getRotation()) { + case 1: + ssd1306_swap(x, y); + x = WIDTH - x - 1; + break; + case 2: + x = WIDTH - x - 1; + y = HEIGHT - y - 1; + break; + case 3: + ssd1306_swap(x, y); + y = HEIGHT - y - 1; + break; + } + switch(color) { + case WHITE: buffer[x + (y/8)*WIDTH] |= (1 << (y&7)); break; + case BLACK: buffer[x + (y/8)*WIDTH] &= ~(1 << (y&7)); break; + case INVERSE: buffer[x + (y/8)*WIDTH] ^= (1 << (y&7)); break; + } + } +} + +/*! + @brief Clear contents of display buffer (set all pixels to off). + @return None (void). + @note Changes buffer contents only, no immediate effect on display. + Follow up with a call to display(), or with other graphics + commands as needed by one's own application. +*/ +void Adafruit_SSD1306::clearDisplay(void) { + memset(buffer, 0, WIDTH * ((HEIGHT + 7) / 8)); +} + +/*! + @brief Draw a horizontal line. This is also invoked by the Adafruit_GFX + library in generating many higher-level graphics primitives. + @param x + Leftmost column -- 0 at left to (screen width - 1) at right. + @param y + Row of display -- 0 at top to (screen height -1) at bottom. + @param w + Width of line, in pixels. + @param color + Line color, one of: BLACK, WHITE or INVERT. + @return None (void). + @note Changes buffer contents only, no immediate effect on display. + Follow up with a call to display(), or with other graphics + commands as needed by one's own application. +*/ +void Adafruit_SSD1306::drawFastHLine( + int16_t x, int16_t y, int16_t w, uint16_t color) { + boolean bSwap = false; + switch(rotation) { + case 1: + // 90 degree rotation, swap x & y for rotation, then invert x + bSwap = true; + ssd1306_swap(x, y); + x = WIDTH - x - 1; + break; + case 2: + // 180 degree rotation, invert x and y, then shift y around for height. + x = WIDTH - x - 1; + y = HEIGHT - y - 1; + x -= (w-1); + break; + case 3: + // 270 degree rotation, swap x & y for rotation, + // then invert y and adjust y for w (not to become h) + bSwap = true; + ssd1306_swap(x, y); + y = HEIGHT - y - 1; + y -= (w-1); + break; + } + + if(bSwap) drawFastVLineInternal(x, y, w, color); + else drawFastHLineInternal(x, y, w, color); +} + +void Adafruit_SSD1306::drawFastHLineInternal( + int16_t x, int16_t y, int16_t w, uint16_t color) { + + if((y >= 0) && (y < HEIGHT)) { // Y coord in bounds? + if(x < 0) { // Clip left + w += x; + x = 0; + } + if((x + w) > WIDTH) { // Clip right + w = (WIDTH - x); + } + if(w > 0) { // Proceed only if width is positive + uint8_t *pBuf = &buffer[(y / 8) * WIDTH + x], + mask = 1 << (y & 7); + switch(color) { + case WHITE: while(w--) { *pBuf++ |= mask; }; break; + case BLACK: mask = ~mask; while(w--) { *pBuf++ &= mask; }; break; + case INVERSE: while(w--) { *pBuf++ ^= mask; }; break; + } + } + } +} + +/*! + @brief Draw a vertical line. This is also invoked by the Adafruit_GFX + library in generating many higher-level graphics primitives. + @param x + Column of display -- 0 at left to (screen width -1) at right. + @param y + Topmost row -- 0 at top to (screen height - 1) at bottom. + @param h + Height of line, in pixels. + @param color + Line color, one of: BLACK, WHITE or INVERT. + @return None (void). + @note Changes buffer contents only, no immediate effect on display. + Follow up with a call to display(), or with other graphics + commands as needed by one's own application. +*/ +void Adafruit_SSD1306::drawFastVLine( + int16_t x, int16_t y, int16_t h, uint16_t color) { + boolean bSwap = false; + switch(rotation) { + case 1: + // 90 degree rotation, swap x & y for rotation, + // then invert x and adjust x for h (now to become w) + bSwap = true; + ssd1306_swap(x, y); + x = WIDTH - x - 1; + x -= (h-1); + break; + case 2: + // 180 degree rotation, invert x and y, then shift y around for height. + x = WIDTH - x - 1; + y = HEIGHT - y - 1; + y -= (h-1); + break; + case 3: + // 270 degree rotation, swap x & y for rotation, then invert y + bSwap = true; + ssd1306_swap(x, y); + y = HEIGHT - y - 1; + break; + } + + if(bSwap) drawFastHLineInternal(x, y, h, color); + else drawFastVLineInternal(x, y, h, color); +} + +void Adafruit_SSD1306::drawFastVLineInternal( + int16_t x, int16_t __y, int16_t __h, uint16_t color) { + + if((x >= 0) && (x < WIDTH)) { // X coord in bounds? + if(__y < 0) { // Clip top + __h += __y; + __y = 0; + } + if((__y + __h) > HEIGHT) { // Clip bottom + __h = (HEIGHT - __y); + } + if(__h > 0) { // Proceed only if height is now positive + // this display doesn't need ints for coordinates, + // use local byte registers for faster juggling + uint8_t y = __y, h = __h; + uint8_t *pBuf = &buffer[(y / 8) * WIDTH + x]; + + // do the first partial byte, if necessary - this requires some masking + uint8_t mod = (y & 7); + if(mod) { + // mask off the high n bits we want to set + mod = 8 - mod; + // note - lookup table results in a nearly 10% performance + // improvement in fill* functions + // uint8_t mask = ~(0xFF >> mod); + static const uint8_t PROGMEM premask[8] = + { 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE }; + uint8_t mask = pgm_read_byte(&premask[mod]); + // adjust the mask if we're not going to reach the end of this byte + if(h < mod) mask &= (0XFF >> (mod - h)); + + switch(color) { + case WHITE: *pBuf |= mask; break; + case BLACK: *pBuf &= ~mask; break; + case INVERSE: *pBuf ^= mask; break; + } + pBuf += WIDTH; + } + + if(h >= mod) { // More to go? + h -= mod; + // Write solid bytes while we can - effectively 8 rows at a time + if(h >= 8) { + if(color == INVERSE) { + // separate copy of the code so we don't impact performance of + // black/white write version with an extra comparison per loop + do { + *pBuf ^= 0xFF; // Invert byte + pBuf += WIDTH; // Advance pointer 8 rows + h -= 8; // Subtract 8 rows from height + } while(h >= 8); + } else { + // store a local value to work with + uint8_t val = (color != BLACK) ? 255 : 0; + do { + *pBuf = val; // Set byte + pBuf += WIDTH; // Advance pointer 8 rows + h -= 8; // Subtract 8 rows from height + } while(h >= 8); + } + } + + if(h) { // Do the final partial byte, if necessary + mod = h & 7; + // this time we want to mask the low bits of the byte, + // vs the high bits we did above + // uint8_t mask = (1 << mod) - 1; + // note - lookup table results in a nearly 10% performance + // improvement in fill* functions + static const uint8_t PROGMEM postmask[8] = + { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F }; + uint8_t mask = pgm_read_byte(&postmask[mod]); + switch(color) { + case WHITE: *pBuf |= mask; break; + case BLACK: *pBuf &= ~mask; break; + case INVERSE: *pBuf ^= mask; break; + } + } + } + } // endif positive height + } // endif x in bounds +} + +/*! + @brief Return color of a single pixel in display buffer. + @param x + Column of display -- 0 at left to (screen width - 1) at right. + @param y + Row of display -- 0 at top to (screen height -1) at bottom. + @return true if pixel is set (usually WHITE, unless display invert mode + is enabled), false if clear (BLACK). + @note Reads from buffer contents; may not reflect current contents of + screen if display() has not been called. +*/ +boolean Adafruit_SSD1306::getPixel(int16_t x, int16_t y) { + if((x >= 0) && (x < width()) && (y >= 0) && (y < height())) { + // Pixel is in-bounds. Rotate coordinates if needed. + switch(getRotation()) { + case 1: + ssd1306_swap(x, y); + x = WIDTH - x - 1; + break; + case 2: + x = WIDTH - x - 1; + y = HEIGHT - y - 1; + break; + case 3: + ssd1306_swap(x, y); + y = HEIGHT - y - 1; + break; + } + return (buffer[x + (y / 8) * WIDTH] & (1 << (y & 7))); + } + return false; // Pixel out of bounds +} + +/*! + @brief Get base address of display buffer for direct reading or writing. + @return Pointer to an unsigned 8-bit array, column-major, columns padded + to full byte boundary if needed. +*/ +uint8_t *Adafruit_SSD1306::getBuffer(void) { + return buffer; +} + +// REFRESH DISPLAY --------------------------------------------------------- + +/*! + @brief Push data currently in RAM to SSD1306 display. + @return None (void). + @note Drawing operations are not visible until this function is + called. Call after each graphics command, or after a whole set + of graphics commands, as best needed by one's own application. +*/ +void Adafruit_SSD1306::display(void) { + + // TODO: Update SDL surface with content of `buffer` + +} + +// SCROLLING FUNCTIONS ----------------------------------------------------- + +/*! + @brief Activate a right-handed scroll for all or part of the display. + @param start + First row. + @param stop + Last row. + @return None (void). +*/ +// To scroll the whole display, run: display.startscrollright(0x00, 0x0F) +void Adafruit_SSD1306::startscrollright(uint8_t __attribute__((unused)) start, uint8_t __attribute__((unused)) stop) { + + // not used for now + +} + +/*! + @brief Activate a left-handed scroll for all or part of the display. + @param start + First row. + @param stop + Last row. + @return None (void). +*/ +// To scroll the whole display, run: display.startscrollleft(0x00, 0x0F) +void Adafruit_SSD1306::startscrollleft(uint8_t __attribute__((unused)) start, uint8_t __attribute__((unused)) stop) { + // not used for now +} + +/*! + @brief Activate a diagonal scroll for all or part of the display. + @param start + First row. + @param stop + Last row. + @return None (void). +*/ +// display.startscrolldiagright(0x00, 0x0F) +void Adafruit_SSD1306::startscrolldiagright(uint8_t __attribute__((unused)) start, uint8_t __attribute__((unused)) stop) { + // not used for now +} + +/*! + @brief Activate alternate diagonal scroll for all or part of the display. + @param start + First row. + @param stop + Last row. + @return None (void). +*/ +// To scroll the whole display, run: display.startscrolldiagleft(0x00, 0x0F) +void Adafruit_SSD1306::startscrolldiagleft(uint8_t __attribute__((unused)) start, uint8_t __attribute__((unused)) stop) { + // not used for now +} + +/*! + @brief Cease a previously-begun scrolling action. + @return None (void). +*/ +void Adafruit_SSD1306::stopscroll(void) { + // not used for now +} + +// OTHER HARDWARE SETTINGS ------------------------------------------------- + +/*! + @brief Enable or disable display invert mode (white-on-black vs + black-on-white). + @param i + If true, switch to invert mode (black-on-white), else normal + mode (white-on-black). + @return None (void). + @note This has an immediate effect on the display, no need to call the + display() function -- buffer contents are not changed, rather a + different pixel mode of the display hardware is used. When + enabled, drawing BLACK (value 0) pixels will actually draw white, + WHITE (value 1) will draw black. +*/ +void Adafruit_SSD1306::invertDisplay(boolean i) { + // not used for now + inverted_ = i; +} + +/*! + @brief Dim the display. + @param dim + true to enable lower brightness mode, false for full brightness. + @return None (void). + @note This has an immediate effect on the display, no need to call the + display() function -- buffer contents are not changed. +*/ +void Adafruit_SSD1306::dim(boolean dim) +{ + dimmed_ = dim; +} + +void Adafruit_SSD1306::ssd1306_command(uint8_t cmd) +{ + // TODO: parse command and act on it... + switch(cmd) + { + case SSD1306_DISPLAYOFF: + enabled_ = false; + break; + case SSD1306_DISPLAYON: + enabled_ = true; + break; + + default: break; + } +} \ No newline at end of file diff --git a/simulation/src/Print.cpp b/simulation/src/Print.cpp new file mode 100644 index 0000000..db53bc2 --- /dev/null +++ b/simulation/src/Print.cpp @@ -0,0 +1,337 @@ +/* Teensyduino Core Library + * http://www.pjrc.com/teensy/ + * Copyright (c) 2017 PJRC.COM, LLC. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * 1. The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * 2. If the Software is incorporated into a build system that allows + * selection among a list of target devices, then similar target + * devices manufactured by PJRC.COM must be included in the list of + * target devices and selectable in the same manner. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +// Long ago this file contained code from Arduino.cc, which was +// Copyright (c) 2008 David A. Mellis. No substantial portion of +// Arduino's original code remains. In fact, several improvements +// developed for Teensyduino have made their way back into +// Arduino's code base. :-) + +#include +#include "Print.h" + + +size_t Print::write(const uint8_t *buffer, size_t size) +{ + size_t count = 0; + while (size--) count += write(*buffer++); + return count; +} + + +// size_t Print::print(const String &s) +// { +// uint8_t buffer[33]; +// size_t count = 0; +// unsigned int index = 0; +// unsigned int len = s.length(); +// while (len > 0) { +// s.getBytes(buffer, sizeof(buffer), index); +// unsigned int nbytes = len; +// if (nbytes > sizeof(buffer)-1) nbytes = sizeof(buffer)-1; +// index += nbytes; +// len -= nbytes; +// count += write(buffer, nbytes); +// } +// return count; +// } + + +size_t Print::print(long n) +{ + uint8_t sign=0; + + if (n < 0) { + sign = '-'; + n = -n; + } + return printNumber(n, 10, sign); +} + + +size_t Print::println(void) +{ + uint8_t buf[2]={'\r', '\n'}; + return write(buf, 2); +} + +extern "C" { +__attribute__((weak)) +int _write(long file, char *ptr, int len) +{ + ((class Print *)file)->write((uint8_t *)ptr, len); + return len; +} +} + +int Print::printf(const char *format, ...) +{ + va_list ap; + va_start(ap, format); +#ifdef __STRICT_ANSI__ + return 0; // TODO: make this work with -std=c++0x +#else + return vdprintf((int)this, format, ap); +#endif +} + +int Print::printf(const __FlashStringHelper *format, ...) +{ + va_list ap; + va_start(ap, format); +#ifdef __STRICT_ANSI__ + return 0; +#else + return vdprintf((int)this, (const char *)format, ap); +#endif +} + +#ifdef __MKL26Z64__ + +// optimized code inspired by Stimmer's optimization +// obviously a dit different, adapted to 32 bit Cortex-M0+ +// http://forum.arduino.cc/index.php?topic=167414.msg1293679#msg1293679 +// http://forum.arduino.cc/index.php?topic=167414.msg1309482#msg1309482 +// equivelant code: +// mod = div % 10; +// div = div / 10; +// tmp1 = {random}; +// tmp2 = 10; +#if 1 +// https://forum.pjrc.com/threads/28932-LC-is-10-9-times-slower-than-T3-1?p=76072&viewfull=1#post76072 +void inline divmod10_v2(uint32_t n,uint32_t *div,uint32_t *mod) { + uint32_t p,q; + /* Using 32.16 fixed point representation p.q */ + /* p.q = (n+1)/512 */ + q = (n&0xFFFF) + 1; + p = (n>>16); + /* p.q = 51*(n+1)/512 */ + q = 13107*q; + p = 13107*p; + /* p.q = (1+1/2^8+1/2^16+1/2^24)*51*(n+1)/512 */ + q = q + (q>>16) + (p&0xFFFF); + p = p + (p>>16) + (q>>16); + /* divide by 2 */ + p = p>>1; + *div = p; + *mod = n-10*p; +} +#define divmod10_asm(div, mod, tmp1, tmp2, const3333) \ + divmod10_v2(div, &div, &mod); +/* +#define divmod10_asm(div, mod, tmp1, tmp2, const3333) \ +asm ( \ + " lsr %2, %0, #16" "\n\t" \ + " mul %2, %4" "\n\t" \ + " uxth %1, %0" "\n\t" \ + " mul %1, %4" "\n\t" \ + " add %1, #1" "\n\t" \ + " lsr %0, %2, #16" "\n\t" \ + " lsl %2, %2, #16" "\n\t" \ + " add %1, %2" "\n\t" \ + " mov %3, #0" "\n\t" \ + " adc %0, %3" "\n\t" \ + " lsl %0, %0, #15" "\n\t" \ + " lsr %2, %1, #17" "\n\t" \ + " orr %0, %2" "\n\t" \ + " lsl %1, %1, #15" "\n\t" \ + " lsr %2, %1, #16" "\n\t" \ + " lsl %3, %0, #16" "\n\t" \ + " orr %2, %3" "\n\t" \ + " lsr %3, %0, #16" "\n\t" \ + " add %1, %0" "\n\t" \ + " adc %0, %1" "\n\t" \ + " sub %0, %1" "\n\t" \ + " add %1, %2" "\n\t" \ + " adc %0, %3" "\n\t" \ + " lsr %1, %1, #4" "\n\t" \ + " mov %3, #10" "\n\t" \ + " mul %1, %3" "\n\t" \ + " lsr %1, %1, #28" "\n\t" \ + : "+l" (div), \ + "=&l" (mod), \ + "=&l" (tmp1), \ + "=&l" (tmp2) \ + : "l" (const3333) \ + : \ +) +*/ +#else +#define divmod10_asm(_div, _mod, _tmp1, _tmp2, _const3333) \ + ({ _tmp1 = _div; _div = _div / 10; _mod = _tmp1 - _div * 10; }) + // ({_mod = _div % 10, _div = _div / 10; }) +#endif + + +size_t Print::printNumberDec(unsigned long n, uint8_t sign) +{ + uint8_t buf[11], *p; + uint32_t digit; + //uint32_t t1, t2, c3333=0x3333; + + p = buf + (sizeof(buf)); + do { + uint32_t div; + divmod10_v2(n, &div, &digit); + n = div; + //divmod10_asm(n, digit, t1, t2, c3333); + *--p = digit + '0'; + } while (n); + if (sign) *--p = '-'; + return write(p, sizeof(buf) - (p - buf)); +} + +size_t Print::printNumberHex(unsigned long n) +{ + uint8_t digit, buf[8], *p; + + p = buf + (sizeof(buf)); + do { + digit = n & 15; + *--p = (digit < 10) ? '0' + digit : 'A' + digit - 10; + n >>= 4; + } while (n); + return write(p, sizeof(buf) - (p - buf)); +} + +size_t Print::printNumberBin(unsigned long n) +{ + uint8_t buf[32], *p; + + p = buf + (sizeof(buf)); + do { + *--p = '0' + ((uint8_t)n & 1); + n >>= 1; + } while (n); + return write(p, sizeof(buf) - (p - buf)); +} + +size_t Print::printNumberAny(unsigned long n, uint8_t base) +{ + uint8_t digit, buf[21], *p; + uint32_t tmp; + + p = buf + sizeof(buf); + do { + tmp = n; + n = n / base; + digit = tmp - n * base; + *--p = (digit < 10) ? '0' + digit : 'A' + digit - 10; + } while (n); + return write(p, sizeof(buf) - (p - buf)); +} + +#else + +size_t Print::printNumber(unsigned long n, uint8_t base, uint8_t sign) +{ + uint8_t buf[34]; + uint8_t digit, i; + + // TODO: make these checks as inline, since base is + // almost always a constant. base = 0 (BYTE) should + // inline as a call directly to write() + if (base == 0) { + return write((uint8_t)n); + } else if (base == 1) { + base = 10; + } + + + if (n == 0) { + buf[sizeof(buf) - 1] = '0'; + i = sizeof(buf) - 1; + } else { + i = sizeof(buf) - 1; + while (1) { + digit = n % base; + buf[i] = ((digit < 10) ? '0' + digit : 'A' + digit - 10); + n /= base; + if (n == 0) break; + i--; + } + } + if (sign) { + i--; + buf[i] = '-'; + } + return write(buf + i, sizeof(buf) - i); +} + +#endif + +size_t Print::printFloat(double number, uint8_t digits) +{ + uint8_t sign=0; + size_t count=0; + + if (isnan(number)) return print("nan"); + if (isinf(number)) return print("inf"); + if (number > 4294967040.0f) return print("ovf"); // constant determined empirically + if (number <-4294967040.0f) return print("ovf"); // constant determined empirically + + // Handle negative numbers + if (number < 0.0) { + sign = 1; + number = -number; + } + + // Round correctly so that print(1.999, 2) prints as "2.00" + double rounding = 0.5; + for (uint8_t i=0; i 0) { + uint8_t n, buf[16], count=1; + buf[0] = '.'; + + // Extract digits from the remainder one at a time + if (digits > sizeof(buf) - 1) digits = sizeof(buf) - 1; + + while (digits-- > 0) { + remainder *= 10.0; + n = (uint8_t)(remainder); + buf[count++] = '0' + n; + remainder -= n; + } + count += write(buf, count); + } + return count; +} + + diff --git a/simulation/src/filters.cpp b/simulation/src/filters.cpp new file mode 100644 index 0000000..6f7eed9 --- /dev/null +++ b/simulation/src/filters.cpp @@ -0,0 +1,3 @@ +#include + +#include "FilterOnepole.cpp" diff --git a/simulation/src/glcdfont.c b/simulation/src/glcdfont.c new file mode 100644 index 0000000..6f88bd2 --- /dev/null +++ b/simulation/src/glcdfont.c @@ -0,0 +1,276 @@ +// This is the 'classic' fixed-space bitmap font for Adafruit_GFX since 1.0. +// See gfxfont.h for newer custom bitmap font info. + +#ifndef FONT5X7_H +#define FONT5X7_H + +#ifdef __AVR__ + #include + #include +#elif defined(ESP8266) + #include +#else + #define PROGMEM +#endif + +// Standard ASCII 5x7 font + +static const unsigned char font[] PROGMEM = { + 0x00, 0x00, 0x00, 0x00, 0x00, + 0x3E, 0x5B, 0x4F, 0x5B, 0x3E, + 0x3E, 0x6B, 0x4F, 0x6B, 0x3E, + 0x1C, 0x3E, 0x7C, 0x3E, 0x1C, + 0x18, 0x3C, 0x7E, 0x3C, 0x18, + 0x1C, 0x57, 0x7D, 0x57, 0x1C, + 0x1C, 0x5E, 0x7F, 0x5E, 0x1C, + 0x00, 0x18, 0x3C, 0x18, 0x00, + 0xFF, 0xE7, 0xC3, 0xE7, 0xFF, + 0x00, 0x18, 0x24, 0x18, 0x00, + 0xFF, 0xE7, 0xDB, 0xE7, 0xFF, + 0x30, 0x48, 0x3A, 0x06, 0x0E, + 0x26, 0x29, 0x79, 0x29, 0x26, + 0x40, 0x7F, 0x05, 0x05, 0x07, + 0x40, 0x7F, 0x05, 0x25, 0x3F, + 0x5A, 0x3C, 0xE7, 0x3C, 0x5A, + 0x7F, 0x3E, 0x1C, 0x1C, 0x08, + 0x08, 0x1C, 0x1C, 0x3E, 0x7F, + 0x14, 0x22, 0x7F, 0x22, 0x14, + 0x5F, 0x5F, 0x00, 0x5F, 0x5F, + 0x06, 0x09, 0x7F, 0x01, 0x7F, + 0x00, 0x66, 0x89, 0x95, 0x6A, + 0x60, 0x60, 0x60, 0x60, 0x60, + 0x94, 0xA2, 0xFF, 0xA2, 0x94, + 0x08, 0x04, 0x7E, 0x04, 0x08, + 0x10, 0x20, 0x7E, 0x20, 0x10, + 0x08, 0x08, 0x2A, 0x1C, 0x08, + 0x08, 0x1C, 0x2A, 0x08, 0x08, + 0x1E, 0x10, 0x10, 0x10, 0x10, + 0x0C, 0x1E, 0x0C, 0x1E, 0x0C, + 0x30, 0x38, 0x3E, 0x38, 0x30, + 0x06, 0x0E, 0x3E, 0x0E, 0x06, + 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x5F, 0x00, 0x00, + 0x00, 0x07, 0x00, 0x07, 0x00, + 0x14, 0x7F, 0x14, 0x7F, 0x14, + 0x24, 0x2A, 0x7F, 0x2A, 0x12, + 0x23, 0x13, 0x08, 0x64, 0x62, + 0x36, 0x49, 0x56, 0x20, 0x50, + 0x00, 0x08, 0x07, 0x03, 0x00, + 0x00, 0x1C, 0x22, 0x41, 0x00, + 0x00, 0x41, 0x22, 0x1C, 0x00, + 0x2A, 0x1C, 0x7F, 0x1C, 0x2A, + 0x08, 0x08, 0x3E, 0x08, 0x08, + 0x00, 0x80, 0x70, 0x30, 0x00, + 0x08, 0x08, 0x08, 0x08, 0x08, + 0x00, 0x00, 0x60, 0x60, 0x00, + 0x20, 0x10, 0x08, 0x04, 0x02, + 0x3E, 0x51, 0x49, 0x45, 0x3E, + 0x00, 0x42, 0x7F, 0x40, 0x00, + 0x72, 0x49, 0x49, 0x49, 0x46, + 0x21, 0x41, 0x49, 0x4D, 0x33, + 0x18, 0x14, 0x12, 0x7F, 0x10, + 0x27, 0x45, 0x45, 0x45, 0x39, + 0x3C, 0x4A, 0x49, 0x49, 0x31, + 0x41, 0x21, 0x11, 0x09, 0x07, + 0x36, 0x49, 0x49, 0x49, 0x36, + 0x46, 0x49, 0x49, 0x29, 0x1E, + 0x00, 0x00, 0x14, 0x00, 0x00, + 0x00, 0x40, 0x34, 0x00, 0x00, + 0x00, 0x08, 0x14, 0x22, 0x41, + 0x14, 0x14, 0x14, 0x14, 0x14, + 0x00, 0x41, 0x22, 0x14, 0x08, + 0x02, 0x01, 0x59, 0x09, 0x06, + 0x3E, 0x41, 0x5D, 0x59, 0x4E, + 0x7C, 0x12, 0x11, 0x12, 0x7C, + 0x7F, 0x49, 0x49, 0x49, 0x36, + 0x3E, 0x41, 0x41, 0x41, 0x22, + 0x7F, 0x41, 0x41, 0x41, 0x3E, + 0x7F, 0x49, 0x49, 0x49, 0x41, + 0x7F, 0x09, 0x09, 0x09, 0x01, + 0x3E, 0x41, 0x41, 0x51, 0x73, + 0x7F, 0x08, 0x08, 0x08, 0x7F, + 0x00, 0x41, 0x7F, 0x41, 0x00, + 0x20, 0x40, 0x41, 0x3F, 0x01, + 0x7F, 0x08, 0x14, 0x22, 0x41, + 0x7F, 0x40, 0x40, 0x40, 0x40, + 0x7F, 0x02, 0x1C, 0x02, 0x7F, + 0x7F, 0x04, 0x08, 0x10, 0x7F, + 0x3E, 0x41, 0x41, 0x41, 0x3E, + 0x7F, 0x09, 0x09, 0x09, 0x06, + 0x3E, 0x41, 0x51, 0x21, 0x5E, + 0x7F, 0x09, 0x19, 0x29, 0x46, + 0x26, 0x49, 0x49, 0x49, 0x32, + 0x03, 0x01, 0x7F, 0x01, 0x03, + 0x3F, 0x40, 0x40, 0x40, 0x3F, + 0x1F, 0x20, 0x40, 0x20, 0x1F, + 0x3F, 0x40, 0x38, 0x40, 0x3F, + 0x63, 0x14, 0x08, 0x14, 0x63, + 0x03, 0x04, 0x78, 0x04, 0x03, + 0x61, 0x59, 0x49, 0x4D, 0x43, + 0x00, 0x7F, 0x41, 0x41, 0x41, + 0x02, 0x04, 0x08, 0x10, 0x20, + 0x00, 0x41, 0x41, 0x41, 0x7F, + 0x04, 0x02, 0x01, 0x02, 0x04, + 0x40, 0x40, 0x40, 0x40, 0x40, + 0x00, 0x03, 0x07, 0x08, 0x00, + 0x20, 0x54, 0x54, 0x78, 0x40, + 0x7F, 0x28, 0x44, 0x44, 0x38, + 0x38, 0x44, 0x44, 0x44, 0x28, + 0x38, 0x44, 0x44, 0x28, 0x7F, + 0x38, 0x54, 0x54, 0x54, 0x18, + 0x00, 0x08, 0x7E, 0x09, 0x02, + 0x18, 0xA4, 0xA4, 0x9C, 0x78, + 0x7F, 0x08, 0x04, 0x04, 0x78, + 0x00, 0x44, 0x7D, 0x40, 0x00, + 0x20, 0x40, 0x40, 0x3D, 0x00, + 0x7F, 0x10, 0x28, 0x44, 0x00, + 0x00, 0x41, 0x7F, 0x40, 0x00, + 0x7C, 0x04, 0x78, 0x04, 0x78, + 0x7C, 0x08, 0x04, 0x04, 0x78, + 0x38, 0x44, 0x44, 0x44, 0x38, + 0xFC, 0x18, 0x24, 0x24, 0x18, + 0x18, 0x24, 0x24, 0x18, 0xFC, + 0x7C, 0x08, 0x04, 0x04, 0x08, + 0x48, 0x54, 0x54, 0x54, 0x24, + 0x04, 0x04, 0x3F, 0x44, 0x24, + 0x3C, 0x40, 0x40, 0x20, 0x7C, + 0x1C, 0x20, 0x40, 0x20, 0x1C, + 0x3C, 0x40, 0x30, 0x40, 0x3C, + 0x44, 0x28, 0x10, 0x28, 0x44, + 0x4C, 0x90, 0x90, 0x90, 0x7C, + 0x44, 0x64, 0x54, 0x4C, 0x44, + 0x00, 0x08, 0x36, 0x41, 0x00, + 0x00, 0x00, 0x77, 0x00, 0x00, + 0x00, 0x41, 0x36, 0x08, 0x00, + 0x02, 0x01, 0x02, 0x04, 0x02, + 0x3C, 0x26, 0x23, 0x26, 0x3C, + 0x1E, 0xA1, 0xA1, 0x61, 0x12, + 0x3A, 0x40, 0x40, 0x20, 0x7A, + 0x38, 0x54, 0x54, 0x55, 0x59, + 0x21, 0x55, 0x55, 0x79, 0x41, + 0x22, 0x54, 0x54, 0x78, 0x42, // a-umlaut + 0x21, 0x55, 0x54, 0x78, 0x40, + 0x20, 0x54, 0x55, 0x79, 0x40, + 0x0C, 0x1E, 0x52, 0x72, 0x12, + 0x39, 0x55, 0x55, 0x55, 0x59, + 0x39, 0x54, 0x54, 0x54, 0x59, + 0x39, 0x55, 0x54, 0x54, 0x58, + 0x00, 0x00, 0x45, 0x7C, 0x41, + 0x00, 0x02, 0x45, 0x7D, 0x42, + 0x00, 0x01, 0x45, 0x7C, 0x40, + 0x7D, 0x12, 0x11, 0x12, 0x7D, // A-umlaut + 0xF0, 0x28, 0x25, 0x28, 0xF0, + 0x7C, 0x54, 0x55, 0x45, 0x00, + 0x20, 0x54, 0x54, 0x7C, 0x54, + 0x7C, 0x0A, 0x09, 0x7F, 0x49, + 0x32, 0x49, 0x49, 0x49, 0x32, + 0x3A, 0x44, 0x44, 0x44, 0x3A, // o-umlaut + 0x32, 0x4A, 0x48, 0x48, 0x30, + 0x3A, 0x41, 0x41, 0x21, 0x7A, + 0x3A, 0x42, 0x40, 0x20, 0x78, + 0x00, 0x9D, 0xA0, 0xA0, 0x7D, + 0x3D, 0x42, 0x42, 0x42, 0x3D, // O-umlaut + 0x3D, 0x40, 0x40, 0x40, 0x3D, + 0x3C, 0x24, 0xFF, 0x24, 0x24, + 0x48, 0x7E, 0x49, 0x43, 0x66, + 0x2B, 0x2F, 0xFC, 0x2F, 0x2B, + 0xFF, 0x09, 0x29, 0xF6, 0x20, + 0xC0, 0x88, 0x7E, 0x09, 0x03, + 0x20, 0x54, 0x54, 0x79, 0x41, + 0x00, 0x00, 0x44, 0x7D, 0x41, + 0x30, 0x48, 0x48, 0x4A, 0x32, + 0x38, 0x40, 0x40, 0x22, 0x7A, + 0x00, 0x7A, 0x0A, 0x0A, 0x72, + 0x7D, 0x0D, 0x19, 0x31, 0x7D, + 0x26, 0x29, 0x29, 0x2F, 0x28, + 0x26, 0x29, 0x29, 0x29, 0x26, + 0x30, 0x48, 0x4D, 0x40, 0x20, + 0x38, 0x08, 0x08, 0x08, 0x08, + 0x08, 0x08, 0x08, 0x08, 0x38, + 0x2F, 0x10, 0xC8, 0xAC, 0xBA, + 0x2F, 0x10, 0x28, 0x34, 0xFA, + 0x00, 0x00, 0x7B, 0x00, 0x00, + 0x08, 0x14, 0x2A, 0x14, 0x22, + 0x22, 0x14, 0x2A, 0x14, 0x08, + 0x55, 0x00, 0x55, 0x00, 0x55, // #176 (25% block) missing in old code + 0xAA, 0x55, 0xAA, 0x55, 0xAA, // 50% block + 0xFF, 0x55, 0xFF, 0x55, 0xFF, // 75% block + 0x00, 0x00, 0x00, 0xFF, 0x00, + 0x10, 0x10, 0x10, 0xFF, 0x00, + 0x14, 0x14, 0x14, 0xFF, 0x00, + 0x10, 0x10, 0xFF, 0x00, 0xFF, + 0x10, 0x10, 0xF0, 0x10, 0xF0, + 0x14, 0x14, 0x14, 0xFC, 0x00, + 0x14, 0x14, 0xF7, 0x00, 0xFF, + 0x00, 0x00, 0xFF, 0x00, 0xFF, + 0x14, 0x14, 0xF4, 0x04, 0xFC, + 0x14, 0x14, 0x17, 0x10, 0x1F, + 0x10, 0x10, 0x1F, 0x10, 0x1F, + 0x14, 0x14, 0x14, 0x1F, 0x00, + 0x10, 0x10, 0x10, 0xF0, 0x00, + 0x00, 0x00, 0x00, 0x1F, 0x10, + 0x10, 0x10, 0x10, 0x1F, 0x10, + 0x10, 0x10, 0x10, 0xF0, 0x10, + 0x00, 0x00, 0x00, 0xFF, 0x10, + 0x10, 0x10, 0x10, 0x10, 0x10, + 0x10, 0x10, 0x10, 0xFF, 0x10, + 0x00, 0x00, 0x00, 0xFF, 0x14, + 0x00, 0x00, 0xFF, 0x00, 0xFF, + 0x00, 0x00, 0x1F, 0x10, 0x17, + 0x00, 0x00, 0xFC, 0x04, 0xF4, + 0x14, 0x14, 0x17, 0x10, 0x17, + 0x14, 0x14, 0xF4, 0x04, 0xF4, + 0x00, 0x00, 0xFF, 0x00, 0xF7, + 0x14, 0x14, 0x14, 0x14, 0x14, + 0x14, 0x14, 0xF7, 0x00, 0xF7, + 0x14, 0x14, 0x14, 0x17, 0x14, + 0x10, 0x10, 0x1F, 0x10, 0x1F, + 0x14, 0x14, 0x14, 0xF4, 0x14, + 0x10, 0x10, 0xF0, 0x10, 0xF0, + 0x00, 0x00, 0x1F, 0x10, 0x1F, + 0x00, 0x00, 0x00, 0x1F, 0x14, + 0x00, 0x00, 0x00, 0xFC, 0x14, + 0x00, 0x00, 0xF0, 0x10, 0xF0, + 0x10, 0x10, 0xFF, 0x10, 0xFF, + 0x14, 0x14, 0x14, 0xFF, 0x14, + 0x10, 0x10, 0x10, 0x1F, 0x00, + 0x00, 0x00, 0x00, 0xF0, 0x10, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xFF, 0xFF, 0xFF, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xFF, 0xFF, + 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, + 0x38, 0x44, 0x44, 0x38, 0x44, + 0xFC, 0x4A, 0x4A, 0x4A, 0x34, // sharp-s or beta + 0x7E, 0x02, 0x02, 0x06, 0x06, + 0x02, 0x7E, 0x02, 0x7E, 0x02, + 0x63, 0x55, 0x49, 0x41, 0x63, + 0x38, 0x44, 0x44, 0x3C, 0x04, + 0x40, 0x7E, 0x20, 0x1E, 0x20, + 0x06, 0x02, 0x7E, 0x02, 0x02, + 0x99, 0xA5, 0xE7, 0xA5, 0x99, + 0x1C, 0x2A, 0x49, 0x2A, 0x1C, + 0x4C, 0x72, 0x01, 0x72, 0x4C, + 0x30, 0x4A, 0x4D, 0x4D, 0x30, + 0x30, 0x48, 0x78, 0x48, 0x30, + 0xBC, 0x62, 0x5A, 0x46, 0x3D, + 0x3E, 0x49, 0x49, 0x49, 0x00, + 0x7E, 0x01, 0x01, 0x01, 0x7E, + 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, + 0x44, 0x44, 0x5F, 0x44, 0x44, + 0x40, 0x51, 0x4A, 0x44, 0x40, + 0x40, 0x44, 0x4A, 0x51, 0x40, + 0x00, 0x00, 0xFF, 0x01, 0x03, + 0xE0, 0x80, 0xFF, 0x00, 0x00, + 0x08, 0x08, 0x6B, 0x6B, 0x08, + 0x36, 0x12, 0x36, 0x24, 0x36, + 0x06, 0x0F, 0x09, 0x0F, 0x06, + 0x00, 0x00, 0x18, 0x18, 0x00, + 0x00, 0x00, 0x10, 0x10, 0x00, + 0x30, 0x40, 0xFF, 0x01, 0x01, + 0x00, 0x1F, 0x01, 0x01, 0x1E, + 0x00, 0x19, 0x1D, 0x17, 0x12, + 0x00, 0x3C, 0x3C, 0x3C, 0x3C, + 0x00, 0x00, 0x00, 0x00, 0x00 // #255 NBSP +}; +#endif // FONT5X7_H diff --git a/simulation/src/nuevisim.cpp b/simulation/src/nuevisim.cpp new file mode 100644 index 0000000..416b5df --- /dev/null +++ b/simulation/src/nuevisim.cpp @@ -0,0 +1,278 @@ +#include + +#include + +#include +#include +#include "globals.h" +#include "hardware.h" + +#include + +// Forward declarations +static void SimQuit(void); +static int SimInit(void); +static int SimRun(void); +static void SimLoop(std::function, std::function); + + + +extern Adafruit_SSD1306 display; +extern Adafruit_MPR121 touchSensor; +SimWire Wire; +SimSerial Serial; + + +static const int scale = 2; + +static SDL_Window *window; +static SDL_Surface *surface; + +void _reboot_Teensyduino_() +{ + // TODO: reboot + // Ignore that this is marked as noreturn + printf("Some kind of panic, rebooting teensy...\n"); + setup(); +} + +extern void menu(void); +extern void initDisplay(void); +extern void breath(); +extern int noteValueCheck(int); +extern unsigned int breathCurve(unsigned int); +extern void pitch_bend(void); +extern void portamento_(void); +extern void extraController(void); +extern void statusLEDs(void); +extern void doorKnobCheck(void); +extern void portOn(void); +extern void portOff(void); +extern void port(void); +extern void readSwitches(void); +extern int patchLimit(int value); + +static uint8_t digitalInputs[256]; // random number of inputs.. +static uint8_t digitalOutputs[256]; // random number of inputs.. +static uint16_t analogInputs[256]; // random number of inputs.. +static uint16_t analogOutputs[256]; // random number of inputs.. +static int _analogRes = 12; + +void digitalWrite(uint8_t pin, uint8_t val) +{ + printf("digital write %d = %d\n", pin, val); + digitalOutputs[pin] = val; +} + +uint8_t digitalRead(uint8_t pin) { + return digitalInputs[pin]; +} + +void delay(unsigned int ms) +{ + uint32_t endTick = SDL_GetTicks() + ms; + auto checktime = [endTick]() -> bool { return endTick > SDL_GetTicks(); }; + SimLoop(checktime,NULL); +} + +void pinMode(uint8_t __attribute((unused)) pin, uint8_t __attribute((unused)) mode) +{ + +} + +int analogRead(uint8_t pin) +{ + return analogInputs[pin]; +} + + +void analogReadRes(unsigned int __attribute__((unused)) bits) +{ + // ?? +} + +uint32_t analogWriteRes(uint32_t res) +{ + _analogRes = res; // ?? + return _analogRes; // ?? +} + + +void analogWriteFrequency(uint8_t __unused pin, float __unused frequency) +{ +} + +void analogWrite(uint8_t pin, int value) +{ + analogOutputs[pin] = value; +} + + +uint16_t micros() +{ + return SDL_GetTicks()*1000; +} + +uint16_t millis() +{ + return SDL_GetTicks(); +} + +int touchRead(uint8_t reg){ return touchSensor.readRegister16(reg); } + + +int main() +{ + return SimRun(); +} + +//*********************************************************** + +static void GetDisplay(SDL_Surface* dest) +{ + int w = display.width(); + int h = display.height(); + + SDL_LockSurface(dest); + uint8_t* buffer = (uint8_t*)surface->pixels; + int pitch = surface->pitch; + + if(!display.enabled_) { + SDL_memset( buffer, 0, (w*h)*3); + } else { + int fg = 255; + int bg = 0; + + if( display.dimmed_) fg = 127; + if( display.inverted_ ) { int tmp = fg; fg = bg; bg = tmp; } + + for(int y = 0 ; y < h; ++y) { + for(int x = 0 ; x < w; ++x) { + int color = display.getPixel(x,y) ? fg : bg; + SDL_memset( buffer + pitch*y + x*3, color, 3); + } + } + } + SDL_UnlockSurface(dest); +} + + +static int doQuit = 0; + +static void SimLoop(std::function continue_predicate, std::function loopFunc) +{ + uint32_t time; + while( continue_predicate() ) { + SDL_Event event; + while( SDL_PollEvent(&event) ) { + if( event.type == SDL_QUIT ) { + doQuit = 1; + break; + } + if(event.type == SDL_KEYDOWN) + { + switch(event.key.keysym.sym) + { + case SDLK_LEFT: digitalInputs[mPin] = 0; break; + case SDLK_RIGHT: digitalInputs[ePin] = 0; break; + case SDLK_UP: digitalInputs[uPin] = 0; break; + case SDLK_DOWN: digitalInputs[dPin] = 0; break; + } + } + else if(event.type == SDL_KEYUP ) + { + switch(event.key.keysym.sym) + { + case SDLK_LEFT: digitalInputs[mPin] = 1; break; + case SDLK_RIGHT: digitalInputs[ePin] = 1; break; + case SDLK_UP: digitalInputs[uPin] = 1; break; + case SDLK_DOWN: digitalInputs[dPin] = 1; break; + } + fflush(stdout); + } + } + + if(doQuit) + break; + + if(loopFunc) loopFunc(); + + time = SDL_GetTicks(); + + // TODO: Get buffer from SSD1306 and copy to surface... + + GetDisplay(surface); + + SDL_Surface *dstSurface = SDL_GetWindowSurface(window); + SDL_BlitScaled( surface, NULL, dstSurface, NULL ); + SDL_UpdateWindowSurface(window); + + uint32_t timePassed = SDL_GetTicks() - time; + if( timePassed < 16 ) { + SDL_Delay( 16-timePassed ); + } + } +} + +static int SimRun( ) +{ + if( 0 != SimInit() ) { return 1; } + setup(); + SimLoop( []() -> bool { return true; }, loop ); + SimQuit(); + return 0; +} + + +static int SimInit() +{ + int result = result = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO ); + if( 0 != result ) { + fprintf(stderr, "Could not initialize SDL"); + return 1; + } + + window = SDL_CreateWindow( "TinySim" + , SDL_WINDOWPOS_UNDEFINED + , SDL_WINDOWPOS_UNDEFINED + , 128*scale + , 64*scale + , SDL_WINDOW_SHOWN ); + + if( window == NULL ) { + fprintf(stderr, "Could not create SDL window"); + SimQuit(); + return 2; + } + + SDL_SetWindowTitle( window, "Tiny Sim" ); + + memset(digitalInputs, 1, sizeof(digitalInputs)); + + int16_t w = display.width(); + int16_t h = display.height(); + + surface = SDL_CreateRGBSurfaceWithFormat(0, w, h, 0, SDL_PIXELFORMAT_RGB24); + if(!surface) + { + printf("Could not create surface with size %d %d\n", w,h); + SimQuit(); + } + + printf("create surface with size %d %d\n", w,h); + + return result; +} + +static void SimQuit() +{ + printf("Leaving Sim, see you later!\n"); + + if( window != NULL ) { + SDL_DestroyWindow( window ); + SDL_FreeSurface( surface ); + } + SDL_Quit(); +} + +#include "NuEVI.ino" diff --git a/simulation/src/simeeprom.cpp b/simulation/src/simeeprom.cpp new file mode 100644 index 0000000..2ce70d1 --- /dev/null +++ b/simulation/src/simeeprom.cpp @@ -0,0 +1,30 @@ + +#include +#include "EEPROM.h" + +// TODO: Fake eeprom a bit better, maybe even save to file. + +static char someFakeEEPROM_memory[4096]; + + +uint8_t EEPROMClass::read( int idx ) +{ + return someFakeEEPROM_memory[idx]; +} + + +void EEPROMClass::write( int idx, uint8_t val ) +{ + someFakeEEPROM_memory[idx] = val; +} + +void EEPROMClass::update( int idx, uint8_t val ) +{ + someFakeEEPROM_memory[idx] = val; +} +uint16_t EEPROMClass::length() +{ + return sizeof(someFakeEEPROM_memory); +} + +// TODO: Add missing functioality.. \ No newline at end of file diff --git a/simulation/src/simmidi.cpp b/simulation/src/simmidi.cpp new file mode 100644 index 0000000..74b45cb --- /dev/null +++ b/simulation/src/simmidi.cpp @@ -0,0 +1,56 @@ +#include +#include "midi.h" + +void midiSetChannel(byte __attribute__((unused)) channel){} + +byte midiGetChannel(){ return 1; } + +void midiSendProgramChange(int __attribute__((unused)) patch) +{ + +} + +void midiSendControlChange(int __attribute__((unused)) ccParam, int __attribute__((unused)) ccValue) +{ + +} + +void midiSendNoteOn(byte __attribute__((unused)) note, int __attribute__((unused)) velocity) +{ + +} + +void midiSendNoteOff(byte __attribute__((unused)) note) +{ + +} + +void midiSendAfterTouch(byte __attribute__((unused)) value) +{ + +} + +void midiSendPitchBend(int __attribute__((unused)) value) +{ + +} + +void midiDiscardInput() +{ + +} + +void midiReset() +{ + +} + +void midiPanic() +{ + // turn all notes off +} + +void midiInitialize(byte __attribute__((unused)) channel) +{ + +} \ No newline at end of file diff --git a/simulation/src/simserial.cpp b/simulation/src/simserial.cpp new file mode 100644 index 0000000..65e3bd7 --- /dev/null +++ b/simulation/src/simserial.cpp @@ -0,0 +1,43 @@ +#include +#include + + +#include "Arduino.h" + +/******************************** + * + */ +void SimSerial::begin(uint32_t) +{ + +} + + +void SimSerial::print(const char* str) +{ + printf( "[Serial::print] %s\n", str ); +} + + +void SimSerial::print(uint32_t intValue) +{ + printf( "[Serial::print] %d\n", intValue ); +} + +void SimSerial::println() +{ + printf("\n"); +} + + +void SimSerial::println(uint32_t intValue) +{ + printf( "[Serial::println] %d\n", intValue ); +} + + +void SimSerial::println(const char *str) +{ + printf( "[Serial::println] %s\n", str ); +} + diff --git a/simulation/src/simwire.cpp b/simulation/src/simwire.cpp new file mode 100644 index 0000000..3bb47a3 --- /dev/null +++ b/simulation/src/simwire.cpp @@ -0,0 +1,94 @@ + +#include + +#include "Wire.h" + +/******************************** + * + */ + +SimWire::SimWire( bool verbose ) +: verbose_( verbose ) +{ + +} + +void SimWire::setClock(uint32_t) +{ + // Ignore.. lol +} + + + +void SimWire::begin() +{ + +} + +void SimWire::beginTransmission(uint8_t address) +{ + if( verbose_ ) + printf("[SimWire::beginTransmission] $%02x\n", address); +} + + +void SimWire::beginTransmission(int address) +{ + beginTransmission((uint8_t)address); +} + + +uint8_t SimWire::endTransmission() +{ + if( verbose_ ) + printf("[SimWire::endTransmission]\n"); + return 0; +} + +uint8_t SimWire::endTransmission(uint8_t what) +{ + if( verbose_ ) + printf("[SimWire::endTransmission %d]\n", what); + return 0; +} + + + +uint8_t SimWire::requestFrom(uint8_t address, uint8_t count) +{ + if( verbose_ ) + printf("[SimWire::requestFrom] $%02x for %d bytes\n", address, count); + + // TODO: We must check if there is an actual slave for that address. + return 0; +} + +int SimWire::read() +{ + // TODO: Verify that bus is in read mode. + // if( current_slave_ != NULL ) { + // return current_slave_->i2cReadData( ); + // } else { + printf("No slave selected, returning ones\n"); + return 0xffu; + // } +} + +int SimWire::available() +{ + // TODO: This needs to be implemented!! + return 0; +} + + +size_t SimWire::write(uint8_t __attribute__((unused)) data) +{ + // // TODO: Verify that bus is in write mode. + // if( current_slave_ != NULL ) { + // current_slave_->i2cWriteData( data ); + // } else { + printf("No slave selected i2c writes to the void.\n"); + // } + return 1; +} +