Dependencies:   ESP-call

Committer:
dshin
Date:
Mon May 23 05:22:16 2022 +0000
Revision:
0:bd4e93ef863c
lab07-simple-timer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dshin 0:bd4e93ef863c 1 // ======================================================================
dshin 0:bd4e93ef863c 2
dshin 0:bd4e93ef863c 3 #include "mbed.h"
dshin 0:bd4e93ef863c 4 #include "ESP-call.h"
dshin 0:bd4e93ef863c 5
dshin 0:bd4e93ef863c 6 // ======================================================================
dshin 0:bd4e93ef863c 7 // NodeMCU program
dshin 0:bd4e93ef863c 8
dshin 0:bd4e93ef863c 9 NodeMCU_PGM simple_timer[] = {
dshin 0:bd4e93ef863c 10 "", 1,
dshin 0:bd4e93ef863c 11 "delayms = 500", 1,
dshin 0:bd4e93ef863c 12 "", 1,
dshin 0:bd4e93ef863c 13 "LEDpin = 4", 1,
dshin 0:bd4e93ef863c 14 "LEDstate = 0", 1,
dshin 0:bd4e93ef863c 15 "", 1,
dshin 0:bd4e93ef863c 16 "gpio.mode(LEDpin,gpio.OUTPUT)", 1,
dshin 0:bd4e93ef863c 17 "", 1,
dshin 0:bd4e93ef863c 18 "function toggle()", 1,
dshin 0:bd4e93ef863c 19 " if (LEDstate) then gpio.write(LEDpin,gpio.LOW)", 1,
dshin 0:bd4e93ef863c 20 " else gpio.write(LEDpin,gpio.HIGH)", 1,
dshin 0:bd4e93ef863c 21 " end", 1,
dshin 0:bd4e93ef863c 22 " LEDstate = not LEDstate", 1,
dshin 0:bd4e93ef863c 23 "end", 1,
dshin 0:bd4e93ef863c 24 "", 1,
dshin 0:bd4e93ef863c 25 "mytimer = tmr.create()", 1,
dshin 0:bd4e93ef863c 26 "mytimer:register(delayms, tmr.ALARM_AUTO, function () toggle() end)", 1,
dshin 0:bd4e93ef863c 27 "mytimer:start()", 1,
dshin 0:bd4e93ef863c 28 NULL, 0, // last line should be NULL and 0
dshin 0:bd4e93ef863c 29 };
dshin 0:bd4e93ef863c 30
dshin 0:bd4e93ef863c 31 // ======================================================================
dshin 0:bd4e93ef863c 32 // Main thread
dshin 0:bd4e93ef863c 33
dshin 0:bd4e93ef863c 34 int main()
dshin 0:bd4e93ef863c 35 {
dshin 0:bd4e93ef863c 36 int i;
dshin 0:bd4e93ef863c 37 char *p;
dshin 0:bd4e93ef863c 38
dshin 0:bd4e93ef863c 39 // Config baudrate of PC and ESP
dshin 0:bd4e93ef863c 40 PC.baud(115200);
dshin 0:bd4e93ef863c 41 ESP.baud(115200);
dshin 0:bd4e93ef863c 42
dshin 0:bd4e93ef863c 43 // Reset ESP
dshin 0:bd4e93ef863c 44 PC.printf("\r\nReset ESP...\r\n");
dshin 0:bd4e93ef863c 45 ESP_reset();
dshin 0:bd4e93ef863c 46
dshin 0:bd4e93ef863c 47 // Setup ESP noecho mode
dshin 0:bd4e93ef863c 48 PC.printf("Setup ESP noecho...\r\n");
dshin 0:bd4e93ef863c 49 ESP_noecho();
dshin 0:bd4e93ef863c 50
dshin 0:bd4e93ef863c 51 // Execute a NodeMCU program
dshin 0:bd4e93ef863c 52 PC.printf("Execute a NodeMCU program...\r\n");
dshin 0:bd4e93ef863c 53 ESP_call_multi(simple_timer);
dshin 0:bd4e93ef863c 54
dshin 0:bd4e93ef863c 55 // Setup ESP echo mode
dshin 0:bd4e93ef863c 56 PC.printf("\r\nSetup ESP echo...\r\n");
dshin 0:bd4e93ef863c 57 ESP_echo();
dshin 0:bd4e93ef863c 58
dshin 0:bd4e93ef863c 59 // Config ESP passthrough mode
dshin 0:bd4e93ef863c 60 PC.printf("ESP passthrough mode...\r\n");
dshin 0:bd4e93ef863c 61 PC.attach(&ISR_PC_to_ESP, Serial::RxIrq);
dshin 0:bd4e93ef863c 62 ESP.attach(&ISR_ESP_to_PC, Serial::RxIrq);
dshin 0:bd4e93ef863c 63
dshin 0:bd4e93ef863c 64 // Main thread sleeps
dshin 0:bd4e93ef863c 65 while(1) {
dshin 0:bd4e93ef863c 66 sleep();
dshin 0:bd4e93ef863c 67 }
dshin 0:bd4e93ef863c 68 }
dshin 0:bd4e93ef863c 69
dshin 0:bd4e93ef863c 70 // ======================================================================