|
| 1 | +# Arduino core for ESP8266, with multi-threading |
| 2 | + |
| 3 | +WARNING! This is a clone of the official Arduino core for the ESP8266, with |
| 4 | +some highly experimental cooperative multi-threading support. It will likely |
| 5 | +make fun of you while you try to figure out why it doesn't work. |
| 6 | + |
| 7 | +### Motivation |
| 8 | +Multi-threading can make it easier to write and maintain code, by avoiding |
| 9 | +complex state machines. There are some libraries out there for simulating this, |
| 10 | +but they typically don't have separate stacks, or they don't address the issue |
| 11 | +with blocking calls in the ESP8266 WiFi libraries. Since I had some spare time, |
| 12 | +and the support basically was already there in the core, I made a crude attempt |
| 13 | +to expose it. |
| 14 | + |
| 15 | +### How it works |
| 16 | +This is a case of cooperative multi-threading, meaning each thread either need |
| 17 | +to exit its loop regularly, or call one of the `yield()` or `delay(ms)` |
| 18 | +functions, to let the other threads run. All threads have the same priority and |
| 19 | +are scheduled in a round-robin fashion. If a thread function exit it will be |
| 20 | +invoked again later, just like the regular `loop()` function. There is no way |
| 21 | +to terminate threads. |
| 22 | + |
| 23 | +The stack for each new thread is allocated on the heap and is by default 1024 |
| 24 | +bytes. See `ESP8266Scheduler.h` for the function signature. |
| 25 | + |
| 26 | +In addition to the thread support, a number of library call sites have been |
| 27 | +modified to avoid `esp_yield()` and just call `yield()` in a loop instead, |
| 28 | +to avoid the blocking behavior. One should note this enables (accidental) |
| 29 | +concurrent access to the libraries, which may be TOTALLY UNSAFE. It's probably |
| 30 | +best if you keep your threads responsibilities completely separate. |
| 31 | + |
| 32 | +### Example |
| 33 | +``` |
| 34 | +#include <ESP8266Scheduler.h> |
| 35 | +
|
| 36 | +void serialLoop() { |
| 37 | + int num = 0; |
| 38 | + while (true) { |
| 39 | + Serial.printf("The number is %d\n", num++); |
| 40 | + delay(1000); |
| 41 | + } |
| 42 | +} |
| 43 | +
|
| 44 | +void setup() { |
| 45 | + pinMode(LED_BUILTIN, OUTPUT); |
| 46 | + Serial.begin(115200); |
| 47 | + Scheduler.startLoop(serialLoop); |
| 48 | +} |
| 49 | +
|
| 50 | +void loop() { |
| 51 | + digitalWrite(LED_BUILTIN, HIGH); |
| 52 | + delay(1000); |
| 53 | + digitalWrite(LED_BUILTIN, LOW); |
| 54 | + delay(1000); |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +### Installation |
| 59 | +Since this is a modified version of the original core, you need to remove that |
| 60 | +one if you have it installed and then install this one instead. Don't expect |
| 61 | +any support on it though, especially not from the original authors. |
| 62 | + |
| 63 | +### Performance |
| 64 | +Haven't checked; probably horrible. |
| 65 | + |
| 66 | +### Known issues |
| 67 | +- Using ArduinoOTA is currently not super-safe since it doesn't suspend other threads, but it should not be that hard to fix. |
| 68 | +- Since `delay()` has been replaced with `yield()` loops, the CPU will never enter any low power states. Unsure if it did that before though. |
| 69 | + |
| 70 | +Original README follows below. |
| 71 | + |
1 | 72 | Arduino core for ESP8266 WiFi chip
|
2 | 73 | ===========================================
|
3 | 74 |
|
|
0 commit comments