|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h" |
|
#include "hardware.h" |
|
#include "runtime.h" |
|
|
|
|
|
void __attribute__((weak)) bloop() {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR) |
|
uint8_t bsystype = SYSTYPE_AVR; |
|
#elif defined(ARDUINO_ARCH_ESP8266) |
|
uint8_t bsystype = SYSTYPE_ESP8266; |
|
#elif defined(ARDUINO_ARCH_ESP32) |
|
uint8_t bsystype = SYSTYPE_ESP32; |
|
#elif defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_MBED_RP2040) |
|
uint8_t bsystype = SYSTYPE_RP2040; |
|
#elif defined(ARDUINO_ARCH_SAM) && defined(ARDUINO_ARCH_SAMD) |
|
uint8_t bsystype = SYSTYPE_SAM; |
|
#elif defined(ARDUINO_ARCH_XMC) |
|
uint8_t bsystype = SYSTYPE_XMC; |
|
#elif defined(ARDUINO_ARCH_SMT32) |
|
uint8_t bsystype = SYSTYPE_SMT32; |
|
#elif defined(ARDUINO_ARCH_RENESAS) |
|
uint8_t bsystype = SYSTYPE_NRENESA; |
|
#elif defined(ARDUINO_ARCH_MBED_GIGA) |
|
uint8_t bsystype = SYSTYPE_GIGA; |
|
#else |
|
uint8_t bsystype = SYSTYPE_UNKNOWN; |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int8_t id; |
|
int8_t od; |
|
int8_t idd = ISERIAL; |
|
int8_t odd = OSERIAL; |
|
int8_t ioer = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HASMSTAB |
|
uint8_t charcount[5]; |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char* nullbuffer = ibuffer; |
|
uint16_t nullbufsize = BUFSIZE; |
|
uint8_t bufferstat(uint8_t ch) { return 1; } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOPROGMEM |
|
#ifdef ARDUINO_ARCH_ESP32 |
|
#include <pgmspace.h> |
|
#else |
|
#include <avr/pgmspace.h> |
|
#endif |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINO_ARCH_MBED_GIGA |
|
#include "mbed.h" |
|
#include <mbed_mktime.h> |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOEEPROM |
|
#if defined(ARDUINO_ARCH_XMC) |
|
#include "src/XMCEEPROMLib/XMCEEPROMLib.h" |
|
#elif defined(ARDUINO_ARCH_SAMD) |
|
#define EEPROM_EMULATION_SIZE 2048 |
|
#include "src/FlashStorage_SAMD/FlashStorage_SAMD.h" |
|
#else |
|
#include <EEPROM.h> |
|
#endif |
|
#endif |
|
|
|
|
|
|
|
#ifdef ARDUINOSPI |
|
#include <SPI.h> |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(HASWIRE) |
|
#include <Wire.h> |
|
#endif |
|
|
|
#if defined(HASSIMPLEWIRE) && !defined(ARDUINODIRECTI2C) |
|
#include <Wire.h> |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOPS2 |
|
#include <PS2Keyboard.h> |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOUSBKBD |
|
#include <KeyboardController.h> |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOZX81KBD |
|
#include <ZX81Keyboard.h> |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef GIGAUSBKBD |
|
#include "USBHostGiga.h" |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef LCDSHIELD |
|
#include <LiquidCrystal.h> |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOLCDI2C |
|
#include <LiquidCrystal_I2C.h> |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(ARDUINONOKIA51) || defined(ARDUINOSSD1306) |
|
#include <U8g2lib.h> |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOILI9488 |
|
#include <Adafruit_GFX.h> |
|
#include <ILI9488.h> |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOMCUFRIEND |
|
#include <Adafruit_GFX.h> |
|
#include <MCUFRIEND_kbv.h> |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOTFT |
|
#include <memorysaver.h> |
|
#include <UTFT.h> |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOEDP47 |
|
#include "epd_driver.h" |
|
#include "font/firasans.h" |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(ARDUINOVGA) && defined(ARDUINO_TTGO_T7_V14_Mini32) |
|
#include <WiFi.h> |
|
#include <fabgl.h> |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOETH |
|
#include <Ethernet.h> |
|
#else |
|
#ifdef ARDUINOWIFI |
|
#if defined(ARDUINO_ARCH_ESP8266) |
|
#include <ESP8266WiFi.h> |
|
#elif defined(ARDUINO_ARCH_ESP32) |
|
#include <WiFi.h> |
|
#elif defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_SAMD) |
|
#include <WiFiNINA.h> |
|
#elif defined(ARDUINO_UNOR4_WIFI) |
|
#include <WiFiS3.h> |
|
#elif defined(ARDUINO_ARCH_MBED_GIGA) |
|
#include <WiFi.h> |
|
#endif |
|
#endif |
|
#endif |
|
|
|
|
|
#if defined(ARDUINOMQTT) |
|
#include <PubSubClient.h> |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOSD |
|
#define FILESYSTEMDRIVER |
|
#if defined(SOFTWARE_SPI_FOR_SD) |
|
#include <SoftSD.h> |
|
#else |
|
#include <SD.h> |
|
#endif |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ESPSPIFFS |
|
#define FILESYSTEMDRIVER |
|
#ifdef ARDUINO_ARCH_ESP8266 |
|
#include <FS.h> |
|
#endif |
|
#ifdef ARDUINO_ARCH_ESP32 |
|
#include <FS.h> |
|
#include <SPIFFS.h> |
|
#endif |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ESP32FAT |
|
#define FILESYSTEMDRIVER |
|
#ifdef ARDUINO_ARCH_ESP32 |
|
#include <FS.h> |
|
#include <FFat.h> |
|
#endif |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#ifdef ESPSDMMC |
|
#include "FS.h" |
|
#include "SD_MMC.h" |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef GIGAUSBFS |
|
#include <DigitalOut.h> |
|
#include <FATFileSystem.h> |
|
#include <Arduino_USBHostMbed5.h> |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef RP2040LITTLEFS |
|
#define FILESYSTEMDRIVER |
|
#define LFS_MBED_RP2040_VERSION_MIN_TARGET "LittleFS_Mbed_RP2040 v1.1.0" |
|
#define LFS_MBED_RP2040_VERSION_MIN 1001000 |
|
#define _LFS_LOGLEVEL_ 1 |
|
#define RP2040_FS_SIZE_KB 1024 |
|
#define FORCE_REFORMAT false |
|
#include <LittleFS_Mbed_RP2040.h> |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#ifdef STM32SDIO |
|
#define FILESYSTEMDRIVER |
|
#include <STM32SD.h> |
|
#ifndef SD_DETECT_PIN |
|
#define SD_DETECT_PIN SD_DETECT_NONE |
|
#endif |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(ARDUINOSD) ||defined(ESPSDMMC) |
|
#undef ESPSPIFFS |
|
#undef RP2040LITTLEFS |
|
#undef ESP32FAT |
|
#undef GIGAUSBFS |
|
#define FILESYSTEMDRIVER |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOEFS |
|
#undef ESPSPIFFS |
|
#undef ESP32FAT |
|
#undef RP2040LITTLEFS |
|
#undef ARDUINOSD |
|
#undef STM32SDIO |
|
#undef GIGAUSBFS |
|
#define FILESYSTEMDRIVER |
|
#endif |
|
|
|
|
|
|
|
#if (defined(ARDUINOI2CEEPROM) && defined(ARDUINOI2CEEPROM_BUFFERED)) || defined(ARDUINOEFS) |
|
#include <EepromFS.h |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef ARDUINO_AVR_MEGA2560 |
|
#undef SOFTWARE_SPI_FOR_SD |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef TFTESPI |
|
#include <TFT_eSPI.h> |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(ARDUINOI2CEEPROM) |
|
unsigned int i2ceepromsize = 0; |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const uint16_t serial_baudrate = 9600; |
|
uint8_t sendcr = 0; |
|
|
|
#ifdef ARDUINOPRT |
|
uint32_t serial1_baudrate = 9600; |
|
uint8_t blockmode = 1; |
|
#else |
|
const int serial1_baudrate = 0; |
|
uint8_t blockmode = 0; |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ioinit() { |
|
|
|
|
|
#ifdef STANDALONE |
|
idd = IKEYBOARD; |
|
odd = ODSP; |
|
#endif |
|
|
|
|
|
|
|
|
|
#ifdef STANDALONESECONDSERIAL |
|
idd = ISERIAL1; |
|
odd = OPRT; |
|
blockmode = 0; |
|
sendcr = 0; |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
signalon(); |
|
|
|
|
|
wiringbegin(); |
|
|
|
|
|
serialbegin(); |
|
|
|
|
|
#ifdef ARDUINOPRT |
|
prtbegin(); |
|
#endif |
|
|
|
|
|
#ifdef ARDUINOSPI |
|
spibegin(); |
|
#endif |
|
|
|
|
|
#if defined(HASWIRE) || defined(HASSIMPLEWIRE) |
|
wirebegin(); |
|
#endif |
|
|
|
|
|
fsbegin(); |
|
|
|
|
|
#ifdef ARDUINOMQTT |
|
netbegin(); |
|
mqttbegin(); |
|
#endif |
|
|
|
|
|
#if defined(HASKEYBOARD) || defined(HASKEYPAD) |
|
kbdbegin(); |
|
#endif |
|
|
|
|
|
#if defined(DISPLAYDRIVER) || defined(GRAPHDISPLAYDRIVER) |
|
dspbegin(); |
|
#endif |
|
|
|
|
|
|
|
#if defined(ARDUINOVGA) |
|
vgabegin(); |
|
#endif |
|
|
|
|
|
#ifdef ARDUINOSENSORS |
|
sensorbegin(); |
|
#endif |
|
|
|
|
|
#if defined(HASCLOCK) |
|
rtcbegin(); |
|
#endif |
|
|
|
|
|
ebegin(); |
|
|
|
|
|
iodefaults(); |
|
|
|
} |
|
|
|
|
|
|
|
int iostat(int channel) { |
|
switch(channel) { |
|
|
|
case ONULL: |
|
return bufferstat(0); |
|
break; |
|
|
|
case ISERIAL: |
|
return serialstat(0); |
|
break; |
|
|
|
case ODSP: |
|
return dspstat(0); |
|
break; |
|
|
|
#ifdef ARDUINOPRT |
|
case ISERIAL1: |
|
return prtstat(0); |
|
break; |
|
#endif |
|
|
|
#if defined(HASWIRE) |
|
case IWIRE: |
|
return wirestat(0); |
|
break; |
|
#endif |
|
|
|
#ifdef HASRF24 |
|
case IRADIO: |
|
return radiostat(0); |
|
break; |
|
#endif |
|
|
|
#ifdef ARDUINOMQTT |
|
case IMQTT: |
|
return mqttstat(0); |
|
break; |
|
#endif |
|
|
|
#ifdef FILESYSTEMDRIVER |
|
case IFILE: |
|
return fsstat(0); |
|
break; |
|
#endif |
|
} |
|
return 0; |
|
} |
|
|
|
|
|
void iodefaults() { |
|
od=odd; |
|
id=idd; |
|
} |
|
|
|
|
|
|
|
int cheof(int c) { if ((c == -1) || (c == 255)) return 1; else return 0; } |
|
|
|
|
|
char inch() { |
|
switch(id) { |
|
case ONULL: |
|
return bufferread(); |
|
case ISERIAL: |
|
return serialread(); |
|
#ifdef ARDUINOPRT |
|
case ISERIAL1: |
|
return prtread(); |
|
#endif |
|
#if defined(HASKEYBOARD) || defined(HASKEYPAD) || defined(HASVT52) |
|
case IKEYBOARD: |
|
#if defined(HASVT52) |
|
if (vt52avail()) return vt52read(); |
|
#endif |
|
#if defined(HASKEYBOARD) || defined(HASKEYPAD) |
|
return kbdread(); |
|
#endif |
|
#endif |
|
#if defined(HASWIRE) |
|
case IWIRE: |
|
return wireread(); |
|
#endif |
|
#ifdef HASRF24 |
|
case IRADIO: |
|
return radioread(); |
|
#endif |
|
#ifdef ARDUINOMQTT |
|
case IMQTT: |
|
return mqttread(); |
|
#endif |
|
#ifdef FILESYSTEMDRIVER |
|
case IFILE: |
|
return fileread(); |
|
#endif |
|
} |
|
return 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
char checkch(){ |
|
switch (id) { |
|
case ONULL: |
|
return buffercheckch(); |
|
case ISERIAL: |
|
return serialcheckch(); |
|
#ifdef FILESYSTEMDRIVER |
|
case IFILE: |
|
return fileavailable(); |
|
#endif |
|
#ifdef HASRF24 |
|
case IRADIO: |
|
return radioavailable(); |
|
#endif |
|
#ifdef ARDUINOMQTT |
|
case IMQTT: |
|
return mqttcheckch(); |
|
#endif |
|
#ifdef HASWIRE |
|
case IWIRE: |
|
return 0; |
|
#endif |
|
#ifdef ARDUINOPRT |
|
case ISERIAL1: |
|
return prtcheckch(); |
|
#endif |
|
case IKEYBOARD: |
|
#if defined(HASKEYBOARD) || defined(HASKEYPAD) |
|
return kbdcheckch(); |
|
#endif |
|
break; |
|
} |
|
return 0; |
|
} |
|
|
|
|
|
uint16_t availch(){ |
|
switch (id) { |
|
case ONULL: |
|
return bufferavailable(); |
|
case ISERIAL: |
|
return serialavailable(); |
|
#ifdef FILESYSTEMDRIVER |
|
case IFILE: |
|
return fileavailable(); |
|
#endif |
|
#ifdef HASRF24 |
|
case IRADIO: |
|
return radioavailable(); |
|
#endif |
|
#ifdef ARDUINOMQTT |
|
case IMQTT: |
|
return mqttavailable(); |
|
#endif |
|
#if defined(HASWIRE) |
|
case IWIRE: |
|
return wireavailable(); |
|
#endif |
|
#ifdef ARDUINOPRT |
|
case ISERIAL1: |
|
return prtavailable(); |
|
#endif |
|
case IKEYBOARD: |
|
#if defined(HASKEYBOARD) || defined(HASKEYPAD) || defined(HASVT52) |
|
#if defined(HASVT52) |
|
if (vt52avail()) return vt52avail(); |
|
#endif |
|
#if defined(HASKEYBOARD) || defined(HASKEYPAD) |
|
return kbdavailable(); |
|
#endif |
|
#endif |
|
break; |
|
} |
|
return 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t inb(char *b, uint16_t nb) { |
|
long m; |
|
uint16_t z; |
|
int16_t i = 0; |
|
|
|
if (blockmode == 1) { |
|
i=availch(); |
|
if (i>nb-1) i=nb-1; |
|
b[0]=(unsigned char)i; |
|
z=i; |
|
b[i+1]=0; |
|
b++; |
|
while (i--) {*b++=inch();} |
|
} else if (blockmode > 1) { |
|
m=millis(); |
|
while (i < nb-1) { |
|
if (availch()) b[++i]=inch(); |
|
if (millis() > m+blockmode) break; |
|
} |
|
b[0]=(unsigned char)i; |
|
z=i; |
|
b[i+1]=0; |
|
} else { |
|
b[0]=0; |
|
z=0; |
|
b[1]=0; |
|
} |
|
return z; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t consins(char *b, uint16_t nb) { |
|
char c; |
|
uint16_t z; |
|
|
|
z=1; |
|
while(z < nb) { |
|
c=inch(); |
|
if (id == ISERIAL || id == IKEYBOARD) { |
|
outch(c); |
|
} |
|
if (c == '\r') c=inch(); |
|
if (c == '\n' || c == -1 || c == 255) { |
|
break; |
|
} else if (c == 127 || c == 8) { |
|
if (z>1) z--; |
|
} else { |
|
b[z++]=c; |
|
} |
|
} |
|
b[z]=0; |
|
z--; |
|
b[0]=z; |
|
return z; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t ins(char *b, uint16_t nb) { |
|
switch(id) { |
|
case ONULL: |
|
return bufferins(b, nb); |
|
case ISERIAL: |
|
return serialins(b, nb); |
|
#if defined(HASKEYBOARD) || defined(HASKEYPAD) |
|
case IKEYBOARD: |
|
return kbdins(b, nb); |
|
#endif |
|
#ifdef ARDUINOPRT |
|
case ISERIAL1: |
|
return prtins(b, nb); |
|
#endif |
|
#if defined(HASWIRE) |
|
case IWIRE: |
|
return wireins(b, nb); |
|
#endif |
|
#ifdef HASRF24 |
|
case IRADIO: |
|
return radioins(b, nb); |
|
#endif |
|
#ifdef ARDUINOMQTT |
|
case IMQTT: |
|
return mqttins(b, nb); |
|
#endif |
|
#ifdef FILESYSTEMDRIVER |
|
case IFILE: |
|
return consins(b, nb); |
|
#endif |
|
default: |
|
b[0]=0; b[1]=0; |
|
return 0; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void outch(char c) { |
|
|
|
#ifdef HASMSTAB |
|
if (od <= OPRT) { |
|
if (c > 31) charcount[od]+=1; |
|
if (c == 10) charcount[od]=0; |
|
} |
|
#endif |
|
|
|
switch(od) { |
|
case ONULL: |
|
bufferwrite(c); |
|
break; |
|
case OSERIAL: |
|
serialwrite(c); |
|
break; |
|
#ifdef ARDUINOPRT |
|
case OPRT: |
|
prtwrite(c); |
|
break; |
|
#endif |
|
#ifdef FILESYSTEMDRIVER |
|
case OFILE: |
|
filewrite(c); |
|
break; |
|
#endif |
|
#ifdef ARDUINOVGA |
|
case ODSP: |
|
vgawrite(c); |
|
break; |
|
#elif defined(DISPLAYDRIVER) || defined(GRAPHDISPLAYDRIVER) |
|
case ODSP: |
|
dspwrite(c); |
|
break; |
|
#endif |
|
#ifdef ARDUINOMQTT |
|
case OMQTT: |
|
mqttwrite(c); |
|
break; |
|
#endif |
|
default: |
|
break; |
|
} |
|
byield(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
void outs(char *b, uint16_t l){ |
|
uint16_t i; |
|
|
|
switch (od) { |
|
#ifdef HASRF24 |
|
case ORADIO: |
|
radioouts(b, l); |
|
break; |
|
#endif |
|
#if (defined(HASWIRE)) |
|
case OWIRE: |
|
wireouts(b, l); |
|
break; |
|
#endif |
|
#ifdef ARDUINOMQTT |
|
case OMQTT: |
|
mqttouts(b, l); |
|
break; |
|
#endif |
|
#ifdef GRAPHDISPLAYDRIVER |
|
case ODSP: |
|
dspouts(b, l); |
|
break; |
|
#endif |
|
default: |
|
for(i=0; i<l; i++) outch(b[i]); |
|
} |
|
byield(); |
|
} |
|
|
|
|
|
void timeinit() {} |
|
|
|
|
|
void wiringbegin() {} |
|
|
|
|
|
void signalon() {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_XMC) || defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_RENESAS) |
|
extern "C" char* sbrk(int incr); |
|
long freeRam() { |
|
char top; |
|
return &top - reinterpret_cast<char*>(sbrk(0)); |
|
} |
|
#elif defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR) || defined(ARDUINO_ARCH_LGT8F) |
|
long freeRam() { |
|
extern int __heap_start,*__brkval; |
|
int v; |
|
return (int)&v - (__brkval == 0 |
|
? (int)&__heap_start : (int) __brkval); |
|
} |
|
#elif defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) |
|
long freeRam() { |
|
return ESP.getFreeHeap(); |
|
} |
|
#else |
|
long freeRam() { |
|
return 0; |
|
} |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
long freememorysize() { |
|
#if defined(ARDUINO_ARCH_RENESAS) |
|
return freeRam() - 2000; |
|
#endif |
|
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_STM32) |
|
return freeRam() - 4000; |
|
#endif |
|
#if defined(ARDUINO_ARCH_ESP32) |
|
#if defined(ARDUINO_TTGO_T7_V14_Mini32) |
|
return freeRam() - 4000; |
|
#else |
|
return freeRam() - 4000; |
|
#endif |
|
#endif |
|
#if defined(ARDUINO_ARCH_XMC) |
|
return freeRam() - 2000; |
|
#endif |
|
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR) || defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_LGT8F) |
|
int overhead=256; |
|
#ifdef ARDUINO_ARCH_LGT8F |
|
overhead+=0; |
|
#endif |
|
#ifdef ARDUINO_AVR_MEGA2560 |
|
overhead+=96; |
|
#endif |
|
#if (defined(ARDUINOWIRE) || defined(ARDUINOSIMPLEWIRE)) && !defined(ARDUINODIRECTI2C) |
|
overhead+=200; |
|
#endif |
|
#ifdef ARDUINORF24 |
|
overhead+=128; |
|
#endif |
|
#if defined(ARDUINOSD) |
|
overhead+=512; |
|
#endif |
|
#ifdef ARDUINOZX81KBD |
|
overhead+=64; |
|
#endif |
|
#ifdef ARDUINOETH |
|
overhead+=256; |
|
#endif |
|
return freeRam() - overhead; |
|
#endif |
|
#if defined(ARDUINO_NANO_RP2040_CONNECT) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ARCH_MBED_GIGA) |
|
return 65536; |
|
#endif |
|
return 0; |
|
} |
|
|
|
|
|
|
|
|
|
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR) |
|
void(* callzero)() = 0; |
|
#endif |
|
|
|
void restartsystem() { |
|
eflush(); |
|
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) |
|
ESP.restart(); |
|
#endif |
|
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR) |
|
callzero(); |
|
#endif |
|
#if defined(ARDUINO_ARCH_LGT8F) |
|
#endif |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(ARDUINO_ARCH_SAMD) |
|
#include "RTCZero.h" |
|
#include "ArduinoLowPower.h" |
|
RTCZero rtc; |
|
#endif |
|
|
|
|
|
#if defined(ARDUINO_ARCH_STM32) |
|
#include "STM32RTC.h" |
|
#include "STM32LowPower.h" |
|
STM32RTC& rtc = STM32RTC::getInstance(); |
|
#endif |
|
|
|
|
|
#if defined(ARDUINO_ARCH_RENESAS) |
|
#include "RTC.h" |
|
RTCTime rtc; |
|
#endif |
|
|
|
|
|
#if defined(ARDUINO_ARCH_ESP32) |
|
#include "time.h" |
|
#include <sys/time.h> |
|
#endif |
|
|
|
|
|
|
|
void rtcsqw(); |
|
|
|
#define LOWPOWERINTPIN 2 |
|
void aftersleepinterrupt(void) { } |
|
|
|
void activatesleep(long t) { |
|
eflush(); |
|
#if defined(ARDUINO_ARCH_ESP8266) |
|
ESP.deepSleep(t*1000); |
|
#endif |
|
#if defined(ARDUINO_ARCH_ESP32) |
|
esp_sleep_enable_timer_wakeup(t*1000); |
|
esp_deep_sleep_start(); |
|
#endif |
|
#if defined(ARDUINO_ARCH_SAMD) |
|
LowPower.sleep((int) t); |
|
#endif |
|
#if defined(ARDUINO_AVR_ATmega644) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void spibegin() { |
|
#ifdef ARDUINOSPI |
|
#ifdef ARDUINO_TTGO_T7_V14_Mini32 |
|
|
|
SPI.begin(14, 2, 12, 13); |
|
#else |
|
#ifdef TFTESPI |
|
|
|
pinMode(BOARD_POWERON, OUTPUT); |
|
digitalWrite(BOARD_POWERON, HIGH); |
|
|
|
|
|
pinMode(BOARD_SDCARD_CS, OUTPUT); |
|
pinMode(RADIO_CS_PIN, OUTPUT); |
|
pinMode(BOARD_TFT_CS, OUTPUT); |
|
|
|
digitalWrite(BOARD_SDCARD_CS, HIGH); |
|
digitalWrite(RADIO_CS_PIN, HIGH); |
|
digitalWrite(BOARD_TFT_CS, HIGH); |
|
|
|
pinMode(BOARD_SPI_MISO, INPUT_PULLUP); |
|
SPI.begin(BOARD_SPI_SCK, BOARD_SPI_MISO, BOARD_SPI_MOSI); |
|
#else |
|
SPI.begin(); |
|
#endif |
|
#endif |
|
#endif |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t rgbtovga(uint8_t r, uint8_t g, uint8_t b) { |
|
short vga; |
|
if (r>191 || g>191 || b>191) vga=8; else vga=0; |
|
vga=vga+r/128+g/128*2+b/128*4; |
|
return vga; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
#ifdef LCDSHIELD |
|
#define DISPLAYDRIVER |
|
#undef DISPLAYHASCOLOR |
|
#undef DISPLAYHASGRAPH |
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef LCDSHIELDPINS |
|
#ifdef ARDUINO_ESP8266_WEMOS_D1R1 |
|
#define LCDSHIELDPINS 0,2,4,14,12,13 |
|
#define LCD3VOLTS |
|
#else |
|
#define LCDSHIELDPINS 8,9,4,5,6,7 |
|
#endif |
|
#endif |
|
const int dsp_rows=2; |
|
const int dsp_columns=16; |
|
LiquidCrystal lcd(LCDSHIELDPINS); |
|
void dspbegin() { lcd.begin(dsp_columns, dsp_rows); dspsetscrollmode(1, 1); } |
|
void dspprintchar(char c, uint8_t col, uint8_t row) { lcd.setCursor(col, row); if (c) lcd.write(c);} |
|
void dspclear() { lcd.clear(); } |
|
void dspupdate() {} |
|
void dspsetcursor(uint8_t c) { if (c) lcd.blink(); else lcd.noBlink(); } |
|
void dspsetfgcolor(uint8_t c) {} |
|
void dspsetbgcolor(uint8_t c) {} |
|
void dspsetreverse(uint8_t c) {} |
|
uint8_t dspident() {return 0; } |
|
#define HASKEYPAD |
|
|
|
char keypadread(){ |
|
int a=analogRead(A0); |
|
#ifndef LCD3VOLTS |
|
if (a >= 850) return 0; |
|
else if (a>=600 && a<850) return 10; |
|
else if (a>=400 && a<600) return '1'; |
|
else if (a>=200 && a<400) return '3'; |
|
else if (a>=60 && a<200) return '4'; |
|
else return '2'; |
|
#else |
|
if (a >= 1000) return 0; |
|
else if (a>=900 && a<1000) return 10; |
|
else if (a>=700 && a<900) return '1'; |
|
else if (a>=400 && a<700) return '3'; |
|
else if (a>=100 && a<400) return '4'; |
|
else return '2'; |
|
#endif |
|
} |
|
|
|
uint8_t kbdrepeat=0; |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOLCDI2C |
|
#define DISPLAYDRIVER |
|
#undef DISPLAYHASCOLOR |
|
#undef DISPLAYHASGRAPH |
|
const int dsp_rows=4; |
|
const int dsp_columns=20; |
|
LiquidCrystal_I2C lcd(0x27, dsp_columns, dsp_rows); |
|
void dspbegin() { lcd.init(); lcd.backlight(); dspsetscrollmode(1, 1); } |
|
void dspprintchar(char c, uint8_t col, uint8_t row) { lcd.setCursor(col, row); if (c) lcd.write(c); } |
|
void dspclear() { lcd.clear(); } |
|
void dspupdate() {} |
|
void dspsetcursor(uint8_t c) { if (c) lcd.blink(); else lcd.noBlink(); } |
|
void dspsetfgcolor(uint8_t c) {} |
|
void dspsetbgcolor(uint8_t c) {} |
|
void dspsetreverse(uint8_t c) {} |
|
uint8_t dspident() {return 0; } |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINONOKIA51 |
|
#define DISPLAYDRIVER |
|
#define DISPLAYPAGEMODE |
|
#undef DISPLAYHASCOLOR |
|
#define DISPLAYHASGRAPH |
|
#ifndef NOKIA_CS |
|
#define NOKIA_CS 15 |
|
#endif |
|
#ifndef NOKIA_DC |
|
#define NOKIA_DC 0 |
|
#endif |
|
#ifndef NOKIA_RST |
|
#define NOKIA_RST 2 |
|
#endif |
|
U8G2_PCD8544_84X48_F_4W_HW_SPI u8g2(U8G2_R0, NOKIA_CS, NOKIA_DC, NOKIA_RST); |
|
const int dsp_rows=6; |
|
const int dsp_columns=10; |
|
typedef uint8_t dspcolor_t; |
|
dspcolor_t dspfgcolor = 1; |
|
dspcolor_t dspbgcolor = 0; |
|
char dspfontsize = 8; |
|
void dspbegin() { u8g2.begin(); u8g2.setFont(u8g2_font_amstrad_cpc_extended_8r); } |
|
void dspprintchar(char c, uint8_t col, uint8_t row) { char b[] = { 0, 0 }; b[0]=c; if (c) u8g2.drawStr(col*dspfontsize+2, (row+1)*dspfontsize, b); } |
|
void dspclear() { u8g2.clearBuffer(); u8g2.sendBuffer(); dspfgcolor=1; } |
|
void dspupdate() { u8g2.sendBuffer(); } |
|
void dspsetcursor(uint8_t c) {} |
|
void dspsetfgcolor(uint8_t c) {} |
|
void dspsetbgcolor(uint8_t c) {} |
|
void dspsetreverse(uint8_t c) {} |
|
uint8_t dspident() {return 0;} |
|
void rgbcolor(uint8_t r, uint8_t g, uint8_t b) {} |
|
void vgacolor(uint8_t c) { dspfgcolor=c%3; u8g2.setDrawColor(dspfgcolor); } |
|
void plot(int x, int y) { u8g2.setDrawColor(dspfgcolor); u8g2.drawPixel(x, y); dspgraphupdate(); } |
|
void line(int x0, int y0, int x1, int y1) { u8g2.drawLine(x0, y0, x1, y1); dspgraphupdate(); } |
|
void rect(int x0, int y0, int x1, int y1) { u8g2.drawFrame(x0, y0, x1-x0, y1-y0); dspgraphupdate(); } |
|
void frect(int x0, int y0, int x1, int y1) { u8g2.drawBox(x0, y0, x1-x0, y1-y0); dspgraphupdate(); } |
|
void circle(int x0, int y0, int r) { u8g2.drawCircle(x0, y0, r); dspgraphupdate(); } |
|
void fcircle(int x0, int y0, int r) { u8g2.drawDisc(x0, y0, r); dspgraphupdate(); } |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOEDP47 |
|
#define GRAPHDISPLAYDRIVER |
|
#define DISPLAYPAGEMODE |
|
#undef DISPLAYHASCOLOR |
|
#define DISPLAYHASGRAPH |
|
const int dsp_width=960; |
|
const int dsp_height=540; |
|
const int dsp_rows=0; |
|
const int dsp_columns=0; |
|
typedef uint8_t dspcolor_t; |
|
dspcolor_t dspfgcolor = 1; |
|
dspcolor_t dspbgcolor = 0; |
|
char dspfontsize = 24; |
|
int dspgraphcursor_x = 0; |
|
int dspgraphcursor_y = dspfontsize; |
|
void dspbegin() { epd_init(); dspclear(); } |
|
void dspprintstring(char* s) { |
|
epd_poweron(); |
|
writeln((GFXfont *)&FiraSans, s, &dspgraphcursor_x, &dspgraphcursor_y, NULL); |
|
epd_poweroff(); |
|
} |
|
void dspclear() { epd_poweron(); epd_clear(); epd_poweroff(); dspfgcolor=1; } |
|
void dspupdate() { } |
|
void dspsetcursor(uint8_t c) {} |
|
void dspsetfgcolor(uint8_t c) {} |
|
void dspsetbgcolor(uint8_t c) {} |
|
void dspsetreverse(uint8_t c) {} |
|
uint8_t dspident() {return 0;} |
|
void rgbcolor(uint8_t r, uint_8 g, uint8_t b) {} |
|
void vgacolor(uint8_t c) { dspfgcolor=c%3; } |
|
void plot(int x, int y) { } |
|
void line(int x0, int y0, int x1, int y1) { } |
|
void rect(int x0, int y0, int x1, int y1) { } |
|
void frect(int x0, int y0, int x1, int y1) { } |
|
void circle(int x0, int y0, int r) { } |
|
void fcircle(int x0, int y0, int r) { } |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOSSD1306 |
|
#define DISPLAYDRIVER |
|
#define DISPLAYPAGEMODE |
|
#undef DISLAYHASCOLOR |
|
#define DISPLAYHASGRAPH |
|
#define SSD1306WIDTH 32 |
|
#define SSD1306HEIGHT 128 |
|
|
|
|
|
|
|
|
|
#if SSD1306WIDTH == 32 |
|
|
|
|
|
|
|
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0); |
|
#endif |
|
#if SSD1306WIDTH == 64 |
|
|
|
#ifdef ARDUINO_heltec_wifi_lora_32_V2 |
|
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, 15, 4, 16); |
|
#else |
|
|
|
|
|
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0); |
|
#endif |
|
#endif |
|
const char dspfontsize = 8; |
|
const int dsp_rows=SSD1306WIDTH/dspfontsize; |
|
const int dsp_columns=SSD1306HEIGHT/dspfontsize; |
|
typedef uint8_t dspcolor_t; |
|
dspcolor_t dspfgcolor = 1; |
|
dspcolor_t dspbgcolor = 0; |
|
void dspbegin() { u8g2.begin(); u8g2.setFont(u8g2_font_amstrad_cpc_extended_8r); } |
|
void dspprintchar(char c, uint8_t col, uint8_t row) { char b[] = { 0, 0 }; b[0]=c; if (c) u8g2.drawStr(col*dspfontsize+2, (row+1)*dspfontsize, b); } |
|
void dspclear() { u8g2.clearBuffer(); u8g2.sendBuffer(); dspfgcolor=1; } |
|
void dspupdate() { u8g2.sendBuffer(); } |
|
void dspsetcursor(uint8_t c) {} |
|
void dspsetfgcolor(uint8_t c) {} |
|
void dspsetbgcolor(uint8_t c) {} |
|
void dspsetreverse(uint8_t c) {} |
|
uint8_t dspident() {return 0;} |
|
void rgbcolor(uint8_t r, uint8_t g, uint8_t b) {} |
|
void vgacolor(uint8_t c) { dspfgcolor=c%3; u8g2.setDrawColor(dspfgcolor); } |
|
void plot(int x, int y) { u8g2.setDrawColor(dspfgcolor); u8g2.drawPixel(x, y); dspgraphupdate(); } |
|
void line(int x0, int y0, int x1, int y1) { u8g2.drawLine(x0, y0, x1, y1); dspgraphupdate(); } |
|
void rect(int x0, int y0, int x1, int y1) { u8g2.drawFrame(x0, y0, x1-x0, y1-y0); dspgraphupdate(); } |
|
void frect(int x0, int y0, int x1, int y1) { u8g2.drawBox(x0, y0, x1-x0, y1-y0); dspgraphupdate(); } |
|
void circle(int x0, int y0, int r) { u8g2.drawCircle(x0, y0, r); dspgraphupdate(); } |
|
void fcircle(int x0, int y0, int r) { u8g2.drawDisc(x0, y0, r); dspgraphupdate(); } |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOILI9488 |
|
#define DISPLAYDRIVER |
|
#define DISPLAYHASCOLOR |
|
#define DISPLAYHASGRAPH |
|
#ifndef ILI_CS |
|
#define ILI_CS 9 |
|
#endif |
|
#ifndef ILI_DC |
|
#define ILI_DC 8 |
|
#endif |
|
#ifndef ILI_RST |
|
#define ILI_RST 7 |
|
#endif |
|
#ifndef ILI_LED |
|
#define ILI_LED A3 |
|
#endif |
|
ILI9488 tft = ILI9488(ILI_CS, ILI_DC, ILI_RST); |
|
|
|
const int dsp_rows=20; |
|
const int dsp_columns=30; |
|
char dspfontsize = 16; |
|
typedef uint16_t dspcolor_t; |
|
const uint16_t dspdefaultfgcolor = 0xFFFF; |
|
const uint8_t dspdefaultfgvgacolor = 0x0F; |
|
dspcolor_t dspfgcolor = dspdefaultfgcolor; |
|
dspcolor_t dspbgcolor = 0; |
|
dspcolor_t dsptmpcolor = 0; |
|
uint8_t dspfgvgacolor = dspdefaultfgvgacolor; |
|
uint8_t dsptmpvgacolor = 0; |
|
void dspbegin() { |
|
tft.begin(); |
|
tft.setRotation(3); |
|
tft.setTextColor(dspfgcolor); |
|
tft.setTextSize(2); |
|
tft.fillScreen(dspbgcolor); |
|
pinMode(ILI_LED, OUTPUT); |
|
analogWrite(ILI_LED, 255); |
|
dspsetscrollmode(1, 4); |
|
} |
|
void dspprintchar(char c, uint8_t col, uint8_t row) { if (c) tft.drawChar(col*dspfontsize, row*dspfontsize, c, dspfgcolor, dspbgcolor, 2); } |
|
void dspclear() { |
|
tft.fillScreen(dspbgcolor); |
|
dspfgcolor = dspdefaultfgcolor; |
|
dspfgvgacolor = dspdefaultfgvgacolor; |
|
} |
|
void dspupdate() {} |
|
void dspsetcursor(uint8_t c) {} |
|
void dspsavepen() { dsptmpcolor=dspfgcolor; dsptmpvgacolor=dspfgvgacolor; } |
|
void dsprestorepen() { dspfgcolor=dsptmpcolor; dspfgvgacolor=dsptmpvgacolor; } |
|
void dspsetfgcolor(uint8_t c) { vgacolor(c); } |
|
void dspsetbgcolor(uint8_t c) { } |
|
void dspsetreverse(uint8_t c) {} |
|
uint8_t dspident() {return 0; } |
|
void rgbcolor(uint8_t r, uint8_t g, uint8_t b) { dspfgvgacolor=rgbtovga(r, g, b); dspfgcolor=tft.color565(r, g, b);} |
|
void vgacolor(uint8_t c) { |
|
short base=128; |
|
dspfgvgacolor=c; |
|
if (c==8) { dspfgcolor=tft.color565(64, 64, 64); return; } |
|
if (c>8) base=255; |
|
dspfgcolor=tft.color565(base*(c&1), base*((c&2)/2), base*((c&4)/4)); |
|
} |
|
void plot(int x, int y) { tft.drawPixel(x, y, dspfgcolor); } |
|
void line(int x0, int y0, int x1, int y1) { tft.drawLine(x0, y0, x1, y1, dspfgcolor); } |
|
void rect(int x0, int y0, int x1, int y1) { tft.drawRect(x0, y0, x1, y1, dspfgcolor);} |
|
void frect(int x0, int y0, int x1, int y1) { tft.fillRect(x0, y0, x1, y1, dspfgcolor); } |
|
void circle(int x0, int y0, int r) { tft.drawCircle(x0, y0, r, dspfgcolor); } |
|
void fcircle(int x0, int y0, int r) { tft.fillCircle(x0, y0, r, dspfgcolor); } |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOMCUFRIEND |
|
#define DISPLAYDRIVER |
|
#define DISPLAYHASCOLOR |
|
#define DISPLAYHASGRAPH |
|
#ifndef LCD_CS |
|
#define LCD_CS A3 |
|
#endif |
|
#ifndef LCD_CD |
|
#define LCD_CD A2 |
|
#endif |
|
#ifndef LCD_WR |
|
#define LCD_WR A1 |
|
#endif |
|
#ifndef LCD_RD |
|
#define LCD_RD A0 |
|
#endif |
|
#ifndef LCD_RESET |
|
#define LCD_RESET A4 |
|
#endif |
|
MCUFRIEND_kbv tft; |
|
|
|
const int dsp_rows=20; |
|
const int dsp_columns=30; |
|
char dspfontsize = 16; |
|
typedef uint16_t dspcolor_t; |
|
const uint16_t dspdefaultfgcolor = 0xFFFF; |
|
const uint8_t dspdefaultfgvgacolor = 0x0F; |
|
dspcolor_t dspfgcolor = dspdefaultfgcolor; |
|
dspcolor_t dspbgcolor = 0; |
|
dspcolor_t dsptmpcolor = 0; |
|
uint8_t dspfgvgacolor = dspdefaultfgvgacolor; |
|
uint8_t dsptmpvgacolor = 0; |
|
void dspbegin() { |
|
uint16_t ID = tft.readID(); |
|
if (ID == 0xD3D3) ID = 0x9481; |
|
tft.begin(ID); |
|
tft.setRotation(1); |
|
tft.setTextColor(dspfgcolor); |
|
tft.setTextSize(2); |
|
tft.fillScreen(dspbgcolor); |
|
dspsetscrollmode(1, 4); |
|
} |
|
void dspprintchar(char c, uint8_t col, uint8_t row) { if (c) tft.drawChar(col*dspfontsize, row*dspfontsize, c, dspfgcolor, dspbgcolor, 2); } |
|
void dspclear() { |
|
tft.fillScreen(dspbgcolor); |
|
dspfgcolor = dspdefaultfgcolor; |
|
dspfgvgacolor = dspdefaultfgvgacolor; |
|
} |
|
void dspupdate() {} |
|
void dspsetcursor(uint8_t c) {} |
|
void dspsavepen() { dsptmpcolor=dspfgcolor; dsptmpvgacolor=dspfgvgacolor; } |
|
void dsprestorepen() { dspfgcolor=dsptmpcolor; dspfgvgacolor=dsptmpvgacolor; } |
|
void dspsetfgcolor(uint8_t c) { vgacolor(c); } |
|
void dspsetbgcolor(uint8_t c) { } |
|
void dspsetreverse(uint8_t c) {} |
|
uint8_t dspident() {return 0; } |
|
void rgbcolor(uint8_t r, uint8_t g, uint8_t b) { dspfgvgacolor=rgbtovga(r, g, b); dspfgcolor=tft.color565(r, g, b);} |
|
void vgacolor(uint8_t c) { |
|
short base=128; |
|
dspfgvgacolor=c; |
|
if (c==8) { dspfgcolor=tft.color565(64, 64, 64); return; } |
|
if (c>8) base=255; |
|
dspfgcolor=tft.color565(base*(c&1), base*((c&2)/2), base*((c&4)/4)); |
|
} |
|
void plot(int x, int y) { tft.drawPixel(x, y, dspfgcolor); } |
|
void line(int x0, int y0, int x1, int y1) { tft.drawLine(x0, y0, x1, y1, dspfgcolor); } |
|
void rect(int x0, int y0, int x1, int y1) { tft.drawRect(x0, y0, x1, y1, dspfgcolor);} |
|
void frect(int x0, int y0, int x1, int y1) { tft.fillRect(x0, y0, x1, y1, dspfgcolor); } |
|
void circle(int x0, int y0, int r) { tft.drawCircle(x0, y0, r, dspfgcolor); } |
|
void fcircle(int x0, int y0, int r) { tft.fillCircle(x0, y0, r, dspfgcolor); } |
|
#endif |
|
|
|
#ifdef TFTESPI |
|
#define DISPLAYDRIVER |
|
#define DISPLAYHASCOLOR |
|
#define DISPLAYHASGRAPH |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const int dsp_rows=15; |
|
const int dsp_columns=20; |
|
char dspfontsize = 16; |
|
const char dspfontsizeindex = 2; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef uint16_t dspcolor_t; |
|
const uint16_t dspdefaultfgcolor = 0xFFFF; |
|
const uint8_t dspdefaultfgvgacolor = 0x0F; |
|
dspcolor_t dspfgcolor = dspdefaultfgcolor; |
|
dspcolor_t dspbgcolor = 0; |
|
dspcolor_t dsptmpcolor = 0; |
|
uint8_t dspfgvgacolor = dspdefaultfgvgacolor; |
|
uint8_t dsptmpvgacolor = 0; |
|
|
|
TFT_eSPI tft; |
|
|
|
void tftespisetBrightness(uint8_t value) |
|
{ |
|
static uint8_t level = 0; |
|
static uint8_t steps = 16; |
|
if (value == 0) { |
|
digitalWrite(BOARD_BL_PIN, 0); |
|
delay(3); |
|
level = 0; |
|
return; |
|
} |
|
if (level == 0) { |
|
digitalWrite(BOARD_BL_PIN, 1); |
|
level = steps; |
|
delayMicroseconds(30); |
|
} |
|
int from = steps - level; |
|
int to = steps - value; |
|
int num = (steps + to - from) % steps; |
|
for (int i = 0; i < num; i++) { |
|
digitalWrite(BOARD_BL_PIN, 0); |
|
digitalWrite(BOARD_BL_PIN, 1); |
|
} |
|
level = value; |
|
} |
|
void dspbegin() { |
|
tft.begin(); |
|
tft.setRotation(1); |
|
tft.setTextDatum(6); |
|
tft.setTextColor(dspfgcolor); |
|
tft.fillScreen(dspbgcolor); |
|
dspsetscrollmode(1, 4); |
|
pinMode(BOARD_BL_PIN, OUTPUT); |
|
tftespisetBrightness(16); |
|
} |
|
void dspprintchar(char c, uint8_t col, uint8_t row) { |
|
if (c) tft.drawChar(col*dspfontsize, row*dspfontsize, c, dspfgcolor, dspbgcolor, dspfontsizeindex); |
|
} |
|
void dspclear() { |
|
tft.fillScreen(dspbgcolor); |
|
dspfgcolor = dspdefaultfgcolor; |
|
dspfgvgacolor = dspdefaultfgvgacolor; |
|
} |
|
void dspupdate() {} |
|
void dspsetcursor(uint8_t c) {} |
|
void dspsavepen() { dsptmpcolor=dspfgcolor; dsptmpvgacolor=dspfgvgacolor; } |
|
void dsprestorepen() { dspfgcolor=dsptmpcolor; dspfgvgacolor=dsptmpvgacolor; } |
|
void dspsetfgcolor(uint8_t c) { vgacolor(c); } |
|
void dspsetbgcolor(uint8_t c) { } |
|
void dspsetreverse(uint8_t c) {} |
|
uint8_t dspident() {return 0; } |
|
void rgbcolor(uint8_t r, uint8_t g, uint8_t b) { dspfgvgacolor=rgbtovga(r, g, b); dspfgcolor=tft.color565(r, g, b);} |
|
void vgacolor(uint8_t c) { |
|
short base=128; |
|
dspfgvgacolor=c; |
|
if (c==8) { dspfgcolor=tft.color565(64, 64, 64); return; } |
|
if (c>8) base=255; |
|
dspfgcolor=tft.color565(base*(c&1), base*((c&2)/2), base*((c&4)/4)); |
|
} |
|
void plot(int x, int y) { tft.drawPixel(x, y, dspfgcolor); } |
|
void line(int x0, int y0, int x1, int y1) { tft.drawLine(x0, y0, x1, y1, dspfgcolor); } |
|
void rect(int x0, int y0, int x1, int y1) { tft.drawRect(x0, y0, x1, y1, dspfgcolor);} |
|
void frect(int x0, int y0, int x1, int y1) { tft.fillRect(x0, y0, x1, y1, dspfgcolor); } |
|
void circle(int x0, int y0, int r) { tft.drawCircle(x0, y0, r, dspfgcolor); } |
|
void fcircle(int x0, int y0, int r) { tft.fillCircle(x0, y0, r, dspfgcolor); } |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOGRAPHDUMMY |
|
#define DISPLAYDRIVER |
|
#undef DISPLAYHASCOLOR |
|
#define DISPLAYHASGRAPH |
|
const int dsp_rows=20; |
|
const int dsp_columns=30; |
|
const uint16_t dspdefaultfgcolor = 1; |
|
char dspfontsize = 16; |
|
typedef uint16_t dspcolor_t; |
|
dspcolor_t dspfgcolor = 0xFFFF; |
|
dspcolor_t dspbgcolor = 0x0000; |
|
void dspbegin() { dspsetscrollmode(1, 4); } |
|
void dspprintchar(char c, uint8_t col, uint8_t row) {} |
|
void dspclear() {} |
|
void dspupdate() {} |
|
void dspsetcursor(uint8_t c) {} |
|
void dspsetfgcolor(uint8_t c) {} |
|
void dspsetbgcolor(uint8_t c) {} |
|
void dspsetreverse(uint8_t c) {} |
|
uint8_t dspident() {return 0; } |
|
void rgbcolor(uint8_t r, uint8_t g, uint8_t b) { dspfgcolor=0; } |
|
void vgacolor(uint8_t c) { |
|
short base=128; |
|
if (c==8) { rgbcolor(64, 64, 64); return; } |
|
if (c>8) base=255; |
|
rgbcolor(base*(c&1), base*((c&2)/2), base*((c&4)/4)); |
|
} |
|
void plot(int x, int y) {} |
|
void line(int x0, int y0, int x1, int y1) {} |
|
void rect(int x0, int y0, int x1, int y1) {} |
|
void frect(int x0, int y0, int x1, int y1) {} |
|
void circle(int x0, int y0, int r) {} |
|
void fcircle(int x0, int y0, int r) {} |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOTFT |
|
#define DISPLAYDRIVER |
|
#define DISPLAYHASCOLOR |
|
#define DISPLAYHASGRAPH |
|
extern uint8_t SmallFont[]; |
|
extern uint8_t BigFont[]; |
|
#ifdef ARDUINO_SAM_DUE |
|
UTFT tft(CTE70,25,26,27,28); |
|
#else |
|
UTFT tft(CTE70,38,39,40,41); |
|
#endif |
|
const int dsp_rows=30; |
|
const int dsp_columns=50; |
|
char dspfontsize = 16; |
|
const uint32_t dspdefaultfgcolor = 0x00FFFFFF; |
|
const uint8_t dspdefaultfgvgacolor = 0x0F; |
|
typedef uint32_t dspcolor_t; |
|
dspcolor_t dspfgcolor = dspdefaultfgcolor; |
|
dspcolor_t dspbgcolor = 0; |
|
dspcolor_t dsptmpcolor = 0; |
|
uint8_t dspfgvgacolor = dspdefaultfgvgacolor; |
|
uint8_t dsptmpvgacolor = 0; |
|
void dspbegin() { tft.InitLCD(); tft.setFont(BigFont); tft.clrScr(); dspsetscrollmode(1, 4); } |
|
void dspprintchar(char c, uint8_t col, uint8_t row) { if (c) tft.printChar(c, col*dspfontsize, row*dspfontsize); } |
|
void dspclear() { |
|
tft.clrScr(); |
|
dspfgcolor = dspdefaultfgcolor; |
|
dspfgvgacolor = dspdefaultfgvgacolor; |
|
vgacolor(dspfgvgacolor); |
|
} |
|
void rgbcolor(uint8_t r, uint8_t g, uint8_t b) { |
|
tft.setColor(r,g,b); |
|
dspfgcolor=(r << 16) + (g << 8) + b; |
|
dspfgvgacolor=rgbtovga(r, g, b); |
|
} |
|
void vgacolor(uint8_t c) { |
|
short base=128; |
|
dspfgvgacolor=c; |
|
if (c==8) { tft.setColor(64, 64, 64); return; } |
|
if (c>8) base=255; |
|
tft.setColor(base*(c&1), base*((c&2)/2), base*((c&4)/4)); |
|
} |
|
void dspupdate() {} |
|
void dspsetcursor(uint8_t c) {} |
|
void dspsavepen() { dsptmpcolor=dspfgcolor; dsptmpvgacolor=dspfgvgacolor; } |
|
void dsprestorepen() { dspfgcolor=dsptmpcolor; dspfgvgacolor=dsptmpvgacolor; } |
|
void dspsetfgcolor(uint8_t c) { vgacolor(c); } |
|
void dspsetbgcolor(uint8_t c) { } |
|
void dspsetreverse(uint8_t c) {} |
|
uint8_t dspident() {return 0;} |
|
void plot(int x, int y) { tft.drawPixel(x, y); } |
|
void line(int x0, int y0, int x1, int y1) { tft.drawLine(x0, y0, x1, y1); } |
|
void rect(int x0, int y0, int x1, int y1) { tft.drawRect(x0, y0, x1, y1); } |
|
void frect(int x0, int y0, int x1, int y1) { tft.fillRect(x0, y0, x1, y1); } |
|
void circle(int x0, int y0, int r) { tft.drawCircle(x0, y0, r); } |
|
void fcircle(int x0, int y0, int r) { tft.fillCircle(x0, y0, r); } |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(ARDUINOVGA) && defined(ARDUINO_TTGO_T7_V14_Mini32) |
|
#ifndef TTGOVGACONTROLLER |
|
#define TTGOVGACONTROLLER VGA16Controller |
|
#endif |
|
#ifndef TTGOVGACOLUMNS |
|
#define TTGOVGACOLUMNS -1 |
|
#endif |
|
#ifndef TTGOVGAROWS |
|
#define TTGOVGAROWS -1 |
|
#endif |
|
#ifndef TTGOVGARESOLUTION |
|
#define TTGOVGARESOLUTION VGA_640x480_60Hz |
|
#endif |
|
fabgl::TTGOVGACONTROLLER VGAController; |
|
static fabgl::Terminal Terminal; |
|
static Canvas cv(&VGAController); |
|
TerminalController tc(&Terminal); |
|
Color vga_graph_pen = Color::BrightWhite; |
|
Color vga_graph_brush = Color::Black; |
|
Color vga_txt_pen = Color::BrightGreen; |
|
Color vga_txt_background = Color::Black; |
|
fabgl::SoundGenerator soundGenerator; |
|
|
|
|
|
void vgabegin() { |
|
VGAController.begin(GPIO_NUM_22, GPIO_NUM_21, GPIO_NUM_19, GPIO_NUM_18, GPIO_NUM_5, GPIO_NUM_4, GPIO_NUM_23, GPIO_NUM_15); |
|
VGAController.setResolution(TTGOVGARESOLUTION); |
|
Terminal.begin(&VGAController, TTGOVGACOLUMNS, TTGOVGAROWS); |
|
Terminal.setBackgroundColor(vga_txt_background); |
|
Terminal.setForegroundColor(vga_txt_pen); |
|
#ifdef TTGOVGAFONT |
|
Terminal.loadFont(&fabgl::TTGOVGAFONT); |
|
#endif |
|
Terminal.connectLocally(); |
|
Terminal.clear(); |
|
Terminal.enableCursor(1); |
|
Terminal.setTerminalType(TermType::VT52); |
|
} |
|
|
|
|
|
void vgaend() {} |
|
|
|
|
|
int vgastat(uint8_t c) {return 0; } |
|
|
|
|
|
void vgascale(int* x, int* y) { |
|
|
|
} |
|
|
|
void rgbcolor(uint8_t r, uint8_t g, uint8_t b) { |
|
short vga; |
|
if (r>191 || g>191 || b>191) vga=8; else vga=0; |
|
vga=vga+r/128+g/128*2+b/128*4; |
|
vga_graph_pen=fabgl::Color(vga); |
|
} |
|
|
|
void vgacolor(uint8_t c) { vga_graph_pen = fabgl::Color(c%16); } |
|
void plot(int x, int y) { |
|
vgascale(&x, &y); |
|
cv.setPenColor(vga_graph_pen); |
|
cv.setPixel(x,y); |
|
cv.setPenColor(vga_txt_pen); |
|
} |
|
|
|
void line(int x0, int y0, int x1, int y1) { |
|
vgascale(&x0, &y0); |
|
vgascale(&x1, &y1); |
|
cv.setPenColor(vga_graph_pen); |
|
cv.setPenWidth(1); |
|
cv.drawLine(x0, y0, x1, y1); |
|
cv.setPenColor(vga_txt_pen); |
|
} |
|
|
|
void rect(int x0, int y0, int x1, int y1) { |
|
vgascale(&x0, &y0); |
|
vgascale(&x1, &y1); |
|
cv.setPenColor(vga_graph_pen); |
|
cv.setPenWidth(1); |
|
cv.drawRectangle(x0, y0, x1, y1); |
|
cv.setPenColor(vga_txt_pen); |
|
} |
|
|
|
void frect(int x0, int y0, int x1, int y1) { |
|
vgascale(&x0, &y0); |
|
vgascale(&x1, &y1); |
|
cv.setBrushColor(vga_graph_pen); |
|
cv.fillRectangle(x0, y0, x1, y1); |
|
cv.setBrushColor(vga_txt_background); |
|
} |
|
|
|
void circle(int x0, int y0, int r) { |
|
int rx = r; |
|
int ry = r; |
|
vgascale(&x0, &y0); |
|
vgascale(&rx, &ry); |
|
cv.setPenColor(vga_graph_pen); |
|
cv.setPenWidth(1); |
|
cv.drawEllipse(x0, y0, rx, ry); |
|
cv.setPenColor(vga_txt_pen); |
|
} |
|
|
|
void fcircle(int x0, int y0, int r) { |
|
int rx = r; |
|
int ry = r; |
|
vgascale(&x0, &y0); |
|
vgascale(&rx, &ry); |
|
cv.setBrushColor(vga_graph_pen); |
|
cv.fillEllipse(x0, y0, rx, ry); |
|
cv.setBrushColor(vga_txt_background); |
|
} |
|
|
|
void vgawrite(char c){ |
|
switch(c) { |
|
case 12: |
|
Terminal.write(27); Terminal.write('H'); |
|
Terminal.write(27); Terminal.write('J'); |
|
return; |
|
case 10: |
|
Terminal.write(10); Terminal.write(13); |
|
return; |
|
} |
|
Terminal.write(c); |
|
} |
|
#else |
|
void vgabegin(){} |
|
int vgastat(uint8_t c) {return 0; } |
|
void vgawrite(char c){} |
|
void vgaend() {} |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(ARDUINO_TTGO_T7_V14_Mini32) && defined(ARDUINOVGA) |
|
fabgl::PS2Controller PS2Controller; |
|
char fabgllastchar = 0; |
|
#else |
|
#if defined(ARDUINO) && defined(ARDUINOPS2) |
|
PS2Keyboard keyboard; |
|
#else |
|
#if defined(ARDUINO) && defined(ARDUINOUSBKBD) |
|
USBHost usb; |
|
KeyboardController keyboard(usb); |
|
char usbkey=0; |
|
#else |
|
#if defined(ARDUINOZX81KBD) |
|
ZX81Keyboard keyboard; |
|
#else |
|
#if defined(ARDUINOI2CKBD) |
|
|
|
char i2ckey=0; |
|
#else |
|
#if defined(GIGAUSBKBD) |
|
Keyboard keyb; |
|
#endif |
|
#endif |
|
#endif |
|
#endif |
|
#endif |
|
#endif |
|
|
|
|
|
|
|
|
|
#if defined(ARDUINOUSBKBD) |
|
|
|
char usbkeymapUS[] = |
|
{' ', '"', '!', '#', '$', '%', '&', '\'', '(', ')', '*', '+', |
|
',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', |
|
'8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', |
|
'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
|
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', |
|
'\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', |
|
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', |
|
't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', 0, 0}; |
|
|
|
|
|
|
|
char usbkeymapGerman[] = |
|
{' ', '!', '!', '#', '$', '%', '/', '>', ')', '=', '(', '+', |
|
',', '-', '.', '-', '0', '1', '2', '3', '4', '5', '6', '7', |
|
'8', '9', ':', '<', ';', '=', ':', '_', '"', 'A', 'B', 'C', |
|
'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
|
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Z', 'Y', '[', |
|
'#', '+', '&', '?', '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', |
|
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', |
|
't', 'u', 'v', 'w', 'x', 'z', 'y', '{', '\'', '*', 0, 0}; |
|
|
|
|
|
|
|
|
|
void keyPressed() {} |
|
void keyReleased() { |
|
switch (keyboard.getOemKey()) { |
|
case 40: |
|
usbkey=10; |
|
break; |
|
case 42: |
|
case 76: |
|
usbkey=127; |
|
break; |
|
case 41: |
|
usbkey=27; |
|
break; |
|
default: |
|
usbkey=keyboard.getKey(); |
|
#if ARDUINOKBDLANG_GERMAN |
|
if (usbkey>31 && usbkey<128) usbkey=usbkeymapGerman[usbkey-32]; |
|
#else |
|
if (usbkey>31 && usbkey<128) usbkey=usbkeymapUS[usbkey-32]; |
|
#endif |
|
} |
|
} |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#ifdef ZX81KEYBOARD |
|
const byte zx81pins[] = {ZX81PINS}; |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOI2CKBD |
|
char i2ckbdgetkey() { |
|
char c=0; |
|
Wire.requestFrom(I2CKBDADDR, 1); |
|
if (Wire.available()) { |
|
c=Wire.read(); |
|
if (c == -1) { ioer=1; c=0; } |
|
} |
|
return c; |
|
} |
|
#endif |
|
|
|
|
|
void kbdbegin() { |
|
#ifdef PS2KEYBOARD |
|
#ifdef ARDUINOKBDLANG_GERMAN |
|
keyboard.begin(PS2DATAPIN, PS2IRQPIN, PS2Keymap_German); |
|
#else |
|
keyboard.begin(PS2DATAPIN, PS2IRQPIN, PS2Keymap_US); |
|
#endif |
|
#else |
|
#ifdef PS2FABLIB |
|
PS2Controller.begin(PS2Preset::KeyboardPort0); |
|
#ifdef ARDUINOKBDLANG_GERMAN |
|
PS2Controller.keyboard()->setLayout(&fabgl::GermanLayout); |
|
#else |
|
PS2Controller.keyboard()->setLayout(&fabgl::USLayout); |
|
#endif |
|
#else |
|
#ifdef USBKEYBOARD |
|
|
|
#else |
|
#ifdef ZX81KEYBOARD |
|
keyboard.begin(zx81pins); |
|
#else |
|
#ifdef ARDUINOI2CKBD |
|
|
|
delay(500); |
|
#else |
|
#ifdef GIGAUSBKBD |
|
pinMode(PA_15, OUTPUT); |
|
keyb.begin(); |
|
#endif |
|
#endif |
|
#endif |
|
#endif |
|
#endif |
|
#endif |
|
} |
|
|
|
uint8_t kbdstat(uint8_t c) {return 0; } |
|
|
|
uint16_t kbdins(char* b, uint16_t nb) { return consins(b, nb); } |
|
|
|
uint8_t kbdavailable(){ |
|
#ifdef PS2KEYBOARD |
|
return keyboard.available(); |
|
#else |
|
#ifdef PS2FABLIB |
|
if (fabgllastchar) return Terminal.available()+1; else return Terminal.available(); |
|
#else |
|
#ifdef USBKEYBOARD |
|
|
|
if (usbkey) return 1; |
|
|
|
if (usbkey) return 1; else return 0; |
|
#else |
|
#ifdef ZX81KEYBOARD |
|
return keyboard.available(); |
|
#else |
|
#ifdef ARDUINOI2CKBD |
|
|
|
if (i2ckey) { |
|
return 1; |
|
} else { |
|
i2ckey=i2ckbdgetkey(); |
|
if (i2ckey != 0) return 1; |
|
} |
|
#else |
|
#if defined(GIGAUSBKBD) |
|
return keyb.available(); |
|
#endif |
|
#endif |
|
#endif |
|
#endif |
|
#endif |
|
#endif |
|
#ifdef HASKEYPAD |
|
|
|
char c=keypadread(); |
|
if (c != 0) { |
|
bdelay(2); |
|
if (c == keypadread()) return 1; |
|
} |
|
#endif |
|
return 0; |
|
} |
|
|
|
char kbdread() { |
|
char c = 0; |
|
|
|
while(!kbdavailable()) byield(); |
|
#ifdef PS2KEYBOARD |
|
c=keyboard.read(); |
|
#endif |
|
#ifdef PS2FABLIB |
|
if (fabgllastchar) { c=fabgllastchar; fabgllastchar=0; } |
|
else c=Terminal.read(); |
|
#else |
|
#ifdef USBKEYBOARD |
|
|
|
c=usbkey; |
|
usbkey=0; |
|
#else |
|
#ifdef ZX81KEYBOARD |
|
c=keyboard.read(); |
|
#else |
|
#ifdef ARDUINOI2CKBD |
|
|
|
if (i2ckey) { |
|
c=i2ckey; |
|
i2ckey=0; |
|
} else { |
|
|
|
c=i2ckbdgetkey(); |
|
} |
|
#else |
|
#if defined(GIGAUSBKBD) |
|
|
|
auto _key = keyb.read(); |
|
c=keyb.getAscii(_key); |
|
#endif |
|
#endif |
|
#endif |
|
#endif |
|
#endif |
|
#ifdef HASKEYPAD |
|
if (c == 0) { |
|
c=keypadread(); |
|
|
|
if (!kbdrepeat) while(kbdavailable()) byield(); |
|
} |
|
#endif |
|
if (c == 13) c=10; |
|
return c; |
|
} |
|
|
|
char kbdcheckch() { |
|
#ifdef PS2KEYBOARD |
|
|
|
|
|
|
|
#ifdef PS2KEYBOARD_HASPEEK |
|
return keyboard.peek(); |
|
#else |
|
|
|
|
|
|
|
|
|
|
|
if (kbdavailable()) return kbdread(); else return 0; |
|
#endif |
|
#else |
|
#ifdef PS2FABLIB |
|
if (fabgllastchar) return fabgllastchar; |
|
if (kbdavailable()) { fabgllastchar=Terminal.read(); return fabgllastchar; } |
|
#else |
|
#ifdef USBKEYBOARD |
|
return usbkey; |
|
#else |
|
#ifdef ZX81KEYBOARD |
|
return keyboard.lastKey; |
|
#else |
|
#ifdef ARDUINOI2CKBD |
|
if (!i2ckey) { |
|
i2ckey=i2ckbdgetkey(); |
|
} |
|
return i2ckey; |
|
#else |
|
#if defined(GIGAUSBKBD) |
|
auto _key = keyb.peek(); |
|
return keyb.getAscii(_key); |
|
#endif |
|
#endif |
|
#endif |
|
#endif |
|
#endif |
|
#endif |
|
#ifdef HASKEYPAD |
|
return keypadread(); |
|
#endif |
|
return 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
#ifdef DISPLAYDRIVER |
|
|
|
|
|
uint8_t dspmycol = 0; |
|
uint8_t dspmyrow = 0; |
|
|
|
|
|
uint8_t dspesc = 0; |
|
|
|
|
|
uint8_t dspupdatemode = 0; |
|
|
|
|
|
uint8_t dspwrap = 0; |
|
|
|
|
|
uint8_t dspprintmode = 0; |
|
|
|
|
|
uint8_t dspscrollmode = 0; |
|
uint8_t dsp_scroll_rows = 1; |
|
|
|
uint8_t dspstat(uint8_t c) { return 1; } |
|
|
|
void dspsetcursorx(uint8_t c) { |
|
if (c>=0 && c<dsp_columns) dspmycol=c; |
|
dspprintchar(0, dspmycol, dspmyrow); |
|
} |
|
|
|
void dspsetcursory(uint8_t r) { |
|
if (r>=0 && r<dsp_rows) dspmyrow=r; |
|
dspprintchar(0, dspmycol, dspmyrow); |
|
} |
|
|
|
uint8_t dspgetcursorx() { return dspmycol; } |
|
|
|
uint8_t dspgetcursory() { return dspmyrow; } |
|
|
|
uint8_t dspactive() { |
|
return od == ODSP; |
|
} |
|
|
|
|
|
void dspbell() {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void dspsetupdatemode(uint8_t c) { |
|
dspupdatemode=c; |
|
} |
|
|
|
uint8_t dspgetupdatemode() { |
|
return dspupdatemode; |
|
} |
|
|
|
void dspgraphupdate() { |
|
if (dspupdatemode == 0) dspupdate(); |
|
} |
|
|
|
|
|
#ifdef DISPLAYCANSCROLL |
|
|
|
#ifndef DISPLAYHASCOLOR |
|
const uint16_t dspfgvgacolor = 0; |
|
#endif |
|
|
|
dspbuffer_t dspbuffer[dsp_rows][dsp_columns]; |
|
|
|
|
|
|
|
|
|
dspbuffer_t dspget(uint16_t i) { |
|
if (i>=0 && i<=dsp_columns*dsp_rows-1) return dspbuffer[i/dsp_columns][i%dsp_columns]; else return 0; |
|
} |
|
|
|
|
|
dspbuffer_t dspgetrc(uint8_t r, uint8_t c) { return dspbuffer[r][c]; } |
|
dspbuffer_t dspgetc(uint8_t c) { return dspbuffer[dspmyrow][c]; } |
|
|
|
|
|
void dspsetxy(dspbuffer_t ch, uint8_t c, uint8_t r) { |
|
if (r>=0 && c>=0 && r<dsp_rows && c<dsp_columns) { |
|
dspbuffer[r][c]=(dspbuffer_t)ch | (dspfgvgacolor << 8); |
|
if (ch != 0) dspprintchar(ch, c, r); else dspprintchar(' ', c, r); |
|
} |
|
} |
|
|
|
|
|
void dspset(uint16_t i, dspbuffer_t ch) { |
|
uint8_t c=i%dsp_columns; |
|
uint8_t r=i/dsp_columns; |
|
dspsetxy(ch, c, r); |
|
} |
|
|
|
|
|
void dspsetscrollmode(uint8_t c, uint8_t l) { |
|
dspscrollmode = c; |
|
dsp_scroll_rows = l; |
|
} |
|
|
|
|
|
void dspbufferclear() { |
|
uint8_t r,c; |
|
for (r=0; r<dsp_rows; r++) |
|
for (c=0; c<dsp_columns; c++) |
|
dspbuffer[r][c]=0; |
|
dspmyrow=0; |
|
dspmycol=0; |
|
} |
|
|
|
#ifdef DISPLAYHASCOLOR |
|
|
|
const uint16_t charmask = 0x80FF; |
|
#endif |
|
|
|
|
|
void dspscroll(uint8_t scroll_rows, uint8_t scroll_top=0){ |
|
uint8_t r,c; |
|
dspbuffer_t a,b; |
|
|
|
|
|
for (r=scroll_top; r<dsp_rows-scroll_rows; r++) { |
|
for (c=0; c<dsp_columns; c++) { |
|
a=dspbuffer[r][c]; |
|
b=dspbuffer[r+scroll_rows][c]; |
|
if ( a != b ) { |
|
if (b != 0) { |
|
#ifdef DISPLAYHASCOLOR |
|
dspsavepen(); |
|
dspsetfgcolor((b >> 8) & 15); |
|
dspprintchar(b & charmask, c, r); |
|
dsprestorepen(); |
|
#else |
|
dspprintchar(b, c, r); |
|
#endif |
|
} else { |
|
dspprintchar(' ', c, r); |
|
} |
|
} |
|
dspbuffer[r][c]=b; |
|
} |
|
} |
|
|
|
|
|
for (r=dsp_rows-scroll_rows; r<dsp_rows; r++) { |
|
for (c=0; c<dsp_columns; c++) { |
|
if (dspbuffer[r][c] != 0 && ( dspbuffer[r][c]) != 32) dspprintchar(' ', c, r); |
|
dspbuffer[r][c]=0; |
|
} |
|
} |
|
|
|
|
|
dspmycol=0; |
|
dspmyrow=dsp_rows-scroll_rows; |
|
} |
|
|
|
|
|
void dspreversescroll(uint8_t line){ |
|
uint8_t r,c; |
|
dspbuffer_t a,b; |
|
|
|
|
|
for (r=dsp_rows; r>line; r--) { |
|
for (c=0; c<dsp_columns; c++) { |
|
a=dspbuffer[r][c]; |
|
b=dspbuffer[r-1][c]; |
|
if ( a != b ) { |
|
if (b != 0) { |
|
#ifdef DISPLAYHASCOLOR |
|
dspsavepen(); |
|
dspsetfgcolor((b >> 8) & 15); |
|
dspprintchar(b & charmask, c, r); |
|
dsprestorepen(); |
|
#else |
|
dspprintchar(b, c, r); |
|
#endif |
|
} else { |
|
dspprintchar(' ', c, r); |
|
} |
|
} |
|
dspbuffer[r][c]=b; |
|
} |
|
} |
|
|
|
|
|
for (c=0; c<dsp_columns; c++) { |
|
if (dspbuffer[line][c] != 0 && dspbuffer[line][c] != 32) dspprintchar(' ', c, r); |
|
dspbuffer[line][c]=0; |
|
} |
|
|
|
|
|
dspmyrow=line; |
|
} |
|
|
|
|
|
char dspwaitonscroll() { |
|
char c; |
|
|
|
if ( dspscrollmode == 1 ) { |
|
if (dspmyrow == dsp_rows-1) { |
|
c=inch(); |
|
if (c == ' ') dspwrite(12); |
|
return c; |
|
} |
|
} |
|
return 0; |
|
} |
|
#else |
|
|
|
|
|
|
|
|
|
dspbuffer_t dspget(uint16_t i) { return 0; } |
|
dspbuffer_t dspgetrc(uint8_t r, uint8_t c) { return 0; } |
|
dspbuffer_t dspgetc(uint8_t c) { return 0; } |
|
|
|
void dspsetxy(dspbuffer_t ch, uint8_t c, uint8_t r) { |
|
if (ch != 0) dspprintchar(ch, c, r); else dspprintchar(' ', c, r); |
|
} |
|
|
|
void dspset(uint16_t i, dspbuffer_t ch) { |
|
uint8_t c=i%dsp_columns; |
|
uint8_t r=i/dsp_columns; |
|
dspsetxy(ch, c, r); |
|
} |
|
|
|
|
|
void dspbufferclear() { |
|
dspmyrow=0; |
|
dspmycol=0; |
|
} |
|
|
|
|
|
char dspwaitonscroll() { return 0; } |
|
|
|
|
|
void dspscroll(uint8_t scroll_rows, uint8_t scroll_top=0){ |
|
dspmyrow=dsp_rows-1; |
|
} |
|
|
|
|
|
void dspsetscrollmode(uint8_t c, uint8_t l) {} |
|
void dspreversescroll(uint8_t a){} |
|
#endif |
|
|
|
|
|
void dspwrite(char c){ |
|
int8_t dspmycolt; |
|
|
|
|
|
#ifdef HASVT52 |
|
if (dspesc) { |
|
dspvt52(&c); |
|
} |
|
#endif |
|
|
|
|
|
#ifdef ARDUINOPRT |
|
if (dspprintmode) { |
|
prtwrite(c); |
|
if (sendcr && c == 10) prtwrite(13); |
|
if (dspprintmode == 2) return; |
|
} |
|
#endif |
|
|
|
|
|
switch(c) { |
|
case 0: |
|
return; |
|
case 7: |
|
dspbell(); |
|
return; |
|
case 9: |
|
dspmycolt = dspmycol/8; |
|
if ((dspmycolt+1)*8<dsp_columns-1) dspmycol=(dspmycolt+1)*8; |
|
return; |
|
case 10: |
|
dspmyrow++; |
|
if (dspmyrow >= dsp_rows) dspscroll(dsp_scroll_rows); |
|
dspmycol=0; |
|
if (dspupdatemode == 1) dspupdate(); |
|
return; |
|
case 11: |
|
if (dspmyrow < dsp_rows-1) dspmyrow++; |
|
return; |
|
case 12: |
|
dspbufferclear(); |
|
dspclear(); |
|
return; |
|
case 13: |
|
dspmycol=0; |
|
return; |
|
case 27: |
|
dspesc=1; |
|
return; |
|
case 28: |
|
if (dspmycol > 0) dspmycol--; |
|
return; |
|
case 29: |
|
if (dspmycol < dsp_columns-1) dspmycol++; |
|
return; |
|
case 8: |
|
case 127: |
|
if (dspmycol > 0) { |
|
dspmycol--; |
|
dspsetxy(0, dspmycol, dspmyrow); |
|
} |
|
return; |
|
case 2: |
|
dspmycol=dspmyrow=0; |
|
return; |
|
case 3: |
|
dspupdate(); |
|
return; |
|
default: |
|
if (c<32) return; |
|
break; |
|
} |
|
|
|
dspsetxy(c, dspmycol, dspmyrow); |
|
dspmycol++; |
|
if (dspmycol == dsp_columns) { |
|
if (!dspwrap) { |
|
dspmycol=0; |
|
dspmyrow=(dspmyrow + 1); |
|
} |
|
if (dspmyrow >= dsp_rows) dspscroll(dsp_scroll_rows); |
|
} |
|
if (dspupdatemode == 0) dspupdate(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HASVT52 |
|
|
|
char vt52s = 0; |
|
|
|
|
|
uint8_t vt52graph = 0; |
|
|
|
|
|
uint8_t vt52mycol = 0; |
|
uint8_t vt52myrow = 0; |
|
|
|
|
|
uint8_t vt52tmpr; |
|
uint8_t vt52tmpc; |
|
|
|
|
|
const uint8_t vt52buffersize = 4; |
|
char vt52outbuffer[vt52buffersize] = { 0, 0, 0, 0 }; |
|
uint8_t vt52bi = 0; |
|
uint8_t vt52bj = 0; |
|
|
|
|
|
char vt52read() { |
|
if (vt52bi<=vt52bj) { vt52bi = 0; vt52bj = 0; } |
|
if (vt52bi>vt52bj) return vt52outbuffer[vt52bj++]; |
|
return 0; |
|
} |
|
|
|
|
|
uint8_t vt52avail() { if (vt52bi > vt52bj) return vt52bi-vt52bj; else return 0; } |
|
|
|
|
|
void vt52push(char c) { |
|
if (vt52bi < vt52buffersize) vt52outbuffer[vt52bi++]=c; |
|
} |
|
|
|
|
|
void vt52clear() { |
|
vt52bi=0; |
|
} |
|
|
|
|
|
uint8_t vt52number(char c) { |
|
uint8_t b=c; |
|
if (b>31) return b-32; else return 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(DISPLAYHASGRAPH) || defined(VT52WIRING) |
|
#define VT52HASREGISTERS |
|
|
|
uint16_t vt52regx = 0; |
|
uint16_t vt52regy = 0; |
|
uint8_t vt52regz = 0; |
|
|
|
|
|
uint8_t vt52arg = 0; |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DISPLAYHASGRAPH |
|
|
|
uint16_t vt52graphx = 0; |
|
uint16_t vt52graphy = 0; |
|
|
|
|
|
void vt52graphcommand(uint8_t c) { |
|
switch(c) { |
|
case 's': |
|
vt52graphx=vt52regx; |
|
vt52graphy=vt52regy; |
|
break; |
|
case 'p': |
|
plot(vt52graphx, vt52graphy); |
|
break; |
|
case 'l': |
|
line(vt52graphx, vt52graphy, vt52regx, vt52regy); |
|
break; |
|
case 'L': |
|
line(vt52graphx, vt52graphy, vt52regx, vt52regy); |
|
vt52graphx=vt52regx; |
|
vt52graphy=vt52regy; |
|
break; |
|
case 'r': |
|
rect(vt52graphx, vt52graphy, vt52regx, vt52regy); |
|
break; |
|
case 'c': |
|
circle(vt52graphx, vt52graphy, vt52regx); |
|
break; |
|
case 'R': |
|
frect(vt52graphx, vt52graphy, vt52regx, vt52regy); |
|
break; |
|
case 'C': |
|
fcircle(vt52graphx, vt52graphy, vt52regx); |
|
break; |
|
} |
|
} |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef VT52WIRING |
|
#define VT52HASREGISTERS |
|
void vt52wiringcommand(uint8_t c) { |
|
switch(c) { |
|
case 'p': |
|
pinMode(vt52regz, vt52regx); |
|
break; |
|
case 'l': |
|
digitalWrite(vt52regz, LOW); |
|
break; |
|
case 'h': |
|
digitalWrite(vt52regz, HIGH); |
|
break; |
|
case 'r': |
|
vt52push(digitalRead(vt52regz)+32); |
|
break; |
|
case 'a': |
|
analogWrite(vt52regz, vt52regx); |
|
break; |
|
case 'A': |
|
break; |
|
case 't': |
|
tone(vt52regz, vt52regx, vt52regy); |
|
break; |
|
} |
|
} |
|
#endif |
|
|
|
|
|
|
|
void dspvt52(char* c){ |
|
|
|
|
|
switch (vt52s) { |
|
case 'Y': |
|
if (dspesc == 2) { |
|
dspsetcursory(vt52number(*c)); |
|
dspesc=1; |
|
*c=0; |
|
return; |
|
} |
|
if (dspesc == 1) { |
|
dspsetcursorx(vt52number(*c)); |
|
*c=0; |
|
} |
|
vt52s=0; |
|
break; |
|
case 'b': |
|
dspsetfgcolor(vt52number(*c)); |
|
*c=0; |
|
vt52s=0; |
|
break; |
|
case 'c': |
|
dspsetbgcolor(vt52number(*c)); |
|
*c=0; |
|
vt52s=0; |
|
break; |
|
#ifdef VT52HASREGISTERS |
|
case 'x': |
|
if (dspesc == 2) { |
|
vt52arg=vt52number(*c); |
|
dspesc=1; |
|
*c=0; |
|
return; |
|
} |
|
if (dspesc == 1) { |
|
vt52regx=vt52arg+vt52number(*c)*128; |
|
*c=0; |
|
} |
|
vt52s=0; |
|
break; |
|
case 'y': |
|
if (dspesc == 2) { |
|
vt52arg=vt52number(*c); |
|
dspesc=1; |
|
*c=0; |
|
return; |
|
} |
|
if (dspesc == 1) { |
|
vt52regy=vt52arg+vt52number(*c)*127; |
|
*c=0; |
|
} |
|
vt52s=0; |
|
break; |
|
case 'z': |
|
vt52regz=vt52number(*c); |
|
*c=0; |
|
vt52s=0; |
|
break; |
|
#endif |
|
#ifdef DISPLAYHASGRAPH |
|
case 'g': |
|
vt52graphcommand(*c); |
|
*c=0; |
|
vt52s=0; |
|
break; |
|
#endif |
|
#ifdef VT52WIRING |
|
case 'a': |
|
vt52wiringcommand(*c); |
|
*c=0; |
|
vt52s=0; |
|
break; |
|
#endif |
|
} |
|
|
|
|
|
|
|
switch (*c) { |
|
case 'v': |
|
dspwrap=0; |
|
break; |
|
case 'w': |
|
dspwrap=1; |
|
break; |
|
case '^': |
|
dspprintmode=1; |
|
break; |
|
case '_': |
|
dspprintmode=0; |
|
break; |
|
case 'W': |
|
dspprintmode=2; |
|
break; |
|
case 'X': |
|
dspprintmode=0; |
|
break; |
|
case 'V': |
|
#if defined(ARDUINOPRT) && defined(DISPLAYCANSCROLL) |
|
for (uint8_t i=0; i<dsp_columns; i++) prtwrite(dspgetc(i)); |
|
#endif |
|
break; |
|
case ']': |
|
#if defined(ARDUINOPRT) && defined(DISPLAYCANSCROLL) |
|
for (uint8_t j=0; j<dsp_rows; j++) |
|
for (uint8_t i=0; i<dsp_columns; i++) prtwrite(dspgetrc(j, i)); |
|
#endif |
|
break; |
|
case 'F': |
|
vt52graph=1; |
|
break; |
|
case 'G': |
|
vt52graph=0; |
|
break; |
|
case 'Z': |
|
vt52clear(); |
|
vt52push(27); |
|
vt52push('/'); |
|
#ifndef ARDUINOPRT |
|
vt52push('K'); |
|
#else |
|
vt52push('L'); |
|
#endif |
|
break; |
|
case '=': |
|
case '>': |
|
break; |
|
case 'b': |
|
case 'c': |
|
vt52s=*c; |
|
dspesc=1; |
|
*c=0; |
|
return; |
|
case 'e': |
|
dspsetcursor(1); |
|
break; |
|
case 'f': |
|
dspsetcursor(0); |
|
break; |
|
case 'p': |
|
dspsetreverse(1); |
|
break; |
|
case 'q': |
|
dspsetreverse(0); |
|
break; |
|
case 'A': |
|
if (dspmyrow>0) dspmyrow--; |
|
break; |
|
case 'B': |
|
if (dspmyrow < dsp_rows-1) dspmyrow++; |
|
break; |
|
case 'C': |
|
if (dspmycol < dsp_columns-1) dspmycol++; |
|
break; |
|
case 'D': |
|
if (dspmycol>0) dspmycol--; |
|
break; |
|
case 'E': |
|
dspbufferclear(); |
|
dspclear(); |
|
break; |
|
case 'H': |
|
dspmyrow=dspmycol=0; |
|
break; |
|
case 'Y': |
|
vt52s='Y'; |
|
dspesc=2; |
|
*c=0; |
|
return; |
|
case 'J': |
|
for (int i=dspmycol+dsp_columns*dspmyrow; i<dsp_columns*dsp_rows; i++) dspset(i, 0); |
|
break; |
|
case 'd': |
|
for (int i=0; i<dspmycol+dsp_columns*dspmyrow; i++) dspset(i, 0); |
|
break; |
|
case 'K': |
|
for (uint8_t i=dspmycol; i<dsp_columns; i++) dspsetxy(0, i, dspmyrow); |
|
break; |
|
case 'l': |
|
for (uint8_t i=0; i<dsp_columns; i++) dspsetxy(0, i, dspmyrow); |
|
break; |
|
case 'o': |
|
for (uint8_t i=0; i<=dspmycol; i++) dspsetxy(0, i, dspmyrow); |
|
break; |
|
case 'k': |
|
dspmycol=vt52mycol; |
|
dspmyrow=vt52myrow; |
|
break; |
|
case 'j': |
|
vt52mycol=dspmycol; |
|
vt52myrow=dspmyrow; |
|
break; |
|
case 'I': |
|
if (dspmyrow>0) dspmyrow--; else dspreversescroll(0); |
|
break; |
|
case 'L': |
|
dspreversescroll(dspmyrow); |
|
break; |
|
case 'M': |
|
vt52tmpr = dspmyrow; |
|
vt52tmpc = dspmycol; |
|
dspscroll(1, dspmyrow); |
|
dspmyrow=vt52tmpr; |
|
dspmycol=vt52tmpc; |
|
break; |
|
#ifdef VT52HASREGISTERS |
|
case 'x': |
|
case 'y': |
|
vt52s=*c; |
|
dspesc=2; |
|
*c=0; |
|
return; |
|
case 'z': |
|
vt52s=*c; |
|
dspesc=1; |
|
*c=0; |
|
return; |
|
break; |
|
#endif |
|
#ifdef DISPLAYHASGRAPH |
|
case 'g': |
|
vt52s=*c; |
|
dspesc=1; |
|
*c=0; |
|
return; |
|
break; |
|
#endif |
|
#ifdef VT52WIRING |
|
case 'a': |
|
vt52s=*c; |
|
dspesc=1; |
|
*c=0; |
|
return; |
|
break; |
|
#endif |
|
} |
|
dspesc=0; |
|
*c=0; |
|
} |
|
#endif |
|
|
|
#else |
|
#ifdef GRAPHDISPLAYDRIVER |
|
|
|
|
|
#undef HASVT52 |
|
#define GBUFFERSIZE 80 |
|
static char gbuffer[GBUFFERSIZE]; |
|
void dspouts(char* s, uint16_t l) { |
|
int i; |
|
for (i=0; i<l-1 && i<GBUFFERSIZE-1; i++) gbuffer[i]=s[i]; |
|
gbuffer[i]=0; |
|
dspprintstring(gbuffer); |
|
} |
|
|
|
void dspwrite(char c) { |
|
switch(c) { |
|
case 0: |
|
return; |
|
case 10: |
|
dspgraphcursor_x=0; |
|
case 11: |
|
dspgraphcursor_y+=dspfontsize; |
|
return; |
|
case 12: |
|
dspclear(); |
|
case 2: |
|
dspgraphcursor_x=0; |
|
dspgraphcursor_y=dspfontsize; |
|
return; |
|
case 13: |
|
dspgraphcursor_x=0; |
|
return; |
|
default: |
|
if (c<32) return; |
|
break; |
|
} |
|
dspouts(&c, 1); |
|
} |
|
int dspstat(char c) { return 1; } |
|
int dspgetcursorx() { return dspgraphcursor_x; } |
|
int dspgetcursory() { return dspgraphcursor_y; } |
|
void dspsetcursorx(int v) { dspgraphcursor_x = v; } |
|
void dspsetcursory(int v) { dspgraphcursor_y = v; } |
|
char dspwaitonscroll() { return 0; }; |
|
uint8_t dspactive() {return 1; } |
|
void dspsetupdatemode(uint8_t c) {} |
|
#else |
|
|
|
const int dsp_rows=0; |
|
const int dsp_columns=0; |
|
void dspsetupdatemode(uint8_t c) {} |
|
void dspwrite(char c){}; |
|
void dspbegin() {}; |
|
uint8_t dspstat(uint8_t c) {return 0; } |
|
char dspwaitonscroll() { return 0; }; |
|
uint8_t dspactive() {return 0; } |
|
void dspsetscrollmode(uint8_t c, uint8_t l) {} |
|
void dspscroll(uint8_t a, uint8_t b){} |
|
char vt52read() { return 0; } |
|
uint8_t vt52avail() { return 0; } |
|
#endif |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(ARDUINORTC) |
|
|
|
|
|
|
|
|
|
|
|
|
|
void rtcbegin() {} |
|
|
|
|
|
uint16_t rtcget(uint8_t i) { |
|
|
|
|
|
wirestart(RTCI2CADDR, 0); |
|
wirewritebyte(i); |
|
wirestop(); |
|
|
|
|
|
wirestart(RTCI2CADDR, 1); |
|
uint8_t v=wirereadbyte(); |
|
|
|
switch (i) { |
|
case 0: |
|
case 1: |
|
return (v & 0b00001111)+((v >> 4) & 0b00000111) * 10; |
|
case 2: |
|
return (v & 0b00001111)+((v >> 4) & 0b00000011) * 10 ; |
|
case 3: |
|
return (v & 0b00001111); |
|
case 4: |
|
return (v & 0b00001111) + ((v >> 4) & 0b00000011) * 10; |
|
case 5: |
|
return (v & 0b00001111) + ((v >> 4) & 0b00000001) * 10; |
|
case 6: |
|
return (v & 0b00001111) + (v >> 4) * 10; |
|
default: |
|
return v; |
|
} |
|
} |
|
|
|
|
|
void rtcset(uint8_t i, uint16_t v) { |
|
uint8_t b; |
|
|
|
|
|
if (i<7) b=(v%10 + ((v/10) << 4)); else b=v; |
|
|
|
switch (i) { |
|
case 0: |
|
case 1: |
|
b = b & 0b01111111; |
|
break; |
|
case 2: |
|
case 4: |
|
b = b & 0b00111111; |
|
break; |
|
case 3: |
|
b = b & 0b00000111; |
|
break; |
|
case 5: |
|
b = b & 0b00011111; |
|
} |
|
|
|
|
|
wirestart(RTCI2CADDR, 0); |
|
wirewritebyte(i); |
|
wirewritebyte(b); |
|
wirestop(); |
|
|
|
} |
|
#elif defined(HASBUILTINRTC) |
|
|
|
|
|
|
|
|
|
|
|
#ifndef ARDUINO_ARCH_RENESAS |
|
|
|
void rtcbegin() { |
|
rtc.begin(); |
|
} |
|
|
|
|
|
uint16_t rtcget(uint8_t i) { |
|
switch (i) { |
|
case 0: |
|
return rtc.getSeconds(); |
|
case 1: |
|
return rtc.getMinutes(); |
|
case 2: |
|
return rtc.getHours(); |
|
case 3: |
|
|
|
return 0; |
|
case 4: |
|
return rtc.getDay(); |
|
case 5: |
|
return rtc.getMonth(); |
|
case 6: |
|
return rtc.getYear(); |
|
default: |
|
return 0; |
|
} |
|
} |
|
|
|
|
|
void rtcset(uint8_t i, uint16_t v) { |
|
switch (i) { |
|
case 0: |
|
rtc.setSeconds(v); |
|
break; |
|
case 1: |
|
rtc.setMinutes(v); |
|
break; |
|
case 2: |
|
rtc.setHours(v); |
|
break; |
|
case 3: |
|
|
|
break; |
|
case 4: |
|
rtc.setDay(v); |
|
break; |
|
case 5: |
|
rtc.setMonth(v); |
|
break; |
|
case 6: |
|
rtc.setYear(v); |
|
break; |
|
default: |
|
return; |
|
} |
|
} |
|
#else |
|
|
|
|
|
void rtcbegin() { |
|
RTC.begin(); |
|
} |
|
|
|
|
|
uint16_t rtcget(uint8_t i) { |
|
RTC.getTime(rtc); |
|
switch (i) { |
|
case 0: |
|
return rtc.getSeconds(); |
|
case 1: |
|
return rtc.getMinutes(); |
|
case 2: |
|
return rtc.getHour(); |
|
case 3: |
|
return static_cast<int>(rtc.getDayOfWeek()); |
|
case 4: |
|
return rtc.getDayOfMonth(); |
|
case 5: |
|
return Month2int(rtc.getMonth()); |
|
case 6: |
|
return rtc.getYear(); |
|
default: |
|
return 0; |
|
} |
|
} |
|
|
|
|
|
void rtcset(uint8_t i, uint16_t v) { |
|
RTC.getTime(rtc); |
|
switch (i) { |
|
case 0: |
|
rtc.setSecond(v); |
|
break; |
|
case 1: |
|
rtc.setMinute(v); |
|
break; |
|
case 2: |
|
rtc.setHour(v); |
|
break; |
|
case 3: |
|
rtc.setDayOfWeek(static_cast<DayOfWeek>(v)); |
|
break; |
|
case 4: |
|
rtc.setDayOfMonth(v); |
|
break; |
|
case 5: |
|
rtc.setMonthOfYear(static_cast<Month>(v-1)); |
|
break; |
|
case 6: |
|
rtc.setYear(v); |
|
break; |
|
default: |
|
return; |
|
} |
|
RTC.setTime(rtc); |
|
} |
|
#endif |
|
#elif defined(ARDUINORTCEMULATION) |
|
|
|
|
|
|
|
|
|
|
|
|
|
void rtcbegin() {} |
|
|
|
|
|
long rtcutime = 0; |
|
|
|
|
|
long rtcutimeoffset = 0; |
|
|
|
|
|
struct { uint8_t second; uint8_t minute; uint8_t hour; uint8_t weekday; uint8_t day; uint8_t month; uint16_t year; } |
|
rtctime = { 0, 0, 0, 4, 1, 1, 1970 }; |
|
|
|
|
|
const int rtcmonthdays[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; |
|
|
|
|
|
|
|
void rtctimetoutime() { |
|
int leapyear = ((rtctime.year-1)-1968)/4 - ((rtctime.year-1)-1900)/100 + ((rtctime.year-1)-1600)/400; |
|
long epochdays = (rtctime.year-1970)*365 + leapyear + rtcmonthdays[rtctime.month-1] + rtctime.day - 1; |
|
if ((rtctime.month > 2) && (rtctime.year%4 == 0 && (rtctime.year%100 != 0 || rtctime.year%400 == 0))) epochdays+=1; |
|
rtcutime = rtctime.second + 60*(rtctime.minute + 60*(rtctime.hour + 24*epochdays)); |
|
} |
|
|
|
|
|
void rtcutimetotime() { |
|
const unsigned long int secondsperday = 86400ul; |
|
const unsigned long int daysperyear = 365ul; |
|
const unsigned long int daysinfoury = 1461ul; |
|
const unsigned long int daysinhundredy = 36524ul; |
|
const unsigned long int daysinfourhundredy = 146097ul; |
|
const unsigned long int daynumberzero = 719468ul; |
|
|
|
unsigned long int daynumber = daynumberzero + rtcutime/secondsperday; |
|
unsigned long int secondssincemidnight = rtcutime%secondsperday; |
|
unsigned long int temp; |
|
|
|
|
|
rtctime.weekday = daynumber % 7; |
|
|
|
|
|
temp = 4 * (daynumber + daysinhundredy + 1) / daysinfourhundredy - 1; |
|
rtctime.year = 100 * temp; |
|
daynumber -= daysinhundredy * temp + temp / 4; |
|
|
|
|
|
temp = 4 * (daynumber + daysperyear + 1) / daysinfoury - 1; |
|
rtctime.year += temp; |
|
daynumber -= daysperyear * temp + temp / 4; |
|
|
|
|
|
rtctime.month = (5 * daynumber + 2) / 153; |
|
rtctime.day = daynumber - (rtctime.month * 153 + 2) / 5 + 1; |
|
|
|
|
|
rtctime.month += 3; |
|
if (rtctime.month > 12) { |
|
rtctime.month -= 12; |
|
rtctime.year++; |
|
} |
|
|
|
|
|
rtctime.hour = secondssincemidnight / 3600; |
|
rtctime.minute = secondssincemidnight % 3600 / 60; |
|
rtctime.second = secondssincemidnight % 60; |
|
} |
|
|
|
|
|
uint16_t rtcget(uint8_t i) { |
|
|
|
rtcutime = millis()/1000 + rtcutimeoffset; |
|
|
|
rtcutimetotime(); |
|
|
|
switch (i) { |
|
case 0: |
|
return rtctime.second; |
|
case 1: |
|
return rtctime.minute; |
|
case 2: |
|
return rtctime.hour; |
|
case 3: |
|
return rtctime.weekday; |
|
case 4: |
|
return rtctime.day; |
|
case 5: |
|
return rtctime.month; |
|
case 6: |
|
return rtctime.year; |
|
default: |
|
return 0; |
|
} |
|
} |
|
|
|
|
|
void rtcset(uint8_t i, uint16_t v) { |
|
|
|
rtcutime = millis()/1000 + rtcutimeoffset; |
|
|
|
|
|
rtcutimetotime(); |
|
|
|
|
|
switch (i) { |
|
case 0: |
|
if (v>=0 && v<60) rtctime.second=v; |
|
break; |
|
case 1: |
|
if (v>=0 && v<60) rtctime.minute=v; |
|
break; |
|
case 2: |
|
if (v>=0 && v<24) rtctime.hour=v; |
|
break; |
|
case 3: |
|
if (v>=0 && v<7) rtctime.weekday=v; |
|
break; |
|
case 4: |
|
if (v>0 && v<32) rtctime.day=v; |
|
break; |
|
case 5: |
|
if (v>0 && v<13) rtctime.month=v; |
|
break; |
|
case 6: |
|
if (v>=1970 && v<2100) rtctime.year=v; |
|
break; |
|
default: |
|
return; |
|
} |
|
|
|
|
|
rtctimetoutime(); |
|
|
|
|
|
rtcutimeoffset = rtcutime - millis()/1000; |
|
} |
|
#elif defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_MBED_GIGA) |
|
|
|
|
|
|
|
|
|
|
|
|
|
void rtcbegin() {} |
|
|
|
|
|
uint16_t rtcget(uint8_t i) { |
|
struct tm rtctime; |
|
time_t now; |
|
time(&now); |
|
localtime_r(&now, &rtctime); |
|
|
|
switch (i) { |
|
case 0: |
|
return rtctime.tm_sec; |
|
case 1: |
|
return rtctime.tm_min; |
|
case 2: |
|
return rtctime.tm_hour; |
|
case 3: |
|
return rtctime.tm_wday; |
|
case 4: |
|
return rtctime.tm_mday; |
|
case 5: |
|
return rtctime.tm_mon+1; |
|
case 6: |
|
if (rtctime.tm_year > 100) return rtctime.tm_year-100; else return rtctime.tm_year; |
|
default: |
|
return 0; |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
|
|
void rtcset(uint8_t i, uint16_t v) { |
|
struct tm rtctime; |
|
struct timeval tv; |
|
|
|
|
|
time_t now; |
|
time(&now); |
|
localtime_r(&now, &rtctime); |
|
|
|
|
|
switch (i) { |
|
case 0: |
|
rtctime.tm_sec = v%60; |
|
break; |
|
case 1: |
|
rtctime.tm_min = v%60; |
|
break; |
|
case 2: |
|
rtctime.tm_hour = v%24; |
|
break; |
|
case 3: |
|
rtctime.tm_wday = v%7; |
|
break; |
|
case 4: |
|
rtctime.tm_mday = v; |
|
break; |
|
case 5: |
|
rtctime.tm_mon = v-1; |
|
break; |
|
case 6: |
|
if (v > 1900) v=v-1900; |
|
if (v < 50) v=v+100; |
|
rtctime.tm_year = v; |
|
break; |
|
} |
|
|
|
|
|
time_t epocht = mktime(&rtctime); |
|
if (epocht > 2082758399){ |
|
tv.tv_sec = epocht - 2082758399; |
|
} else { |
|
tv.tv_sec = epocht; |
|
} |
|
tv.tv_usec = 0; |
|
settimeofday(&tv, NULL); |
|
} |
|
#else |
|
|
|
void rtcbegin() {} |
|
uint16_t rtcget(uint8_t i) { return 0; } |
|
void rtcset(uint8_t i, uint16_t v) { } |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOEFS |
|
#undef ARDUINOI2CEEPROM |
|
#ifndef EFSEEPROMADDR |
|
#define EFSEEPROMADDR 0x50 |
|
#endif |
|
#ifdef EFSEEPROMSIZE |
|
EepromFS EFS(EFSEEPROMADDR, EFSEEPROMSIZE); |
|
#else |
|
EepromFS EFS(EFSEEPROMADDR); |
|
#endif |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(ARDUINOI2CEEPROM) && defined(ARDUINOI2CEEPROM_BUFFERED) |
|
#ifndef I2CEEPROMADDR |
|
#define I2CEEPROMADDR 0x50 |
|
#endif |
|
#ifdef I2CEEPROMSIZE |
|
EepromFS EFSRAW(I2CEEPROMADDR, I2CEEPROMSIZE); |
|
#else |
|
EepromFS EFSRAW(I2CEEPROMADDR); |
|
#endif |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOMQTT |
|
#include "wifisettings.h" |
|
#ifdef ARDUINOETH |
|
EthernetClient bethclient; |
|
PubSubClient bmqtt(bethclient); |
|
#else |
|
WiFiClient bwifi; |
|
PubSubClient bmqtt(bwifi); |
|
#endif |
|
|
|
|
|
char mqtt_otopic[MQTTLENGTH]; |
|
char mqtt_itopic[MQTTLENGTH]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
volatile char mqtt_buffer[MQTTBLENGTH]; |
|
volatile uint16_t mqtt_messagelength; |
|
volatile char mqtt_obuffer[MQTTBLENGTH]; |
|
volatile uint16_t mqtt_charsforsend; |
|
|
|
|
|
|
|
char mqttname[MQTTNAMELENGTH] = "iotbasicxxx"; |
|
void mqttsetname() { |
|
uint32_t m = millis(); |
|
mqttname[8]=(char)(65+m%26); |
|
m=m/26; |
|
mqttname[9]=(char)(65+m%26); |
|
m=m/26; |
|
mqttname[10]=(char)(65+m%26); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void netbegin() { |
|
#ifdef ARDUINOETH |
|
#ifdef ETHPIN |
|
Ethernet.init(ETHPIN); |
|
#endif |
|
Ethernet.begin(mac); |
|
#else |
|
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) |
|
WiFi.mode(WIFI_STA); |
|
WiFi.begin(ssid, password); |
|
WiFi.setAutoReconnect(1); |
|
#endif |
|
#if defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_UNOR4_WIFI) |
|
WiFi.begin(ssid, password); |
|
#endif |
|
#if defined(ARDUINO_ARCH_MBED_GIGA) |
|
int counter=0; |
|
int status=WL_IDLE_STATUS; |
|
while (status != WL_CONNECTED && counter++ < 10) { |
|
status=WiFi.begin(ssid, password); |
|
bdelay(500); |
|
} |
|
#endif |
|
#endif |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
void netstop() { |
|
#ifdef ARDUINOETH |
|
|
|
#else |
|
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) |
|
WiFi.mode(WIFI_OFF); |
|
#endif |
|
#if defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_UNOR4_WIFI) || defined(ARDUINO_ARCH_MBED_GIGA) |
|
WiFi.end(); |
|
#endif |
|
#endif |
|
} |
|
|
|
|
|
|
|
|
|
|
|
void netreconnect() { |
|
#ifdef ARDUINOETH |
|
|
|
#else |
|
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) |
|
WiFi.reconnect(); |
|
bdelay(1000); |
|
#elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_UNOR4_WIFI) || defined(ARDUINO_ARCH_MBED_GIGA) |
|
|
|
netstop(); |
|
netbegin(); |
|
#endif |
|
#endif |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t netconnected() { |
|
#ifdef ARDUINOETH |
|
return bethclient.connected(); |
|
#else |
|
return WiFi.status() == WL_CONNECTED; |
|
#endif |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void mqttcallback(char* t, byte* p, unsigned int l) { |
|
short i; |
|
mqtt_messagelength=l; |
|
for(i=0; i<l; i++) mqtt_buffer[i]=(char)p[i]; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
void mqttbegin() { |
|
bmqtt.setServer(mqtt_server, mqtt_port); |
|
bmqtt.setCallback(mqttcallback); |
|
*mqtt_itopic=0; |
|
*mqtt_otopic=0; |
|
mqtt_charsforsend=0; |
|
} |
|
|
|
|
|
uint8_t mqttstat(uint8_t c) { |
|
#if defined(ARDUINOMQTT) |
|
if (c == 0) return 1; |
|
if (c == 1) return mqttstate(); |
|
#endif |
|
return 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t mqttreconnect() { |
|
uint16_t timer=10; |
|
uint8_t reconnect=0; |
|
|
|
|
|
if (bmqtt.connected()) return 1; |
|
|
|
|
|
if (!netconnected()) { netreconnect(); bdelay(5000); } |
|
if (!netconnected()) return 0; |
|
|
|
|
|
mqttsetname(); |
|
|
|
|
|
while (!bmqtt.connected() && timer < 400) { |
|
if (*mqtt_user == 0) { |
|
bmqtt.connect(mqttname); |
|
} else { |
|
bmqtt.connect(mqttname, mqtt_user, mqtt_passwd); |
|
} |
|
bmqtt.connect(mqttname); |
|
bdelay(timer); |
|
timer=timer*2; |
|
reconnect=1; |
|
} |
|
|
|
|
|
if (*mqtt_itopic && bmqtt.connected() && reconnect) bmqtt.subscribe(mqtt_itopic); |
|
|
|
return bmqtt.connected(); |
|
} |
|
|
|
|
|
uint8_t mqttstate() { |
|
return bmqtt.state(); |
|
} |
|
|
|
|
|
void mqttsubscribe(const char* t) { |
|
uint16_t i; |
|
|
|
for (i=0; i<MQTTLENGTH; i++) { |
|
if ((mqtt_itopic[i]=t[i]) == 0 ) break; |
|
} |
|
if (!mqttreconnect()) {ioer=1; return;}; |
|
if (!bmqtt.subscribe(mqtt_itopic)) ioer=1; |
|
} |
|
|
|
void mqttunsubscribe() { |
|
if (!mqttreconnect()) {ioer=1; return;}; |
|
if (!bmqtt.unsubscribe(mqtt_itopic)) ioer=1; |
|
*mqtt_itopic=0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
void mqttsettopic(const char *t) { |
|
uint16_t i; |
|
|
|
for (i=0; i<MQTTLENGTH; i++) { |
|
if ((mqtt_otopic[i]=t[i]) == 0 ) break; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
void mqttouts(const char *m, uint16_t l) { |
|
uint16_t i=0; |
|
|
|
while(mqtt_charsforsend < MQTTBLENGTH && i<l) mqtt_obuffer[mqtt_charsforsend++]=m[i++]; |
|
if (mqtt_obuffer[mqtt_charsforsend-1] == '\n' || mqtt_charsforsend > MQTTBLENGTH) { |
|
if (!mqttreconnect()) {ioer=1; return;}; |
|
if (!bmqtt.publish(mqtt_otopic, (uint8_t*) mqtt_obuffer, (unsigned int) mqtt_charsforsend-1, false)) ioer=1; |
|
mqtt_charsforsend=0; |
|
} |
|
} |
|
|
|
|
|
void mqttwrite(const char c) { mqttouts(&c, 1); } |
|
|
|
|
|
uint16_t mqttavailable() { |
|
return mqtt_messagelength; |
|
} |
|
|
|
|
|
char mqttcheckch() { |
|
if (mqtt_messagelength>0) return mqtt_buffer[0]; else return 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
uint16_t mqttins(char *b, uint16_t nb) { |
|
uint16_t z; |
|
|
|
for (z=0; z<nb && z<mqtt_messagelength; z++) b[z+1]=mqtt_buffer[z]; |
|
b[0]=z; |
|
mqtt_messagelength=0; |
|
*mqtt_buffer=0; |
|
return z; |
|
} |
|
|
|
|
|
char mqttread() { |
|
char ch=0; |
|
uint16_t i; |
|
|
|
if (mqtt_messagelength>0) { |
|
ch=mqtt_buffer[0]; |
|
for (i=0; i<mqtt_messagelength-1; i++) mqtt_buffer[i]=mqtt_buffer[i+1]; |
|
mqtt_messagelength--; |
|
} |
|
|
|
return ch; |
|
} |
|
|
|
#else |
|
void netbegin() {} |
|
uint8_t netconnected() { return 0; } |
|
void mqttbegin() {} |
|
uint8_t mqttstate() {return 0;} |
|
void mqttsubscribe(char *t) {} |
|
void mqttsettopic(char *t) {} |
|
void mqttouts(char *m, uint16_t l) {} |
|
uint16_t mqttins(char *b, uint16_t nb) { return 0; }; |
|
char mqttread() {return 0;}; |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
void ebegin(){ |
|
|
|
#if (defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) ) && defined(ARDUINOEEPROM) |
|
EEPROM.begin(EEPROMSIZE); |
|
#endif |
|
#if (defined(ARDUINO_ARCH_STM32)) && defined(ARDUINOEEPROM) |
|
eeprom_buffer_fill(); |
|
#endif |
|
#if (defined(ARDUINO_ARCH_XMC)) && defined(ARDUINOEEPROM) |
|
EEPROM.begin(); |
|
#endif |
|
#if (defined(ARDUINO_ARCH_SAMD)) && defined(ARDUINOEEPROM) |
|
|
|
#endif |
|
#if (defined(ARDUINO_ARCH_MBED_NANO)) && defined(ARDUINOEEPROM) |
|
EEPROM.begin(); |
|
#endif |
|
|
|
#if defined(ARDUINOI2CEEPROM) && !defined(ARDUINOI2CEEPROM_BUFFERED) |
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef I2CEEPROMSIZE |
|
int c4=eread(4094); |
|
int c32=eread(32766); |
|
eupdate(4094, 42); |
|
eupdate(32766, 84); |
|
if (ioer !=0 ) return; |
|
if (eread(32766) == 84 && eread(4094) == 42) i2ceepromsize = 32767; else i2ceepromsize = 4096; |
|
eupdate(4094, c4); |
|
eupdate(32766, c32); |
|
#else |
|
i2ceepromsize = I2CEEPROMSIZE; |
|
#endif |
|
#endif |
|
|
|
#if defined(ARDUINOI2CEEPROM) && defined(ARDUINOI2CEEPROM_BUFFERED) |
|
EFSRAW.begin(); |
|
#ifndef I2CEEPROMSIZE |
|
i2ceepromsize = EFSRAW.esize(); |
|
#else |
|
i2ceepromsize = I2CEEPROMSIZE; |
|
#endif |
|
#endif |
|
} |
|
|
|
void eflush(){ |
|
|
|
#if (defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || \ |
|
defined(ARDUINO_ARCH_XMC) || defined(ARDUINO_ARCH_SAMD)) \ |
|
&& defined(ARDUINOEEPROM) |
|
EEPROM.commit(); |
|
#endif |
|
#if (defined(ARDUINO_ARCH_STM32)) && defined(ARDUINOEEPROM) |
|
eeprom_buffer_flush(); |
|
#endif |
|
|
|
#if defined(ARDUINOI2CEEPROM) && defined(ARDUINOI2CEEPROM_BUFFERED) |
|
EFSRAW.rawflush(); |
|
#endif |
|
} |
|
|
|
#if defined(ARDUINOEEPROM) && !defined(ARDUINOI2CEEPROM) |
|
uint16_t elength() { |
|
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) |
|
return EEPROMSIZE; |
|
#endif |
|
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR) || defined(ARDUINO_ARCH_XMC) || \ |
|
defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_RENESAS) || defined(ARDUINO_ARCH_SAMD) |
|
return EEPROM.length(); |
|
#endif |
|
#ifdef ARDUINO_ARCH_LGT8F |
|
return EEPROM.length(); |
|
#endif |
|
return 0; |
|
} |
|
|
|
void eupdate(uint16_t a, int8_t c) { |
|
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)|| defined(AARDUINO_ARCH_LGT8F) || \ |
|
defined(ARDUINO_ARCH_XMC) || defined(ARDUINO_ARCH_SAMD) |
|
EEPROM.write(a, c); |
|
#else |
|
#if defined(ARDUINO_ARCH_STM32) |
|
eeprom_buffered_write_byte(a, c); |
|
#else |
|
EEPROM.update(a, c); |
|
#endif |
|
#endif |
|
} |
|
|
|
int8_t eread(uint16_t a) { |
|
#ifdef ARDUINO_ARCH_STM32 |
|
return (int8_t) eeprom_buffered_read_byte(a); |
|
#else |
|
return (int8_t) EEPROM.read(a); |
|
#endif |
|
} |
|
#else |
|
#if defined(ARDUINOI2CEEPROM) |
|
uint16_t elength() { |
|
return i2ceepromsize; |
|
} |
|
|
|
void eupdate(uint16_t a, int8_t c) { |
|
#if defined(ARDUINOI2CEEPROM_BUFFERED) |
|
EFSRAW.rawwrite(a, c); |
|
#else |
|
|
|
if (eread(a) != c) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wirestart(I2CEEPROMADDR, 0); |
|
wirewritebyte((int)a/256); |
|
wirewritebyte((int)a%256); |
|
wirewritebyte((int)c); |
|
ioer=wirestop(); |
|
|
|
|
|
bdelay(5); |
|
} |
|
#endif |
|
} |
|
|
|
int8_t eread(uint16_t a) { |
|
#ifdef ARDUINOI2CEEPROM_BUFFERED |
|
return (int8_t) EFSRAW.rawread(a); |
|
#else |
|
|
|
|
|
|
|
wirestart(I2CEEPROMADDR, 0); |
|
wirewritebyte((int)a/256); |
|
wirewritebyte((int)a%256); |
|
ioer=wirestop(); |
|
|
|
|
|
if (ioer == 0) { |
|
wirestart(I2CEEPROMADDR, 1); |
|
return wirereadbyte(); |
|
} else return 0; |
|
|
|
#endif |
|
} |
|
#else |
|
|
|
uint16_t elength() { return 0; } |
|
void eupdate(uint16_t a, int8_t c) { return; } |
|
int8_t eread(uint16_t a) { return 0; } |
|
#endif |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_DUEMILANOVE) || defined(ARDUINO_AVR_NANO) |
|
void portwrite(uint8_t p, int v) { |
|
switch (p) { |
|
case 0: PORTB = v; break; |
|
case 1: PORTC = v; break; |
|
case 2: PORTD = v; break; |
|
default: break; |
|
} |
|
} |
|
int portread(uint8_t p) { |
|
switch (p) { |
|
case 0: return PORTB; break; |
|
case 1: return PORTC; break; |
|
case 2: return PORTD; break; |
|
default: return 0; |
|
} |
|
} |
|
void ddrwrite(uint8_t p, int v) { |
|
switch (p) { |
|
case 0: DDRB = v; break; |
|
case 1: DDRC = v; break; |
|
case 2: DDRD = v; break; |
|
default: break; |
|
} |
|
} |
|
int ddrread(uint8_t p) { |
|
switch (p) { |
|
case 0: return DDRB; break; |
|
case 1: return DDRC; break; |
|
case 2: return DDRD; break; |
|
default: return 0; |
|
} |
|
} |
|
int pinread(uint8_t p) { |
|
switch (p) { |
|
case 0: return PINB; break; |
|
case 1: return PINC; break; |
|
case 2: return PIND; break; |
|
default: return 0; |
|
} |
|
} |
|
#else |
|
void portwrite(uint8_t p, int v) {} |
|
int portread(uint8_t p) { return 0; } |
|
void ddrwrite(uint8_t p, int v) {} |
|
int ddrread(uint8_t p) { return 0; } |
|
int pinread(uint8_t p) { return 0; } |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t aread(uint8_t p) { return analogRead(p); } |
|
uint8_t dread(uint8_t p) { return digitalRead(p); } |
|
void awrite(uint8_t p, uint16_t v){ analogWrite(p, v); } |
|
void dwrite(uint8_t p, uint8_t v){ if (v) digitalWrite(p, HIGH); else digitalWrite(p, LOW); } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void pinm(uint8_t p, uint8_t m){ |
|
if (m == 0) m=INPUT; |
|
else if (m == 1) m=OUTPUT; |
|
pinMode(p,m); |
|
} |
|
|
|
|
|
uint32_t pulsein(uint8_t pin, uint8_t val, uint32_t t) { |
|
return pulseIn(pin, val, t); |
|
} |
|
|
|
|
|
void pulseout(uint16_t unit, uint8_t pin, uint16_t duration, uint16_t val, uint16_t repetition, uint16_t interval) { |
|
|
|
pinMode(pin, OUTPUT); |
|
while (repetition--) { |
|
digitalWrite(pin, val); |
|
delayMicroseconds(duration*unit); |
|
digitalWrite(pin, !val); |
|
delayMicroseconds(interval*unit); |
|
} |
|
} |
|
|
|
|
|
#if defined(BREAKPIN) && defined(INPUT_PULLUP) |
|
void breakpinbegin() { pinm(BREAKPIN, INPUT_PULLUP); } |
|
uint8_t getbreakpin() { return dread(BREAKPIN); } |
|
#else |
|
|
|
void breakpinbegin() {} |
|
uint8_t getbreakpin() { return 1; } |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(ARDUINOTONEEMULATION) |
|
uint8_t tone_enabled; |
|
uint8_t tone_pinstate = 0; |
|
uint32_t tone_intervall; |
|
uint32_t tone_micros; |
|
uint16_t tone_duration; |
|
uint32_t tone_start; |
|
uint8_t tone_pin; |
|
|
|
void playtone(uint8_t pin, uint16_t frequency, uint16_t duration, uint8_t volume) { |
|
|
|
|
|
tone_pin=pin; |
|
pinMode(tone_pin, OUTPUT); |
|
digitalWrite(tone_pin, LOW); |
|
tone_pinstate=0; |
|
|
|
|
|
if (duration == 0) duration=32767; |
|
|
|
|
|
if (frequency == 0) { |
|
tone_enabled=0; |
|
return; |
|
} |
|
|
|
|
|
tone_intervall=1000000/frequency/2; |
|
tone_micros=micros(); |
|
|
|
|
|
tone_duration=duration; |
|
tone_start=millis(); |
|
|
|
|
|
tone_enabled=1; |
|
} |
|
|
|
void tonetoggle() { |
|
|
|
|
|
if (!tone_enabled) return; |
|
|
|
|
|
if (millis() > tone_duration+tone_start) { |
|
tone_enabled=0; |
|
digitalWrite(tone_pin, LOW); |
|
tone_pinstate=0; |
|
return; |
|
} |
|
|
|
|
|
if (micros() > tone_intervall+tone_micros) { |
|
tone_micros=micros(); |
|
tone_pinstate=!tone_pinstate; |
|
digitalWrite(tone_pin, tone_pinstate); |
|
} |
|
} |
|
#elif defined(ARDUINO_TTGO_T7_V14_Mini32) && defined(PS2FABLIB) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void playtone(uint8_t pin, uint16_t frequency, uint16_t duration, uint8_t volume) { |
|
|
|
|
|
if (frequency == 0) { |
|
soundGenerator.play(false); |
|
soundGenerator.clear(); |
|
return; |
|
} |
|
|
|
|
|
if (duration == 0) duration=60000; |
|
|
|
if (pin == 128) soundGenerator.playSound(SineWaveformGenerator(), frequency, duration, volume); |
|
if (pin == 129) soundGenerator.playSound(SquareWaveformGenerator(), frequency, duration, volume); |
|
if (pin == 130) soundGenerator.playSound(SawtoothWaveformGenerator(), frequency, duration, volume); |
|
if (pin == 131) soundGenerator.playSound(TriangleWaveformGenerator(), frequency, duration, volume); |
|
if (pin == 132) soundGenerator.playSound(VICNoiseGenerator(), frequency, duration, volume); |
|
if (pin == 133) soundGenerator.playSound(NoiseWaveformGenerator(), frequency, duration, volume); |
|
if (pin >= 255 && pin < 512 ) { |
|
pin=pin-255; |
|
SquareWaveformGenerator sqw; |
|
sqw.setDutyCycle(pin); |
|
soundGenerator.playSound(sqw, frequency, duration, volume); |
|
} |
|
} |
|
#else |
|
|
|
void playtone(uint8_t pin, uint16_t frequency, uint16_t duration, uint8_t volume) { |
|
if (frequency == 0) { |
|
noTone(pin); |
|
} else if (duration == 0) { |
|
tone(pin, frequency); |
|
} else { |
|
tone(pin, frequency, duration); |
|
} |
|
} |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t lastyield=0; |
|
uint32_t lastlongyield=0; |
|
int countfasttick = 0; |
|
|
|
void byield() { |
|
|
|
|
|
fastticker(); |
|
|
|
|
|
bloop(); |
|
|
|
#if defined(BASICBGTASK) |
|
|
|
long m=millis(); |
|
|
|
|
|
|
|
if (m-lastyield > YIELDINTERVAL-1) { |
|
yieldfunction(); |
|
lastyield=m; |
|
} |
|
|
|
|
|
if (m-lastlongyield > LONGYIELDINTERVAL-1) { |
|
longyieldfunction(); |
|
lastlongyield=m; |
|
} |
|
#endif |
|
|
|
|
|
|
|
} |
|
|
|
|
|
void bdelay(uint32_t t) { |
|
unsigned long i; |
|
if (t>0) { |
|
i=millis(); |
|
while (millis() < i+t) byield(); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef FASTTICKERPROFILE |
|
int avgfastticker() { |
|
return countfasttick; |
|
} |
|
|
|
void clearfasttickerprofile() { |
|
countfasttick=0; |
|
} |
|
#endif |
|
|
|
|
|
|
|
void fastticker() { |
|
|
|
#if defined(FASTTICKERPROFILE) |
|
countfasttick++; |
|
#endif |
|
|
|
#ifdef ARDUINOTONEEMULATION |
|
tonetoggle(); |
|
#endif |
|
} |
|
|
|
|
|
void yieldfunction() { |
|
#ifdef ARDUINOMQTT |
|
bmqtt.loop(); |
|
#endif |
|
#ifdef ARDUINOUSBKBD |
|
usb.Task(); |
|
#endif |
|
#ifdef ARDUINOZX81KBD |
|
(void) keyboard.peek(); |
|
#endif |
|
|
|
#if defined(ARDUINO_ARCH_ESP8266) |
|
delay(0); |
|
#endif |
|
} |
|
|
|
|
|
void longyieldfunction() { |
|
#ifdef ARDUINOETH |
|
Ethernet.maintain(); |
|
#endif |
|
#ifdef BREAKINBACKGROUND |
|
if (checkch() == BREAKCHAR) breakcondition=1; |
|
#endif |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define FBUFSIZE 32 |
|
|
|
#if defined(ARDUINOSD) || defined(ESPSPIFFS) || defined(ESP32FAT) || defined(STM32SDIO) ||defined(ESPSDMMC) |
|
File ifile; |
|
File ofile; |
|
char tempname[FBUFSIZE]; |
|
#if defined(ARDUINOSD) || defined(STM32SDIO) ||defined(ESPSDMMC) |
|
File root; |
|
File file; |
|
#ifdef ARDUINO_ARCH_ESP32 |
|
const char rootfsprefix[2] = "/"; |
|
#else |
|
const char rootfsprefix[1] = ""; |
|
#endif |
|
#endif |
|
#if defined(ESPSPIFFS) || defined(ESP32FAT) |
|
const char rootfsprefix[2] = "/"; |
|
#ifdef ARDUINO_ARCH_ESP8266 |
|
Dir root; |
|
File file; |
|
#endif |
|
#ifdef ARDUINO_ARCH_ESP32 |
|
File root; |
|
File file; |
|
#endif |
|
#endif |
|
#ifdef ARDUINO_ARCH_ESP8266 |
|
|
|
|
|
#define FILE_OWRITE (sdfat::O_READ | sdfat::O_WRITE | sdfat::O_CREAT | sdfat::O_TRUNC) |
|
#else |
|
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_STM32) |
|
#define FILE_OWRITE FILE_WRITE |
|
#else |
|
#define FILE_OWRITE (O_READ | O_WRITE | O_CREAT | O_TRUNC) |
|
#endif |
|
#endif |
|
#endif |
|
|
|
|
|
#ifdef RP2040LITTLEFS |
|
#define FILESYSTEMDRIVER |
|
FILE* ifile; |
|
FILE* ofile; |
|
DIR* root; |
|
struct dirent* file; |
|
LittleFS_MBED *myFS; |
|
const char rootfsprefix[10] = MBED_LITTLEFS_FILE_PREFIX; |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef GIGAUSBFS |
|
#define FILESYSTEMDRIVER |
|
USBHostMSD* msdp; |
|
mbed::FATFileSystem* usbp; |
|
FILE* ifile; |
|
FILE* ofile; |
|
DIR* root; |
|
struct dirent *file; |
|
const char rootfsprefix[10] = "/usb/"; |
|
#endif |
|
|
|
|
|
#ifdef ARDUINOEFS |
|
byte ifile; |
|
byte ofile; |
|
byte file; |
|
#endif |
|
|
|
|
|
#ifdef HASBUILDIN |
|
char* buildin_ifile = 0; |
|
uint16_t buildin_ifilepointer = 0; |
|
char* buildin_file = 0; |
|
char* buildin_pgm = 0; |
|
uint8_t buildin_rootactive = 0; |
|
uint16_t buildin_rootpointer = 0; |
|
char buildin_tempname[FBUFSIZE]; |
|
#endif |
|
|
|
|
|
|
|
#if defined(RP2040LITTLEFS) || defined(ESPSPIFFS) || defined(ESP32FAT) || defined(ARDUINOSD) || defined(GIGAUSBFS) || defined(ESPSDMMC) |
|
char tmpfilename[10+FBUFSIZE]; |
|
|
|
|
|
char* mkfilename(const char* filename) { |
|
uint8_t i,j; |
|
for(i=0; i<10 && rootfsprefix[i]!=0; i++) tmpfilename[i]=rootfsprefix[i]; |
|
tmpfilename[i++]='/'; |
|
for(j=0; j<FBUFSIZE && filename[j]!=0; j++) tmpfilename[i++]=filename[j]; |
|
tmpfilename[i]=0; |
|
return tmpfilename; |
|
} |
|
|
|
|
|
const char* rmrootfsprefix(const char* filename) { |
|
uint8_t i=0; |
|
while (filename[i] == rootfsprefix[i] && rootfsprefix[i] !=0 && filename[i] !=0 ) i++; |
|
if (filename[i]=='/') i++; |
|
return &filename[i]; |
|
} |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t fsstart; |
|
|
|
void fsbegin() { |
|
#ifdef ARDUINOSD |
|
#ifndef SDPIN |
|
#define SDPIN |
|
#endif |
|
if (SD.begin(SDPIN)) fsstart=1; else fsstart=0; |
|
#endif |
|
#ifdef STM32SDIO |
|
static int fsbegins = 0; |
|
while (fsbegins<100) { |
|
if (SD.begin(SD_DETECT_PIN)) { fsstart=1; break; } else fsstart=0; |
|
bdelay(20); |
|
fsbegins++; |
|
} |
|
#endif |
|
#if defined(ESPSPIFFS) && defined(ARDUINO_ARCH_ESP8266) |
|
if (SPIFFS.begin()) fsstart=1; else fsstart=0; |
|
#endif |
|
#if defined(ESPSPIFFS) && defined(ARDUINO_ARCH_ESP32) |
|
if (SPIFFS.begin()) fsstart=1; else fsstart=0; |
|
#endif |
|
#if defined(ESP32FAT) && defined(ARDUINO_ARCH_ESP32) |
|
if (FFat.begin()) fsstart=1; else fsstart=0; |
|
#endif |
|
#if defined(ESPSDMMC) && defined(ARDUINO_ARCH_ESP32) |
|
if (SD_MMC.begin()) fsstart=1; else fsstart=0; |
|
#endif |
|
#ifdef RP2040LITTLEFS |
|
myFS = new LittleFS_MBED(); |
|
if (myFS->init()) fsstart=1; else fsstart=0; |
|
#endif |
|
#ifdef GIGAUSBFS |
|
static int fsbegins = 0; |
|
int usbfsstat; |
|
if (RTDEBUG) consolelog("Starting USB file system.\n"); |
|
|
|
pinMode(PA_15, OUTPUT); |
|
if (RTDEBUG) consolelog("Power down USB port.\n"); |
|
digitalWrite(PA_15, LOW); |
|
bdelay(100); |
|
if (RTDEBUG) consolelog("Power up USB port.\n"); |
|
digitalWrite(PA_15, HIGH); |
|
bdelay(100); |
|
|
|
|
|
if (msdp) delete msdp; |
|
if (usbp) delete usbp; |
|
|
|
|
|
msdp=new USBHostMSD(); |
|
usbp=new mbed::FATFileSystem("usb"); |
|
|
|
|
|
while(!msdp->connect() && fsbegins++ < 10) { bdelay(500); } |
|
if (msdp->connected()) { |
|
if (RTDEBUG) { consolelog("msd connected\n"); } |
|
usbfsstat=usbp->mount(msdp); |
|
if (RTDEBUG) { |
|
consolelog("USB mount status is "); |
|
consolelognum(usbfsstat); |
|
consolelog("\n"); |
|
} |
|
fsstart=!usbfsstat; |
|
} else { |
|
if (RTDEBUG) { consolelog("msd not connected\n"); } |
|
fsstart=0; |
|
} |
|
|
|
if (RTDEBUG) { consolelog("fsstart is: "); consolelognum(fsstart); |
|
consolelog("\n"); } |
|
#endif |
|
#ifdef ARDUINOEFS |
|
uint8_t s=EFS.begin(); |
|
if (s>0) { |
|
fsstart=s; |
|
} else { |
|
if (EFS.format(32)) fsstart=32; else fsstart=0;; |
|
} |
|
#endif |
|
#ifdef HASBUILDIN |
|
|
|
#endif |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t fsstat(uint8_t c) { |
|
#if defined(FILESYSTEMDRIVER) |
|
if (c == 0) return 1; |
|
if (c == 1) return fsstart; |
|
#endif |
|
return 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void filewrite(char c) { |
|
#if defined(ARDUINOSD) || defined(ESPSPIFFS)||defined(ESP32FAT)||defined(STM32SDIO)|| defined(ESPSDMMC) |
|
if (ofile) { ofile.write(c); return; } |
|
#endif |
|
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS) |
|
if (ofile) { fputc(c, ofile); return; } |
|
#endif |
|
#if defined(ARDUINOEFS) |
|
if (ofile) { |
|
if (!EFS.fputc(c, ofile)) ioer=-1; |
|
return; |
|
} |
|
#endif |
|
|
|
ioer=1; |
|
} |
|
|
|
char fileread() { |
|
char c; |
|
|
|
|
|
#if defined(HASBUILDIN) |
|
if (buildin_ifile != 0) { |
|
c=pgm_read_byte(buildin_ifile+buildin_ifilepointer); |
|
if (c != '\f') buildin_ifilepointer++; else { ioer=-1; c=255; } |
|
return c; |
|
} |
|
#endif |
|
#if defined(ARDUINOSD) || defined(ESPSPIFFS)||defined(ESP32FAT)||defined(STM32SDIO) || defined(ESPSDMMC) |
|
if (ifile) c=ifile.read(); else { ioer=1; return 0; } |
|
if (c == -1 || c == 255) ioer=-1; |
|
return c; |
|
#endif |
|
#if defined(RP2040LITTLEFS)||defined(GIGAUSBFS) |
|
if (ifile) c=fgetc(ifile); else { ioer=1; return 0; } |
|
if (c == -1 || c == 255) ioer=-1; |
|
return c; |
|
#endif |
|
#ifdef ARDUINOEFS |
|
if (ifile) c=EFS.fgetc(ifile); else { ioer=1; return 0; } |
|
if (c == -1|| c == 255) ioer=-1; |
|
return c; |
|
#endif |
|
return 0; |
|
} |
|
|
|
int fileavailable(){ |
|
|
|
#if defined(HASBUILDIN) |
|
char c; |
|
if (buildin_ifile != 0) { |
|
c=pgm_read_byte(buildin_ifile+buildin_ifilepointer); |
|
if (c != '\f') return 1; else return 0; |
|
} |
|
#endif |
|
#if defined(ARDUINOSD) || defined(ESPSPIFFS) || defined(ESP32FAT) || defined(STM32SDIO) || defined(ESPSDMMC) |
|
return ifile.available(); |
|
#endif |
|
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS) |
|
return !feof(ifile); |
|
#endif |
|
#ifdef ARDUINOEFS |
|
return EFS.available(ifile); |
|
#endif |
|
return 0; |
|
} |
|
|
|
uint8_t ifileopen(const char* filename){ |
|
|
|
#if defined(HASBUILDIN) |
|
byte i, file; |
|
char* name; |
|
|
|
|
|
buildin_ifilepointer=0; |
|
buildin_ifile=0; |
|
|
|
file=0; |
|
while(1) { |
|
|
|
|
|
name=(char*)pgm_read_ptr(&buildin_program_names[file]); |
|
if (name == 0) break; |
|
|
|
|
|
for(i=0;i<32;i++) { |
|
char c=pgm_read_byte(&name[i]); |
|
|
|
|
|
if (c == 0 && filename[i] == 0) { |
|
buildin_ifile=(char*)pgm_read_ptr(&buildin_programs[file]); |
|
return (buildin_ifile != 0); |
|
} |
|
|
|
|
|
if (c != filename[i]) { |
|
file++; |
|
break; |
|
} |
|
} |
|
} |
|
#endif |
|
|
|
|
|
|
|
|
|
if (!fsstart) return 0; |
|
|
|
#if defined(ARDUINOSD) |
|
ifile=SD.open(mkfilename(filename), FILE_READ); |
|
return ifile != 0; |
|
#endif |
|
#ifdef ESPSDMMC |
|
ifile=SD_MMC.open(mkfilename(filename), FILE_READ); |
|
return ifile != 0; |
|
#endif |
|
#if defined(STM32SDIO) |
|
ifile=SD.open(filename); |
|
return ifile != 0; |
|
#endif |
|
#ifdef ESPSPIFFS |
|
ifile=SPIFFS.open(mkfilename(filename), "r"); |
|
return ifile != 0; |
|
#endif |
|
#ifdef ESP32FAT |
|
ifile=FFat.open(mkfilename(filename), "r"); |
|
return ifile != 0; |
|
#endif |
|
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS) |
|
ifile=fopen(mkfilename(filename), "r"); |
|
return ifile != 0; |
|
#endif |
|
#ifdef ARDUINOEFS |
|
ifile=EFS.fopen(filename, "r"); |
|
return ifile != 0; |
|
#endif |
|
return 0; |
|
} |
|
void ifileclose(){ |
|
#if defined(HASBUILDIN) |
|
if (buildin_ifile) { |
|
buildin_ifile=0; |
|
buildin_ifilepointer=0; |
|
} |
|
#endif |
|
#if defined(ARDUINOSD) || defined(ESPSPIFFS) || defined(ESP32FAT) || defined(STM32SDIO) || defined(ESPSDMMC) |
|
if (ifile) ifile.close(); |
|
#endif |
|
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS) |
|
if (ifile) fclose(ifile); |
|
#endif |
|
#ifdef ARDUINOEFS |
|
if (ifile) EFS.fclose(ifile); |
|
#endif |
|
|
|
} |
|
|
|
uint8_t ofileopen(const char* filename, const char* m){ |
|
|
|
|
|
|
|
|
|
|
|
if (!fsstart) return 0; |
|
|
|
#if defined(ARDUINOSD) |
|
if (*m == 'w') ofile=SD.open(mkfilename(filename), FILE_OWRITE); |
|
|
|
#ifdef FILE_APPEND |
|
if (*m == 'a') ofile=SD.open(mkfilename(filename), FILE_APPEND); |
|
#else |
|
if (*m == 'a') ofile=SD.open(mkfilename(filename), FILE_WRITE); |
|
#endif |
|
return ofile != 0; |
|
#endif |
|
#if defined(ESPSDMMC) |
|
if (*m == 'w') ofile=SD_MMC.open(mkfilename(filename), FILE_OWRITE); |
|
|
|
#ifdef FILE_APPEND |
|
if (*m == 'a') ofile=SD_MMC.open(mkfilename(filename), FILE_APPEND); |
|
#else |
|
if (*m == 'a') ofile=SD_MMC.open(mkfilename(filename), FILE_WRITE); |
|
#endif |
|
return ofile != 0; |
|
#endif |
|
#if defined(STM32SDIO) |
|
if (*m == 'w') ofile=SD.open(filename, FILE_OWRITE); |
|
|
|
#ifdef FILE_APPEND |
|
if (*m == 'a') ofile=SD.open(filename, FILE_APPEND); |
|
#else |
|
if (*m == 'a') ofile=SD.open(filename, FILE_WRITE); |
|
#endif |
|
return ofile != 0; |
|
#endif |
|
#ifdef ESPSPIFFS |
|
ofile=SPIFFS.open(mkfilename(filename), m); |
|
return ofile != 0; |
|
#endif |
|
#ifdef ESP32FAT |
|
ofile=FFat.open(mkfilename(filename), m); |
|
return ofile != 0; |
|
#endif |
|
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS) |
|
ofile=fopen(mkfilename(filename), m); |
|
return ofile != 0; |
|
#endif |
|
#ifdef ARDUINOEFS |
|
ofile=EFS.fopen(filename, m); |
|
return ofile != 0; |
|
#endif |
|
|
|
#if defined(HASBUILDIN) |
|
ioer=-1; |
|
return 0; |
|
#endif |
|
return 0; |
|
} |
|
|
|
void ofileclose(){ |
|
#if defined(ARDUINOSD) || defined(ESPSPIFFS)||defined(ESP32FAT)|| defined(STM32SDIO) || defined(ESPSDMMC) |
|
if (ofile) ofile.close(); |
|
#endif |
|
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS) |
|
if (ofile) fclose(ofile); |
|
#endif |
|
#ifdef ARDUINOEFS |
|
if (ofile) EFS.fclose(ofile); |
|
#endif |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void rootopen() { |
|
|
|
#ifdef HASBUILDIN |
|
buildin_rootpointer=0; |
|
buildin_rootactive=1; |
|
#endif |
|
|
|
|
|
|
|
|
|
if (!fsstart) return; |
|
|
|
#if defined(ARDUINOSD) || defined(STM32SDIO) |
|
root=SD.open("/"); |
|
#endif |
|
#if defined(ESPSDMMC) |
|
root=SD_MMC.open("/"); |
|
#endif |
|
#ifdef ESPSPIFFS |
|
#ifdef ARDUINO_ARCH_ESP8266 |
|
root=SPIFFS.openDir("/"); |
|
#endif |
|
#ifdef ARDUINO_ARCH_ESP32 |
|
root=SPIFFS.open("/"); |
|
#endif |
|
#endif |
|
#ifdef ESP32FAT |
|
root=FFat.open("/"); |
|
#endif |
|
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS) |
|
root=opendir(rootfsprefix); |
|
#endif |
|
#ifdef ARDUINOEFS |
|
EFS.dirp=0; |
|
#endif |
|
} |
|
|
|
uint8_t rootnextfile() { |
|
|
|
#ifdef HASBUILDIN |
|
if (buildin_rootactive) { |
|
buildin_file=(char*)pgm_read_ptr(&buildin_program_names[buildin_rootpointer]); |
|
if (buildin_file != 0) { |
|
buildin_pgm=(char*)pgm_read_ptr(&buildin_programs[buildin_rootpointer]); |
|
buildin_rootpointer++; |
|
return 1; |
|
} else { |
|
buildin_rootactive=0; |
|
} |
|
} |
|
#endif |
|
|
|
|
|
|
|
|
|
if (!fsstart) return 0; |
|
|
|
#if defined(ARDUINOSD) || defined(STM32SDIO)||defined(ESPSDMMC) |
|
file=root.openNextFile(); |
|
return (file != 0); |
|
#endif |
|
#ifdef ESPSPIFFS |
|
#ifdef ARDUINO_ARCH_ESP8266 |
|
if (root.next()) { |
|
file=root.openFile("r"); |
|
return 1; |
|
} else { |
|
return 0; |
|
} |
|
#endif |
|
#ifdef ARDUINO_ARCH_ESP32 |
|
file=root.openNextFile(); |
|
return (file != 0); |
|
#endif |
|
#endif |
|
#ifdef ESP32FAT |
|
#ifdef ARDUINO_ARCH_ESP32 |
|
file=root.openNextFile(); |
|
return (file != 0); |
|
#endif |
|
#endif |
|
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS) |
|
file = readdir(root); |
|
return (file != 0); |
|
#endif |
|
#ifdef ARDUINOEFS |
|
file = EFS.readdir(); |
|
return (file != 0); |
|
#endif |
|
return 0; |
|
} |
|
|
|
uint8_t rootisfile() { |
|
#ifdef HASBUILDIN |
|
if (buildin_rootactive) return 1; |
|
#endif |
|
#if defined(ARDUINOSD) || defined(STM32SDIO) || defined(ESPSDMMC) |
|
return (!file.isDirectory()); |
|
#endif |
|
|
|
#ifdef ESPSPIFFS |
|
#ifdef ARDUINO_ARCH_ESP8266 |
|
return 1; |
|
#endif |
|
#ifdef ARDUINO_ARCH_ESP32 |
|
return (!file.isDirectory()); |
|
#endif |
|
#endif |
|
#ifdef ESP32FAT |
|
#ifdef ARDUINO_ARCH_ESP32 |
|
return (!file.isDirectory()); |
|
#endif |
|
#endif |
|
#if defined(RP2040LITTLEFS)||defined(GIGAUSBFS) |
|
return (file->d_type == DT_REG); |
|
#endif |
|
#ifdef ARDUINOEFS |
|
return 1; |
|
#endif |
|
return 0; |
|
} |
|
|
|
const char* rootfilename() { |
|
#ifdef HASBUILDIN |
|
if (buildin_rootactive) { |
|
for(int i=0; i<FBUFSIZE; i++) { |
|
buildin_tempname[i]=(char)pgm_read_byte(&buildin_file[i]); |
|
if (buildin_tempname[i] == 0) return buildin_tempname; |
|
} |
|
} |
|
#endif |
|
#if defined(ARDUINOSD) || defined(ESPSDMMC) |
|
return rmrootfsprefix(file.name()); |
|
#endif |
|
#if defined(STM32SDIO) |
|
return (char*) file.name(); |
|
#endif |
|
#ifdef ESPSPIFFS |
|
#ifdef ARDUINO_ARCH_ESP8266 |
|
int i=0; |
|
String s=root.fileName(); |
|
for (i=0; i<s.length(); i++) tempname[i]=s[i]; |
|
tempname[i]=0; |
|
return rmrootfsprefix(tempname); |
|
#endif |
|
#ifdef ARDUINO_ARCH_ESP32 |
|
return rmrootfsprefix(file.name()); |
|
#endif |
|
#endif |
|
#ifdef ESP32FAT |
|
#ifdef ARDUINO_ARCH_ESP32 |
|
return rmrootfsprefix(file.name()); |
|
#endif |
|
#endif |
|
#if defined(RP2040LITTLEFS)||defined(GIGAUSBFS) |
|
return (file->d_name); |
|
#endif |
|
#ifdef ARDUINOEFS |
|
return EFS.filename(file); |
|
#endif |
|
return 0; |
|
} |
|
|
|
uint32_t rootfilesize() { |
|
#ifdef HASBUILDIN |
|
int i=0; |
|
if (buildin_rootactive && buildin_pgm) { |
|
for (;;i++) { |
|
char ch=pgm_read_byte(&buildin_pgm[i]); |
|
if (ch == 0) return i; |
|
} |
|
} |
|
#endif |
|
#if defined(ARDUINOSD) || defined(ESPSPIFFS) || defined(ESP32FAT) || defined(STM32SDIO) || defined(ESPSDMMC) |
|
return file.size(); |
|
#endif |
|
#if defined(RP2040LITTLEFS)||defined(GIGAUSBFS) |
|
|
|
#endif |
|
#ifdef ARDUINOEFS |
|
return EFS.filesize(file); |
|
#endif |
|
return 0; |
|
} |
|
|
|
void rootfileclose() { |
|
#if defined(ARDUINOSD) || defined(ESPSPIFFS) || defined(ESP32FAT) || defined(STM32SDIO) || defined(ESPSDMMC) |
|
file.close(); |
|
#endif |
|
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS) |
|
|
|
#endif |
|
#ifdef ARDUINOEFS |
|
#endif |
|
} |
|
|
|
void rootclose(){ |
|
#ifdef HASBUILDIN |
|
buildin_rootactive=0; |
|
#endif |
|
#if defined(ARDUINOSD) || defined(STM32SDIO) || defined(ESPSDMMC) |
|
root.close(); |
|
#endif |
|
#ifdef ESPSPIFFS |
|
#ifdef ARDUINO_ARCH_ESP8266 |
|
return; |
|
#endif |
|
#ifdef ARDUINO_ARCH_ESP32 |
|
root.close(); |
|
#endif |
|
#endif |
|
#ifdef ESP32FAT |
|
#ifdef ARDUINO_ARCH_ESP32 |
|
root.close(); |
|
#endif |
|
#endif |
|
#ifdef RP2040LITTLEFS |
|
#endif |
|
#ifdef GIGAUSBFS |
|
if (root) (void) closedir(root); |
|
#endif |
|
#ifdef ARDUINOEFS |
|
#endif |
|
} |
|
|
|
|
|
|
|
|
|
void removefile(const char *filename) { |
|
#if defined(ARDUINOSD) |
|
SD.remove(mkfilename(filename)); |
|
return; |
|
#endif |
|
#if defined(ESPSDMMC) |
|
SD_MMC.remove(mkfilename(filename)); |
|
return; |
|
#endif |
|
#if defined(STM32SDIO) |
|
SD.remove(filename); |
|
return; |
|
#endif |
|
#ifdef ESPSPIFFS |
|
SPIFFS.remove(mkfilename(filename)); |
|
return; |
|
#endif |
|
#ifdef ESP32FAT |
|
FFat.remove(mkfilename(filename)); |
|
return; |
|
#endif |
|
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS) |
|
remove(mkfilename(filename)); |
|
return; |
|
#endif |
|
#ifdef ARDUINOEFS |
|
EFS.remove(filename); |
|
return; |
|
#endif |
|
} |
|
|
|
|
|
|
|
|
|
void formatdisk(uint8_t i) { |
|
#if defined(ARDUINOSD) || defined(STM32SDIO)||defined(GIGAUSBFS) || defined(ESPSDMMC) |
|
return; |
|
return; |
|
#endif |
|
#ifdef ESPSPIFFS |
|
if (SPIFFS.format()) { SPIFFS.begin(); fsstart=1; } else { fsstart=0; } |
|
#endif |
|
#ifdef ESP32FAT |
|
FFat.end(); |
|
if (FFat.format()) { FFat.begin(); fsstart=1; } else { fsstart=0; } |
|
#endif |
|
#ifdef RP2040LITTLEFS |
|
fs.reformat(&bd); |
|
return; |
|
#endif |
|
#ifdef ARDUINOEFS |
|
if (i>0) { |
|
if (EFS.format(i)) { EFS.begin(); fsstart=1; } else { fsstart=0; } |
|
} else ioer=1; |
|
return; |
|
#endif |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void bufferbegin() {} |
|
|
|
|
|
|
|
void bufferwrite(char c) { |
|
if (!nullbuffer) return; |
|
switch (c) { |
|
case 12: |
|
|
|
nullbuffer[0]=0; |
|
nullbuffer[1]=0; |
|
break; |
|
case 10: |
|
case 13: |
|
break; |
|
case 8: |
|
if (nullbuffer[0]>0) nullbuffer[0]--; |
|
break; |
|
default: |
|
if (nullbuffer[0] < nullbufsize-1 && nullbuffer[0] < 127) { |
|
nullbuffer[++nullbuffer[0]]=c; |
|
nullbuffer[nullbuffer[0]+1]=0; |
|
} |
|
break; |
|
} |
|
} |
|
|
|
|
|
char bufferread() { return 0; } |
|
uint16_t bufferavailable() { return 0; } |
|
char buffercheckch() { return 0; } |
|
void bufferflush() { } |
|
uint16_t bufferins(char *b, uint16_t nb) { return 0; } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOPICOSERIAL |
|
volatile char picochar; |
|
volatile uint8_t picoa = 0; |
|
volatile char* picob = NULL; |
|
int16_t picobsize = 0; |
|
volatile int16_t picoi = 1; |
|
volatile uint16_t picoz; |
|
|
|
|
|
const uint16_t MIN_2X_BAUD = F_CPU/(4*(2*0XFFF + 1)) + 1; |
|
|
|
|
|
void picobegin(uint32_t baud) { |
|
uint16_t baud_setting; |
|
cli(); |
|
if ((F_CPU != 16000000UL || baud != 57600) && baud > MIN_2X_BAUD) { |
|
UCSR0A = 1 << U2X0; |
|
baud_setting = (F_CPU / 4 / baud - 1) / 2; |
|
} else { |
|
UCSR0A = 0; |
|
baud_setting = (F_CPU / 8 / baud - 1) / 2; |
|
} |
|
|
|
UBRR0H = baud_setting >> 8; |
|
UBRR0L = baud_setting; |
|
|
|
UCSR0B |= (1 << TXEN0) | (1 << RXEN0) | (1 << RXCIE0); |
|
sei(); |
|
} |
|
|
|
|
|
void picowrite(char b) { |
|
while (((1 << UDRIE0) & UCSR0B) || !(UCSR0A & (1 << UDRE0))) {} |
|
UDR0 = b; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void picogetchar(char c){ |
|
if (picob && (!picoa)) { |
|
picochar=c; |
|
if (picochar != '\n' && picochar != '\r' && picoi<picobsize-1) { |
|
if (c == 127 || c == 8) { |
|
if (picoi>1) picoi--; else return; |
|
} else |
|
picob[picoi++]=picochar; |
|
picowrite(picochar); |
|
} else { |
|
picoa = 1; |
|
picob[picoi]=0; |
|
picob[0]=picoi; |
|
picoz=picoi; |
|
picoi=1; |
|
} |
|
picochar=0; |
|
} else { |
|
if (c != 10) picochar=c; |
|
} |
|
} |
|
|
|
|
|
uint16_t picoins(char *b, uint16_t nb) { |
|
picob=b; |
|
picobsize=nb; |
|
picoa=0; |
|
|
|
|
|
while (!picoa) byield(); |
|
outch(10); |
|
return picoz; |
|
} |
|
|
|
|
|
#ifdef USART_RX_vect |
|
ISR(USART_RX_vect) { |
|
char c=UDR0; |
|
picogetchar(c); |
|
} |
|
#else |
|
|
|
ISR(USART0_RX_vect) { |
|
char c=UDR0; |
|
picogetchar(c); |
|
} |
|
#endif |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char serialread() { |
|
#ifdef ARDUINOPICOSERIAL |
|
char c; |
|
c=picochar; |
|
picochar=0; |
|
return c; |
|
#else |
|
while (!SERIALPORT.available()) byield(); |
|
return SERIALPORT.read(); |
|
#endif |
|
} |
|
|
|
|
|
|
|
|
|
|
|
void serialbegin() { |
|
#ifdef ARDUINOPICOSERIAL |
|
picobegin(serial_baudrate); |
|
#else |
|
SERIALPORT.begin(serial_baudrate); |
|
#endif |
|
bdelay(1000); |
|
} |
|
|
|
|
|
uint8_t serialstat(uint8_t c) { |
|
if (c == 0) return 1; |
|
if (c == 1) return serial_baudrate/100; |
|
return 0; |
|
} |
|
|
|
|
|
void serialwrite(char c) { |
|
#ifdef ARDUINOPICOSERIAL |
|
picowrite(c); |
|
#else |
|
|
|
SERIALPORT.write(c); |
|
|
|
#endif |
|
} |
|
|
|
|
|
char serialcheckch() { |
|
#ifdef ARDUINOPICOSERIAL |
|
return picochar; |
|
#else |
|
if (SERIALPORT.available()) return SERIALPORT.peek(); else return 0; |
|
#endif |
|
} |
|
|
|
|
|
uint16_t serialavailable() { |
|
#ifdef ARDUINOPICOSERIAL |
|
return picoi; |
|
#else |
|
return SERIALPORT.available(); |
|
#endif |
|
} |
|
|
|
|
|
void serialflush() { |
|
#ifdef ARDUINOPICOSERIAL |
|
return; |
|
#else |
|
while (SERIALPORT.available()) SERIALPORT.read(); |
|
#endif |
|
} |
|
|
|
|
|
uint16_t serialins(char *b, uint16_t nb) { |
|
#ifdef ARDUINOPICOSERIAL |
|
return picoins(b, nb); |
|
#else |
|
return consins(b, nb); |
|
#endif |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOPRT |
|
#if !defined(ARDUINO_AVR_MEGA2560) && !defined(ARDUINO_SAM_DUE) && !defined(ARDUINO_AVR_NANO_EVERY) \ |
|
&& !defined(ARDUINO_NANO_RP2040_CONNECT) && !defined(ARDUINO_RASPBERRY_PI_PICO) \ |
|
&& !defined(ARDUINO_SEEED_XIAO_M0) && !defined(ARDUINO_ARCH_RENESAS) &&! defined(ARDUINO_ARCH_MBED) |
|
#include <SoftwareSerial.h> |
|
SoftwareSerial PRTSERIAL(SOFTSERIALRX, SOFTSERIALTX); |
|
#endif |
|
|
|
|
|
void prtbegin() { |
|
PRTSERIAL.begin(serial1_baudrate); |
|
} |
|
|
|
|
|
char prtopen(char* filename, uint16_t mode) {} |
|
void prtclose() {} |
|
|
|
uint8_t prtstat(uint8_t c) { |
|
if (c == 0) return 1; |
|
if (c == 1) return serial1_baudrate; |
|
return 0; |
|
} |
|
|
|
void prtwrite(char c) { |
|
PRTSERIAL.write(c); |
|
} |
|
|
|
char prtread() { |
|
while (!PRTSERIAL.available()) byield(); |
|
return PRTSERIAL.read(); |
|
} |
|
|
|
char prtcheckch() { |
|
if (PRTSERIAL.available()) return PRTSERIAL.peek(); else return 0; |
|
} |
|
|
|
uint16_t prtavailable() { |
|
return PRTSERIAL.available(); |
|
} |
|
|
|
void prtset(uint32_t s) { |
|
serial1_baudrate=s; |
|
prtbegin(); |
|
} |
|
|
|
uint16_t prtins(char* b, uint16_t nb) { |
|
if (blockmode > 0) return inb(b, nb); else return consins(b, nb); |
|
} |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(HASWIRE) |
|
|
|
|
|
|
|
|
|
|
|
void wirebegin() { |
|
#ifndef SDA_PIN |
|
Wire.begin(); |
|
#else |
|
Wire.begin(SDA_PIN, SCL_PIN); |
|
#endif |
|
} |
|
#endif |
|
|
|
|
|
#if defined(HASWIRE) |
|
uint8_t wire_slaveid = 0; |
|
uint8_t wire_myid = 0; |
|
#define ARDUINOWIREBUFFER 32 |
|
#ifdef ARDUINOWIRESLAVE |
|
char wirereceivebuffer[ARDUINOWIREBUFFER]; |
|
short wirereceivechars = 0; |
|
char wirerequestbuffer[ARDUINOWIREBUFFER]; |
|
short wirerequestchars = 0; |
|
#endif |
|
|
|
void wireslavebegin(uint8_t s) { |
|
#ifndef SDA_PIN |
|
Wire.begin(s); |
|
#else |
|
Wire.begin(SDA_PIN, SCL_PIN, s); |
|
#endif |
|
} |
|
|
|
|
|
uint8_t wirestat(uint8_t c) { |
|
switch (c) { |
|
case 0: |
|
return 1; |
|
#ifdef ARDUINOWIRESLAVE |
|
case 1: |
|
return wirerequestchars; |
|
#endif |
|
default: |
|
return 0; |
|
} |
|
} |
|
|
|
|
|
uint16_t wireavailable() { |
|
|
|
if (wire_myid == 0) return 1; |
|
#ifdef ARDUINOWIRESLAVE |
|
else return wirereceivechars; |
|
#else |
|
else return 0; |
|
#endif |
|
} |
|
|
|
#ifdef ARDUINOWIRESLAVE |
|
|
|
void wireonreceive(int h) { |
|
wirereceivechars=h; |
|
if (h>ARDUINOWIREBUFFER) h=ARDUINOWIREBUFFER; |
|
for (int i=0; i<h; i++) wirereceivebuffer[i]=Wire.read(); |
|
} |
|
|
|
|
|
void wireonrequest() { |
|
Wire.write(wirerequestbuffer, wirerequestchars); |
|
wirerequestchars=0; |
|
} |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
void wireopen(uint8_t s, uint8_t m) { |
|
if (m == 0) { |
|
wire_slaveid=s; |
|
|
|
if (wire_myid != 0) { |
|
wirebegin(); |
|
wire_myid=0; |
|
} |
|
} else if ( m == 1 ) { |
|
wire_myid=s; |
|
wire_slaveid=0; |
|
#ifdef ARDUINOWIRESLAVE |
|
wireslavebegin(wire_myid); |
|
Wire.onReceive(&wireonreceive); |
|
Wire.onRequest(&wireonrequest); |
|
#endif |
|
} |
|
} |
|
|
|
|
|
uint16_t wireins(char *b, uint8_t l) { |
|
uint16_t z; |
|
if (wire_myid == 0) { |
|
z=0; |
|
if (l>ARDUINOWIREBUFFER) l=ARDUINOWIREBUFFER; |
|
if (!Wire.requestFrom(wire_slaveid, l)) ioer=1; |
|
while (Wire.available() && z<l) b[++z]=Wire.read(); |
|
} else { |
|
#ifdef ARDUINOWIRESLAVE |
|
for (z=0; z<wirereceivechars && z<l; z++) b[z+1]=wirereceivebuffer[z]; |
|
wirereceivechars=0; |
|
#endif |
|
} |
|
b[0]=z; |
|
return z; |
|
} |
|
|
|
|
|
char wireread() { |
|
char wbuffer[2]; |
|
if (wireins(wbuffer, 1)) return wbuffer[1]; else return 0; |
|
} |
|
|
|
|
|
void wireouts(char *b, uint8_t l) { |
|
int16_t z; |
|
if (l>ARDUINOWIREBUFFER) l=ARDUINOWIREBUFFER; |
|
if (wire_myid == 0) { |
|
Wire.beginTransmission(wire_slaveid); |
|
#ifdef ARDUINO_ARCH_ESP32 |
|
for(z=0; z<l; z++) Wire.write(b[z]); |
|
#else |
|
Wire.write(b, l); |
|
#endif |
|
ioer=Wire.endTransmission(); |
|
} else { |
|
#ifdef ARDUINOWIRESLAVE |
|
for (int i=0; i<l; i++) wirerequestbuffer[i]=b[i]; |
|
wirerequestchars=l; |
|
#endif |
|
} |
|
} |
|
#endif |
|
|
|
|
|
#if defined(HASSIMPLEWIRE) || defined(HASWIRE) |
|
|
|
|
|
|
|
#if defined(ARDUINODIRECTI2C) |
|
|
|
|
|
uint32_t const F_TWI = 100000L; |
|
uint8_t const TWSR_MTX_DATA_ACK = 0x28; |
|
uint8_t const TWSR_MTX_ADR_ACK = 0x18; |
|
uint8_t const TWSR_MRX_ADR_ACK = 0x40; |
|
uint8_t const TWSR_START = 0x08; |
|
uint8_t const TWSR_REP_START = 0x10; |
|
uint8_t const I2C_READ = 1; |
|
uint8_t const I2C_WRITE = 0; |
|
|
|
|
|
uint8_t wirecount; |
|
|
|
|
|
void wirebegin() { |
|
digitalWrite(SDA, HIGH); |
|
digitalWrite(SCL, HIGH); |
|
TWSR = 0; |
|
TWBR = (F_CPU/F_TWI - 16)/2; |
|
} |
|
|
|
|
|
void wirestart (uint8_t port, uint8_t n) { |
|
|
|
|
|
port=port<<1; |
|
|
|
|
|
if (n) { |
|
wirecount=n; |
|
port |= I2C_READ; |
|
} |
|
|
|
|
|
TWCR = 1<<TWINT | 1<<TWSTA | 1<<TWEN; |
|
while (!(TWCR & 1<<TWINT)); |
|
|
|
|
|
if ((TWSR & 0xF8) != TWSR_START && (TWSR & 0xF8) != TWSR_REP_START) { ioer=1; return; } |
|
|
|
|
|
TWDR = port; |
|
TWCR = 1<<TWINT | 1<<TWEN; |
|
while (!(TWCR & 1<<TWINT)); |
|
|
|
|
|
if (port & I2C_READ) { |
|
if ((TWSR & 0xF8) == TWSR_MRX_ADR_ACK) ioer=0; else ioer=1; |
|
} else { |
|
if ((TWSR & 0xF8) == TWSR_MTX_ADR_ACK) ioer=0; else ioer=1; |
|
} |
|
} |
|
|
|
|
|
uint8_t wirestop () { |
|
TWCR = 1<<TWINT | 1<<TWEN | 1<<TWSTO; |
|
while (TWCR & 1<<TWSTO); |
|
return ioer; |
|
} |
|
|
|
|
|
void wirewritebyte(uint8_t b) { |
|
TWDR = b; |
|
TWCR = 1<<TWINT | 1<<TWEN; |
|
while (!(TWCR & 1<<TWINT)); |
|
if (!((TWSR & 0xF8) == TWSR_MTX_DATA_ACK)) ioer=1; |
|
} |
|
|
|
|
|
int16_t wirereadbyte () { |
|
if (wirecount != 0) wirecount--; |
|
TWCR = 1<<TWINT | 1<<TWEN | ((wirecount == 0) ? 0 : (1<<TWEA)); |
|
while (!(TWCR & 1<<TWINT)); |
|
return TWDR; |
|
} |
|
#else |
|
|
|
|
|
void wirebegin() { |
|
#ifndef SDA_PIN |
|
Wire.begin(); |
|
#else |
|
Wire.begin(SDA_PIN, SCL_PIN); |
|
#endif |
|
} |
|
|
|
|
|
void wirestart(uint8_t port, uint8_t n) { |
|
if (n) { |
|
if (!Wire.requestFrom(port, n)) ioer=1; |
|
} else { |
|
Wire.beginTransmission(port); |
|
} |
|
} |
|
|
|
|
|
uint8_t wirestop() { return Wire.endTransmission(); } |
|
void wirewritebyte(uint8_t data) { Wire.write(data); } |
|
int16_t wirereadbyte() { return Wire.read(); } |
|
#endif |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINORF24 |
|
|
|
|
|
|
|
|
|
const char rf24_ce = RF24CEPIN; |
|
const char rf24_csn = RF24CSNPIN; |
|
#include <nRF24L01.h> |
|
#include <RF24.h> |
|
rf24_pa_dbm_e rf24_pa = RF24_PA_MAX; |
|
RF24 radio(rf24_ce, rf24_csn); |
|
|
|
|
|
uint8_t radiostat(uint8_t c) { |
|
if (c == 0) return 1; |
|
if (c == 1) return radio.isChipConnected(); |
|
return 0; |
|
} |
|
|
|
|
|
uint64_t pipeaddr(const char *f){ |
|
uint64_t t = 0; |
|
t=(uint8_t)f[0]; |
|
for(short i=1; i<=4; i++) t=t*256+(uint8_t)f[i]; |
|
return t; |
|
} |
|
|
|
|
|
|
|
uint16_t radioins(char *b, uint8_t nb) { |
|
uint16_t z; |
|
|
|
if (radio.available()) { |
|
radio.read(b+1, nb); |
|
if (!blockmode) { |
|
for (z=0; z<nb; z++) if (b[z+1]==0) break; |
|
} else { |
|
z=radio.getPayloadSize(); |
|
if (z > nb) z=nb; |
|
} |
|
b[0]=z; |
|
} else { |
|
b[0]=0; |
|
b[1]=0; |
|
z=0; |
|
} |
|
return z; |
|
} |
|
|
|
|
|
|
|
|
|
char radioread() { |
|
char rbuffer[2]; |
|
if (radioins(rbuffer, 1)) return rbuffer[1]; else return 0; |
|
} |
|
|
|
|
|
void radioouts(char *b, uint8_t l) { |
|
radio.stopListening(); |
|
if (!radio.write(b, l)) ioer=1; |
|
radio.startListening(); |
|
} |
|
|
|
|
|
uint16_t radioavailable() { |
|
return radio.available(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void iradioopen(const char *filename) { |
|
if (!radio.begin()) ioer=1; |
|
radio.openReadingPipe(1, pipeaddr(filename)); |
|
radio.startListening(); |
|
} |
|
|
|
void oradioopen(const char *filename) { |
|
if (!radio.begin()) ioer=1; |
|
radio.openWritingPipe(pipeaddr(filename)); |
|
} |
|
|
|
void radioset(uint8_t s) { |
|
if ((s<0) && (s>3)) {ioer=1; return; } |
|
rf24_pa=(rf24_pa_dbm_e) s; |
|
radio.setPALevel(rf24_pa); |
|
} |
|
|
|
#else |
|
uint8_t radiostat(uint8_t c) { return 0; } |
|
uint64_t pipeaddr(const char *f){ return 0; } |
|
uint16_t radioins(char *b, uint8_t nb) { b[0]=0; b[1]=0; return 0; } |
|
void radioouts(char *b, uint8_t l) {} |
|
uint16_t radioavailable() { return 0; } |
|
void iradioopen(const char *filename) {} |
|
void oradioopen(const char *filename) {} |
|
void radioset(uint8_t s) {} |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOSENSORS |
|
#ifdef ARDUINODHT |
|
#include "DHT.h" |
|
DHT dht(DHTPIN, DHTTYPE); |
|
#endif |
|
#ifdef ARDUINOSHT |
|
#include <SHT3x.h> |
|
SHT3x SHT; |
|
#endif |
|
#ifdef ARDUINOMQ2 |
|
#include <MQ2.h> |
|
MQ2 mq2(MQ2PIN); |
|
#endif |
|
#ifdef ARDUINOLMS6 |
|
#include <Arduino_LSM6DSOX.h> |
|
|
|
#endif |
|
#ifdef ARDUINOAHT |
|
#include <Adafruit_AHTX0.h> |
|
Adafruit_AHTX0 aht; |
|
#endif |
|
#ifdef ARDUINOBMP280 |
|
#include <Adafruit_BMP280.h> |
|
Adafruit_BMP280 bmp; |
|
#endif |
|
#ifdef ARDUINOBME280 |
|
#include <Adafruit_BME280.h> |
|
Adafruit_BME280 bme; |
|
|
|
#endif |
|
|
|
|
|
void sensorbegin(){ |
|
#ifdef ARDUINODHT |
|
dht.begin(); |
|
#endif |
|
#ifdef ARDUINOSHT |
|
SHT.Begin(); |
|
#endif |
|
#ifdef ARDUINOMQ2 |
|
mq2.begin(); |
|
#endif |
|
#ifdef ARDUINOAHT |
|
aht.begin(); |
|
#endif |
|
#ifdef ARDUINOBMP280 |
|
bmp.begin(); |
|
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, |
|
Adafruit_BMP280::SAMPLING_X2, |
|
Adafruit_BMP280::SAMPLING_X16, |
|
Adafruit_BMP280::FILTER_X16, |
|
Adafruit_BMP280::STANDBY_MS_500); |
|
#endif |
|
#ifdef ARDUINOBME280 |
|
bme.begin(); |
|
#endif |
|
} |
|
|
|
float sensorread(uint8_t s, uint8_t v) { |
|
switch (s) { |
|
case 0: |
|
return analogRead(A0+v); |
|
case 1: |
|
#ifdef ARDUINODHT |
|
switch (v) { |
|
case 0: |
|
return 1; |
|
case 1: |
|
return dht.readHumidity(); |
|
case 2: |
|
return dht.readTemperature(); |
|
} |
|
#endif |
|
return 0; |
|
case 2: |
|
#ifdef ARDUINOSHT |
|
switch (v) { |
|
case 0: |
|
return 1; |
|
case 1: |
|
SHT.UpdateData(); |
|
return SHT.GetRelHumidity(); |
|
case 2: |
|
SHT.UpdateData(); |
|
return SHT.GetTemperature(); |
|
} |
|
#endif |
|
return 0; |
|
case 3: |
|
#ifdef ARDUINOMQ2 |
|
switch (v) { |
|
case 0: |
|
return 1; |
|
case 1: |
|
(void) mq2.read(false); |
|
return mq2.readLPG();; |
|
case 2: |
|
(void) mq2.read(false); |
|
return mq2.readCO(); |
|
case 3: |
|
(void) mq2.read(false); |
|
return mq2.readSmoke(); |
|
} |
|
#endif |
|
return 0; |
|
case 4: |
|
#ifdef ARDUINOAHT |
|
sensors_event_t humidity, temp; |
|
switch (v) { |
|
case 0: |
|
return 1; |
|
case 1: |
|
aht.getEvent(&humidity, &temp); |
|
return temp.temperature; |
|
case 2: |
|
aht.getEvent(&humidity, &temp); |
|
return humidity.relative_humidity; |
|
} |
|
#endif |
|
return 0; |
|
case 5: |
|
#ifdef ARDUINOBMP280 |
|
switch (v) { |
|
case 0: |
|
return 1; |
|
case 1: |
|
return bmp.readTemperature(); |
|
case 2: |
|
return bmp.readPressure() / 100.0; |
|
case 3: |
|
return bmp.readAltitude(1013.25); |
|
} |
|
#endif |
|
return 0; |
|
case 6: |
|
#ifdef ARDUINOBME280 |
|
switch (v) { |
|
case 0: |
|
return 1; |
|
case 1: |
|
return bme.readTemperature(); |
|
case 2: |
|
return bme.readPressure() / 100.0; |
|
case 3: |
|
return bme.readAltitude(1013.25); |
|
case 4: |
|
return bme.readHumidity(); |
|
} |
|
#endif |
|
|
|
return 0; |
|
default: |
|
return 0; |
|
} |
|
} |
|
|
|
#else |
|
void sensorbegin() {} |
|
float sensorread(uint8_t s, uint8_t v) {return 0;}; |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOINTERRUPTS |
|
uint8_t pintointerrupt(uint8_t pin) { return digitalPinToInterrupt(pin); } |
|
void attachinterrupt(uint8_t interrupt, void (*f)(), uint8_t mode) { attachInterrupt(interrupt, f, (PinStatus) mode); }; |
|
void detachinterrupt(uint8_t pin) { detachInterrupt(digitalPinToInterrupt(pin)); }; |
|
#else |
|
uint8_t pintointerrupt(uint8_t pin) { return 0; } |
|
void attachinterrupt(uint8_t interrupt, void (*f)(), uint8_t mode) { }; |
|
void detachinterrupt(uint8_t pin) { }; |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINOSPIRAM |
|
|
|
|
|
|
|
|
|
|
|
#ifndef RAMPIN |
|
#define RAMPIN SS |
|
#endif |
|
|
|
#define SPIRAMWRMR 1 |
|
#define SPIRAMRDMR 5 |
|
#define SPIRAMREAD 3 |
|
#define SPIRAMWRITE 2 |
|
#define SPIRAMRSTIO 0xFF |
|
#define SPIRAMSEQ 0x40 |
|
#define SPIRAMBYTE 0x00 |
|
|
|
|
|
uint16_t spirambegin() { |
|
pinMode(RAMPIN, OUTPUT); |
|
digitalWrite(RAMPIN, LOW); |
|
SPI.transfer(SPIRAMRSTIO); |
|
SPI.transfer(SPIRAMWRMR); |
|
|
|
SPI.transfer(SPIRAMSEQ); |
|
digitalWrite(RAMPIN, HIGH); |
|
return 65535; |
|
} |
|
|
|
|
|
int8_t spiramrawread(uint16_t a) { |
|
uint8_t c; |
|
digitalWrite(RAMPIN, LOW); |
|
SPI.transfer(SPIRAMREAD); |
|
SPI.transfer((uint8_t)(a >> 8)); |
|
SPI.transfer((uint8_t)a); |
|
c = SPI.transfer(0x00); |
|
digitalWrite(RAMPIN, HIGH); |
|
return c; |
|
} |
|
|
|
|
|
|
|
uint16_t spiram_robufferaddr = 0; |
|
uint8_t spiram_robuffervalid=0; |
|
const uint8_t spiram_robuffersize = 32; |
|
int8_t spiram_robuffer[spiram_robuffersize]; |
|
|
|
|
|
|
|
uint16_t spiram_rwbufferaddr = 0; |
|
uint8_t spiram_rwbuffervalid=0; |
|
const uint8_t spiram_rwbuffersize = 32; |
|
int8_t spiram_rwbuffer[spiram_rwbuffersize]; |
|
uint8_t spiram_rwbufferclean = 1; |
|
|
|
const uint16_t spiram_addrmask=0xffe0; |
|
|
|
|
|
|
|
|
|
void spiram_bufferread(uint16_t a, int8_t* b, uint16_t l) { |
|
digitalWrite(RAMPIN, LOW); |
|
SPI.transfer(SPIRAMREAD); |
|
SPI.transfer((uint8_t)(a >> 8)); |
|
SPI.transfer((uint8_t)a); |
|
SPI.transfer(b, l); |
|
digitalWrite(RAMPIN, HIGH); |
|
} |
|
|
|
void spiram_bufferwrite(uint16_t a, int8_t* b, uint16_t l) { |
|
digitalWrite(RAMPIN, LOW); |
|
SPI.transfer(SPIRAMWRITE); |
|
SPI.transfer((uint8_t)(a >> 8)); |
|
SPI.transfer((uint8_t)a); |
|
SPI.transfer(b, l); |
|
digitalWrite(RAMPIN, HIGH); |
|
} |
|
|
|
int8_t spiram_robufferread(uint16_t a) { |
|
|
|
if (spiram_rwbuffervalid && ((a & spiram_addrmask) == spiram_rwbufferaddr)) { |
|
return spiram_rwbuffer[a-spiram_rwbufferaddr]; |
|
} |
|
|
|
|
|
if (!spiram_robuffervalid || a >= spiram_robufferaddr + spiram_robuffersize || a < spiram_robufferaddr ) { |
|
spiram_bufferread(a, spiram_robuffer, spiram_robuffersize); |
|
spiram_robufferaddr=a; |
|
spiram_robuffervalid=1; |
|
} |
|
return spiram_robuffer[a-spiram_robufferaddr]; |
|
} |
|
|
|
|
|
void spiram_rwbufferflush() { |
|
if (!spiram_rwbufferclean) { |
|
spiram_bufferwrite(spiram_rwbufferaddr, spiram_rwbuffer, spiram_rwbuffersize); |
|
spiram_rwbufferclean=1; |
|
} |
|
} |
|
|
|
int8_t spiram_rwbufferread(uint16_t a) { |
|
|
|
uint16_t p=a & spiram_addrmask; |
|
if (!spiram_rwbuffervalid || (p != spiram_rwbufferaddr)) { |
|
|
|
spiram_rwbufferflush(); |
|
|
|
spiram_bufferread(p, spiram_rwbuffer, spiram_rwbuffersize); |
|
spiram_rwbufferaddr=p; |
|
spiram_rwbuffervalid=1; |
|
} |
|
return spiram_rwbuffer[a-spiram_rwbufferaddr]; |
|
} |
|
|
|
|
|
void spiram_rwbufferwrite(uint16_t a, int8_t c) { |
|
uint16_t p=a&spiram_addrmask; |
|
|
|
if (spiram_robuffervalid && a >= spiram_robufferaddr && a < spiram_robufferaddr + spiram_robuffersize) { |
|
spiram_robuffer[a-spiram_robufferaddr]=c; |
|
} |
|
|
|
if (spiram_rwbuffervalid && p == spiram_rwbufferaddr) { |
|
spiram_rwbuffer[a-spiram_rwbufferaddr]=c; |
|
spiram_rwbufferclean=0; |
|
return; |
|
} |
|
|
|
spiram_rwbufferflush(); |
|
(void) spiram_rwbufferread(a); |
|
spiram_rwbuffer[a-spiram_rwbufferaddr]=c; |
|
spiram_rwbufferclean=0; |
|
} |
|
|
|
|
|
void spiramrawwrite(uint16_t a, int8_t c) { |
|
digitalWrite(RAMPIN, LOW); |
|
SPI.transfer(SPIRAMWRITE); |
|
SPI.transfer((uint8_t)(a >> 8)); |
|
SPI.transfer((uint8_t)a); |
|
SPI.transfer((uint8_t) c); |
|
digitalWrite(RAMPIN, HIGH); |
|
|
|
if (a >= spiram_robufferaddr && a < spiram_robufferaddr + spiram_robuffersize && spiram_robufferaddr > 0) |
|
spiram_robuffer[a-spiram_robufferaddr]=c; |
|
|
|
if (a >= spiram_rwbufferaddr && a < spiram_rwbufferaddr + spiram_rwbuffersize && spiram_rwbufferaddr > 0) |
|
spiram_rwbuffer[a-spiram_rwbufferaddr]=c; |
|
} |
|
#endif |
|
|
|
#if defined(USEMEMINTERFACE) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ARDUINO_AVR_MEGA2560 |
|
#define SPIRAMSBSIZE 512 |
|
#else |
|
#define SPIRAMSBSIZE 128 |
|
#endif |
|
|
|
|
|
char spistrbuf1[SPIRAMSBSIZE]; |
|
char spistrbuf2[SPIRAMSBSIZE]; |
|
#endif |
|
|