diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/Nanpy/ArduinoClass.cpp b/Nanpy/ArduinoClass.cpp index ba86a41..c484a12 100644 --- a/Nanpy/ArduinoClass.cpp +++ b/Nanpy/ArduinoClass.cpp @@ -17,18 +17,23 @@ void nanpy::ArduinoClass::elaborate( nanpy::MethodDescriptor* m ) { if (strcmp(m->getName(), "r") == 0) { // digitalRead m->returns(digitalRead(m->getInt(0))); } - +#ifndef ESP32 if (strcmp(m->getName(), "aw") == 0) { // analogWrite analogWrite(m->getInt(0), m->getInt(1)); m->returns(0); } +#endif if (strcmp(m->getName(), "a") == 0) { // analogRead m->returns(analogRead(m->getInt(0))); } if (strcmp(m->getName(), "pm") == 0) { // pinMode - pinMode(m->getInt(0), m->getInt(1)); + int _mode = m->getInt(1); + #ifdef ESP32 + _mode++; + #endif + pinMode(m->getInt(0), _mode); m->returns(0); } diff --git a/Nanpy/ArduinoCoreClass.h b/Nanpy/ArduinoCoreClass.h index df1def5..25af72f 100644 --- a/Nanpy/ArduinoCoreClass.h +++ b/Nanpy/ArduinoCoreClass.h @@ -12,4 +12,3 @@ namespace nanpy { }; }; - diff --git a/CONTRIBUTORS b/Nanpy/CONTRIBUTORS similarity index 100% rename from CONTRIBUTORS rename to Nanpy/CONTRIBUTORS diff --git a/Nanpy/ComChannel.cpp b/Nanpy/ComChannel.cpp index 419047e..174c0bc 100644 --- a/Nanpy/ComChannel.cpp +++ b/Nanpy/ComChannel.cpp @@ -3,21 +3,34 @@ #if USE_WIFI_CONNECTION +#ifdef ESP8266 #include #include +#else +#ifdef ESP32 +#include +#include +#endif +#endif -#define DNS_NAME "nanpy" +#define DNS_NAME "mudpi" #define SERVER_PORT 5678 #define SerialDebug Serial -#define TIMEOUT 10 +#define TIMEOUT 50 WiFiServer server(SERVER_PORT); WiFiClient client; +unsigned long currentTime = millis(); +unsigned long previousTime = 0; +const long timeoutTime = 10000; + void nanpy::ComChannel::connect() { SerialDebug.begin(115200); SerialDebug.println(""); + WiFi.disconnect(); //Flush WiFi + delay(2000); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); SerialDebug.print("\nConnecting to "); SerialDebug.println(WIFI_SSID); @@ -63,17 +76,29 @@ void nanpy::ComChannel::connect() bool nanpy::ComChannel::available() { - if (!client.connected()) - { - // try to connect to a new client - client = server.available(); - if (client.connected()) - SerialDebug.println("client connected"); - } + currentTime = millis(); + if (previousTime == 0) { + previousTime = currentTime; + } + if(client) { + previousTime = currentTime; + // read data from the connected client + if (client.connected() && currentTime - previousTime <= timeoutTime) { + previousTime = currentTime; + return (client.available() > 0); + } + else { + client.stop(); + Serial.println("Client Disconnected."); + } + } else { - // read data from the connected client - return (client.available() > 0); + // try to connect to a new client + client = server.available(); + if (client.connected()) { + SerialDebug.println("Client connected"); + } } return false; } @@ -81,20 +106,24 @@ bool nanpy::ComChannel::available() char nanpy::ComChannel::read_buffer[MAX_READ_BUFFER_SIZE] = {0}; void readLineFromSerial(char* extbuff) { - int i=0; - char ch = '0'; - char* buff = nanpy::ComChannel::read_buffer; - do { - ch = client.read(); - if(ch < 255 && ch >= 0) { - buff[i++] = ch; - } - if (i == MAX_READ_BUFFER_SIZE - 1) { - buff[i] = '\0'; - break; - } - } while(ch != '\0'); - strcpy(extbuff, buff); + if(client) { + if(client.connected()) { + int i=0; + char ch = '0'; + char* buff = nanpy::ComChannel::read_buffer; + do { + ch = client.read(); + if(ch < 255 && ch >= 0) { + buff[i++] = ch; + } + if (i == MAX_READ_BUFFER_SIZE - 1) { + buff[i] = '\0'; + break; + } + } while(ch != '\0'); + strcpy(extbuff, buff); + } + } }; void nanpy::ComChannel::readLine(char* extbuff) { diff --git a/Nanpy/ComChannel.h b/Nanpy/ComChannel.h index 687fb4a..f6cc480 100644 --- a/Nanpy/ComChannel.h +++ b/Nanpy/ComChannel.h @@ -26,4 +26,3 @@ namespace nanpy { } #endif - diff --git a/Nanpy/CounterClass.h b/Nanpy/CounterClass.h index d148990..8a145c0 100644 --- a/Nanpy/CounterClass.h +++ b/Nanpy/CounterClass.h @@ -12,4 +12,3 @@ namespace nanpy { }; }; - diff --git a/Nanpy/DHTClass.cpp b/Nanpy/DHTClass.cpp index a052060..6416895 100644 --- a/Nanpy/DHTClass.cpp +++ b/Nanpy/DHTClass.cpp @@ -18,9 +18,9 @@ void nanpy::DHTClass::elaborate( MethodDescriptor* m ) { if (strcmp(m->getName(),"new") == 0) { DHT* dht; if (m->getNArgs() == 3) { - dht = new DHT (m->getInt(0), m->getInt(1), m->getInt(2)); + dht = new DHT (m->getInt(0), m->getInt(1), m->getInt(2)); } else { - dht = new DHT (m->getInt(0), m->getInt(1)); + dht = new DHT (m->getInt(0), m->getInt(1)); } dht->begin(); v.insert(dht); diff --git a/Nanpy/DefineClass.h b/Nanpy/DefineClass.h index 3cd7d0a..bc4433e 100644 --- a/Nanpy/DefineClass.h +++ b/Nanpy/DefineClass.h @@ -12,4 +12,3 @@ namespace nanpy { }; }; - diff --git a/Nanpy/EspClass.cpp b/Nanpy/EspClass.cpp index 5293bc7..92a7ef8 100644 --- a/Nanpy/EspClass.cpp +++ b/Nanpy/EspClass.cpp @@ -117,4 +117,121 @@ void nanpy::EspClass::elaborate( nanpy::MethodDescriptor* m ) { } }; +#else +#ifdef ESP32 +#include "EspClass.h" +#include + +const char* nanpy::EspClass::get_firmware_id() +{ + return "Esp"; +} +//https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/Esp.cpp +void nanpy::EspClass::elaborate( nanpy::MethodDescriptor* m ) { +// if (strcmp(m->getName(), "wdtEnable") == 0) { +// ESP.wdtEnable(100); +// m->returns(0); +// } +// if (strcmp(m->getName(), "wdtDisable") == 0) { +// ESP.wdtDisable(); +// m->returns(0); +// } +// +// if (strcmp(m->getName(), "wdtFeed") == 0) { +// ESP.wdtFeed(); +// m->returns(0); +// } +// if (strcmp(m->getName(), "deepSleep") == 0) { +// ESP.deepSleep(); +// m->returns(0); +// } + if (strcmp(m->getName(), "reset") == 0) { + ESP.restart(); + m->returns(0); + } + if (strcmp(m->getName(), "restart") == 0) { + ESP.restart(); + m->returns(0); + } +// if (strcmp(m->getName(), "getVcc") == 0) { +//// m->returns(ESP.getVcc()); +// } + if (strcmp(m->getName(), "getFreeHeap") == 0) { + m->returns(ESP.getFreeHeap()); + } + if (strcmp(m->getName(), "getChipId") == 0) { + m->returns(ESP.getChipRevision()); + } + if (strcmp(m->getName(), "getSdkVersion") == 0) { + m->returns(ESP.getSdkVersion()); + } +// if (strcmp(m->getName(), "getBootVersion") == 0) { +//// m->returns(ESP.getBootVersion()); +// } +// if (strcmp(m->getName(), "getBootMode") == 0) { +//// m->returns(ESP.getFlashChipMode()); +// } + if (strcmp(m->getName(), "getCpuFreqMHz") == 0) { + m->returns("80MHZ"); + } +// if (strcmp(m->getName(), "getFlashChipId") == 0) { +// m->returns(ESP.getFlashChipId()); +// } +// if (strcmp(m->getName(), "getFlashChipRealSize") == 0) { +// m->returns(ESP.getFlashChipRealSize()); +// } + + if (strcmp(m->getName(), "getFlashChipSize") == 0) { + m->returns(ESP.getFlashChipSize()); + } + if (strcmp(m->getName(), "getFlashChipSpeed") == 0) { + m->returns(ESP.getFlashChipSpeed()); + } + if (strcmp(m->getName(), "getFlashChipMode") == 0) { + m->returns(ESP.getFlashChipMode()); + } +// if (strcmp(m->getName(), "magicFlashChipSize") == 0) { +// m->returns(ESP.magicFlashChipSize()); +// } +// if (strcmp(m->getName(), "magicFlashChipSpeed") == 0) { +// m->returns(ESP.magicFlashChipSpeed()); +// } +// if (strcmp(m->getName(), "magicFlashChipMode") == 0) { +// m->returns(ESP.magicFlashChipMode()); +// } +// if (strcmp(m->getName(), "getFlashChipSizeByChipId") == 0) { +// m->returns(ESP.getFlashChipSizeByChipId()); +// } +// if (strcmp(m->getName(), "checkFlashConfig") == 0) { +// m->returns(ESP.checkFlashConfig()); +// } +// if (strcmp(m->getName(), "getResetReason") == 0) { +// String s = ESP.getResetReason(); +// m->returns(s); +// } +// if (strcmp(m->getName(), "getResetInfo") == 0) { +// String s = ESP.getResetInfo(); +// m->returns(s); +// } + if (strcmp(m->getName(), "getSketchSize") == 0) { + m->returns(ESP.getSketchSize()); + } + if (strcmp(m->getName(), "getFreeSketchSpace") == 0) { + m->returns(ESP.getFreeSketchSpace()); + } + if (strcmp(m->getName(), "flashEraseSector") == 0) { + m->returns(ESP.flashEraseSector(m->getInt(0))); + } + if (strcmp(m->getName(), "flashWrite") == 0) { + uint32_t data = m->getInt(1); + m->returns(ESP.flashWrite(m->getInt(0), &data, 1)); + } + if (strcmp(m->getName(), "flashRead") == 0) { + uint32_t data; + ESP.flashRead(m->getInt(0), &data, 1); + m->returns(data); + } +}; + +#endif #endif diff --git a/Nanpy/FreqCount.h b/Nanpy/FreqCount.h index dc74526..8db9663 100644 --- a/Nanpy/FreqCount.h +++ b/Nanpy/FreqCount.h @@ -11,4 +11,3 @@ class FreqCountClass { }; extern FreqCountClass FreqCount; - diff --git a/Nanpy/InfoClass.h b/Nanpy/InfoClass.h index 7f9dce2..3f31212 100644 --- a/Nanpy/InfoClass.h +++ b/Nanpy/InfoClass.h @@ -12,4 +12,3 @@ namespace nanpy { }; }; - diff --git a/LICENSE b/Nanpy/LICENSE similarity index 100% rename from LICENSE rename to Nanpy/LICENSE diff --git a/Nanpy/MethodDescriptor.cpp b/Nanpy/MethodDescriptor.cpp index 5a389b9..a2850de 100644 --- a/Nanpy/MethodDescriptor.cpp +++ b/Nanpy/MethodDescriptor.cpp @@ -125,4 +125,3 @@ void nanpy::MethodDescriptor::returns(long val) { void nanpy::MethodDescriptor::returns(unsigned long val) { ComChannel::println(val); } - diff --git a/README.md b/Nanpy/README.md similarity index 100% rename from README.md rename to Nanpy/README.md diff --git a/Nanpy/RegisterClass.cpp b/Nanpy/RegisterClass.cpp index 6468562..4223c58 100644 --- a/Nanpy/RegisterClass.cpp +++ b/Nanpy/RegisterClass.cpp @@ -110,4 +110,3 @@ void nanpy::RegisterClass::elaborate(nanpy::MethodDescriptor* m) } #endif - diff --git a/Nanpy/RegisterClass.h b/Nanpy/RegisterClass.h index d30bb9c..d151798 100644 --- a/Nanpy/RegisterClass.h +++ b/Nanpy/RegisterClass.h @@ -11,4 +11,3 @@ namespace nanpy { }; }; - diff --git a/Nanpy/ServoClass.cpp b/Nanpy/ServoClass.cpp index b28f1a1..01425c9 100644 --- a/Nanpy/ServoClass.cpp +++ b/Nanpy/ServoClass.cpp @@ -50,4 +50,3 @@ void nanpy::ServoClass::elaborate( nanpy::MethodDescriptor* m ) { } #endif - diff --git a/Nanpy/cfg_check.h b/Nanpy/cfg_check.h index 9729e90..ca8b1aa 100644 --- a/Nanpy/cfg_check.h +++ b/Nanpy/cfg_check.h @@ -5,14 +5,14 @@ #ifndef ARDUINO_ARCH_AVR // disable AVR only features #define USE_ArduinoCore 0 -//#define USE_RAM 0 +//#define USE_RAM 0 #define USE_EEPROM 0 #define USE_Register 0 #define USE_Watchdog 0 #define USE_Counter 0 #define USE_DHT 0 #define USE_LiquidCrystal_I2C 0 -#define USE_LiquidCrystal 0 +#define USE_LiquidCrystal 0 #endif // check configuration for problems @@ -28,11 +28,17 @@ #ifdef ESP8266 # define USE_ESP 1 #else +#ifdef ESP32 +# define USE_ESP_32 1 +# define USE_ESP 1 +# define USE_DHT 1 +#else # define USE_ESP 0 #endif +#endif #ifndef ESP8266 +#ifndef ESP32 # define USE_WIFI_CONNECTION 0 #endif - - +#endif diff --git a/Nanpy/cfg_defaults.h b/Nanpy/cfg_defaults.h index 38e8dba..3e835f0 100644 --- a/Nanpy/cfg_defaults.h +++ b/Nanpy/cfg_defaults.h @@ -81,7 +81,7 @@ #endif #ifndef USE_DHT -# define USE_DHT 0 +# define USE_DHT 1 #endif #ifndef USE_LiquidCrystal_I2C diff --git a/Nanpy/consts.h b/Nanpy/consts.h index c647711..3cd1b46 100644 --- a/Nanpy/consts.h +++ b/Nanpy/consts.h @@ -1,4 +1,3 @@ #pragma once #define MAX_READ_BUFFER_SIZE 50 - diff --git a/Nanpy/freeram.c b/Nanpy/freeram.c index 48cefe8..9056f39 100644 --- a/Nanpy/freeram.c +++ b/Nanpy/freeram.c @@ -62,4 +62,4 @@ int free_ram3() return size; } -#endif \ No newline at end of file +#endif diff --git a/Nanpy/freeram.h b/Nanpy/freeram.h index c0ba485..f1dba7a 100644 --- a/Nanpy/freeram.h +++ b/Nanpy/freeram.h @@ -12,4 +12,3 @@ int free_ram3(); #ifdef __cplusplus } #endif - diff --git a/Nanpy/generated_avr_registers.h b/Nanpy/generated_avr_registers.h index caf238d..dce2e9b 100644 --- a/Nanpy/generated_avr_registers.h +++ b/Nanpy/generated_avr_registers.h @@ -13761,4 +13761,4 @@ DEFINE(XOSC_CTRL) #else MISSING(XOSC_CTRL) -#endif \ No newline at end of file +#endif diff --git a/Nanpy/generated_mcu.h b/Nanpy/generated_mcu.h index dfce6ac..69ac374 100644 --- a/Nanpy/generated_mcu.h +++ b/Nanpy/generated_mcu.h @@ -1284,4 +1284,4 @@ # error "MCU is already defined" # endif # define MCU "__AVR_ATxmega64D3__" -#endif \ No newline at end of file +#endif diff --git a/Nanpy/intdefs.h b/Nanpy/intdefs.h index bf66de1..d1e9215 100644 --- a/Nanpy/intdefs.h +++ b/Nanpy/intdefs.h @@ -9,5 +9,3 @@ DEFINE(A0) DEFINE(MISO) DEFINE(SCK) #endif - -