commit 6e444c4dcacafbd696c8698a0a19a35e48738753 Author: FabiClawZ Date: Sat May 30 14:37:04 2026 +0200 Initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..03f4a3c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.pio diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..30cf57e --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/editor.xml b/.idea/editor.xml new file mode 100644 index 0000000..8d0e15e --- /dev/null +++ b/.idea/editor.xml @@ -0,0 +1,345 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d858eb1 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,17 @@ + + + + + + + + \ No newline at end of file diff --git a/include/README b/include/README new file mode 100644 index 0000000..49819c0 --- /dev/null +++ b/include/README @@ -0,0 +1,37 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the convention is to give header files names that end with `.h'. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..9379397 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into the executable file. + +The source code of each library should be placed in a separate directory +("lib/your_library_name/[Code]"). + +For example, see the structure of the following example libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +Example contents of `src/main.c` using Foo and Bar: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +The PlatformIO Library Dependency Finder will find automatically dependent +libraries by scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..a6961b7 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,23 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32dev] +platform = espressif32 +board = esp32dev +framework = arduino +monitor_speed = 115200 +upload_speed = 921600 +lib_deps = + https://github.com/kapraran/FreqCountESP/archive/refs/heads/master.zip + bodmer/TFT_eSPI@^2.5.43 + bodmer/TFT_eWidget@^0.0.6 + adafruit/Adafruit SSD1306@^2.5.16 + adafruit/Adafruit GFX Library@^1.12.5 + diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..9cfa5a1 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,243 @@ +#include +#include +#include +#include +#include + +uint32_t freq = 0; +uint32_t limitFreq = 9000; + +#define SCREEN_WIDTH 128 // OLED display width, in pixels +#define SCREEN_HEIGHT 32 // OLED display height, in pixels +#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) +#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 +uint8_t INJECT_PIN = 16; + +WiFiServer server(80); +IPAddress local_IP(10,0,0,1); +IPAddress gateway(10, 0, 0, 1); +IPAddress subnet(255, 255, 255, 0); +IPAddress dns1(1, 1, 1, 1); +IPAddress dns2(8, 8, 8, 8); + +const char* ssid = "ESP32-Tach"; +const char* password = "Tacho123"; +String header; + +const byte slots = 1; // no of slots in a rotating object +volatile unsigned long us, prevPulseUs, pulseUs, prevPulseUsCopy, pulseUsCopy, ignitionCnt; +volatile bool overspeed, limitActive = true; +unsigned long prevMs, now, pulsePeriod, rpm, durationWrite; +const unsigned long slotUs = 60000000 / slots; + +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); + +void IRAM_ATTR isr() { + us = micros(); + overspeed = false; + if ((us - pulseUs) > 1000 && (us-pulseUs) > 0.5*(pulseUs-prevPulseUs)) { // debounce interval, also determines max rpm + prevPulseUs = pulseUs; + pulseUs = us; + ignitionCnt++; + if (limitActive && pulseUs - prevPulseUs < 60000000/limitFreq) { + overspeed = true; + if (ignitionCnt % 3 == 0) { + digitalWrite(INJECT_PIN, HIGH); + ignitionCnt = 0; + delayMicroseconds((pulseUs-prevPulseUs)); + prevPulseUs = pulseUs; + pulseUs = micros(); + delayMicroseconds(20); + ignitionCnt++; + digitalWrite(INJECT_PIN, LOW); + }else { + delayMicroseconds(20); + digitalWrite(INJECT_PIN, LOW); + } + }else + if (limitActive && pulseUs - prevPulseUs < 60000000/limitFreq-250) { + overspeed = true; + if (ignitionCnt % 5 == 0) { + digitalWrite(INJECT_PIN, HIGH); + ignitionCnt = 0; + delayMicroseconds((pulseUs-prevPulseUs)); + prevPulseUs = pulseUs; + pulseUs = micros(); + delayMicroseconds(20); + ignitionCnt++; + digitalWrite(INJECT_PIN, LOW); + }else { + delayMicroseconds(20); + digitalWrite(INJECT_PIN, LOW); + } + }else + if (limitActive && pulseUs - prevPulseUs < 60000000/limitFreq-500) { + overspeed = true; + if (ignitionCnt % 7 == 0) { + digitalWrite(INJECT_PIN, HIGH); + ignitionCnt = 0; + delayMicroseconds((pulseUs-prevPulseUs)); + prevPulseUs = pulseUs; + pulseUs = micros(); + delayMicroseconds(20); + ignitionCnt++; + digitalWrite(INJECT_PIN, LOW); + }else { + delayMicroseconds(20); + digitalWrite(INJECT_PIN, LOW); + } + }else + if (limitActive &65& pulseUs - prevPulseUs < 60000000/(limitFreq-800)) { + overspeed = true; + if (ignitionCnt % 10 == 0) { + digitalWrite(INJECT_PIN, HIGH); + ignitionCnt = 0; + delayMicroseconds((pulseUs-prevPulseUs)); + prevPulseUs = pulseUs; + pulseUs = micros(); + delayMicroseconds(20); + ignitionCnt++; + digitalWrite(INJECT_PIN, LOW); + }else { + delayMicroseconds(20); + digitalWrite(INJECT_PIN, LOW); + } + }else if (!overspeed && ignitionCnt == 0) { + digitalWrite(INJECT_PIN, LOW); + } + } +} + +bool timeOut(unsigned long ms) { + now = millis(); + if ((now - prevMs) >= ms) { + prevMs = now; + return true; + } + return false; +} + +void setup() { +// write your initialization code here + Serial.begin(115200); + WiFi.mode(WIFI_MODE_AP); + WiFi.softAPConfig(local_IP, gateway, subnet); + WiFi.softAP(ssid, password); + server.begin(); + Wire.begin(21,22); + pinMode(INJECT_PIN, OUTPUT); + pinMode(15, INPUT_PULLUP); + // Wait for display + delay(500); + + unsigned long usWrite = micros(); + digitalWrite(INJECT_PIN, LOW); + durationWrite = micros() - usWrite; + Serial.println(F(" usWrite: ")); + Serial.println(durationWrite); + + delay(1000); + + // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally + if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { + Serial.println(F("SSD1306 allocation failed")); + } + + // Show initial display buffer contents on the screen -- + // the library initializes this with an Adafruit splash screen. + display.display(); + + //NPN Transistor vor GPIO um 1-2V Signal auf 3.3V zu bringen, signal invertiert also FALLING edge ist RISING im original Signal + attachInterrupt(digitalPinToInterrupt(15), isr, RISING); +} + +void loop() { + WiFiClient client = server.available(); + if (client) { + Serial.println(F("Client connected")); + + String currentLine = ""; // make a String to hold incoming data from the client + while (client.connected()) { // loop while the client's connected + if (client.available()) { // if there's bytes to read from the client, + char c = client.read(); // read a byte, then + Serial.write(c); // print it out the serial monitor + if (c == '\n') { // if the byte is a newline character + + // if the current line is blank, you got two newline characters in a row. + // that's the end of the client HTTP request, so send a response: + if (currentLine.length() == 0) { + // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) + // and a content-type so the client knows what's coming, then a blank line: + client.println("HTTP/1.1 200 OK"); + client.println("Content-type:text/html"); + client.println(); + + // the content of the HTTP response follows the header: + client.println(""); + client.println(""); + client.println(""); + client.println(R"()"); + client.println(R"()"); + client.println(""); + client.println(""); + client.println("

Tacho Einstellungen

"); + client.println(R"(Deaktivieren
)"); + client.println(R"(Aktivieren
)"); + client.println(R"(
)"); + client.println(R"()"); + client.println(R"()"); + client.println(R"(
)"); + client.println(""); + client.println(""); + + // The HTTP response ends with another blank line: + client.println(); + // break out of the while loop: + break; + } else { // if you got a newline, then clear currentLine: + currentLine = ""; + } + } else if (c != '\r') { // if you got anything else but a carriage return character, + currentLine += c; // add it to the end of the currentLine + } + + // Check to see if the client request was "GET /H" or "GET /L": + if (currentLine.endsWith("GET /disable")) { + limitActive = false; + } else if (currentLine.endsWith("GET /enable")) { + limitActive = true; + }else if (currentLine.indexOf("GET /limiter") >= 0) { + String newLimit = currentLine.substring(currentLine.indexOf('=') + 1); + limitFreq = newLimit.toInt(); + } + } + } + // close the connection: + client.stop(); + Serial.println("Client Disconnected."); + } + + if (timeOut(100)) { // 10 Hz update frequency + cli(); //stop interrupts + prevPulseUsCopy = prevPulseUs; + pulseUsCopy = pulseUs; + sei(); //allow interrupts + pulsePeriod = pulseUsCopy - prevPulseUsCopy; + if (pulsePeriod) rpm = slotUs / pulsePeriod; + Serial.print(F("freq rpm: ")); + Serial.println(rpm); + display.clearDisplay(); + display.setTextSize(2); + display.setTextColor(SSD1306_WHITE); + display.setCursor(40,0); + display.println(rpm); + display.setCursor(40, display.getCursorY()); + display.print(F("U/m")); + if (overspeed) { + display.print(F(" X")); + }else if (!limitActive) { + display.print(F(" UL")); + } + display.display(); + } +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html