Commit b4ff0355 authored by Nelso Jost's avatar Nelso Jost

DEV: refactor repl; ADD: wifi, software serial

parent 4fbbb322
PORT :=
BAUD := 115200
PIOBOARD = esp12e
PIOBOARD = nodemcuv2
PIOPROJ = firmware/esplogger
PIODIR = .pio
......@@ -9,17 +9,33 @@ PIODIR = .pio
all: firmware
install-pip3:
wget https://bootstrap.pypa.io/get-pip.py && \
sudo python3 get-pip.py && rm get-pip.py
install-platformio:
wget https://bootstrap.pypa.io/get-pip.py && \
sudo python get-pip.py && rm get-pip.py
sudo pip install platformio
$(MAKE) add-udev-rules
$(MAKE) add-dialout
add-dialout:
sudo usermod -a -G dialout $$USER
add-udevrules:
wget https://raw.githubusercontent.com/platformio/platformio/develop/scripts/99-platformio-udev.rules
sudo cp -v 99-platformio-udev.rules /etc/udev/rules.d
sudo service udev restart
firmware: build upload monitor
build:
which platformio || $(MAKE) install-platformio
@ echo "$$PIO_BUILD" | sh
upload:
which platformio || $(MAKE) install-platformio
cd $(PIODIR) && platformio run -t upload
monitor:
......
......@@ -4,12 +4,29 @@
* Purpose: Entry point for the Arduino Toolchain.
*---------------------------------------------------------------------------*/
#include <Arduino.h>
#include <Ticker.h>
#include "repl.h"
#include "repl_gpio.h"
#include "repl_sdcard.h"
#include "wifi.h"
#include <SoftwareSerial.h>
#define PIN_LED 0 // D3
#define PIN_BUTTON 2 // D4
#define ARDUINO_TX 5 // D1 (soft RX)
#define ARDUINO_RX 4 // D2 (soft TX)
void REPL_WIFISCAN(REPL * repl);
void REPL_WIFISTATUS(REPL * repl);
void REPL_WIFICONNECT(REPL * repl);
void REPL_ARDUINO(REPL * repl);
SoftwareSerial arduinoSerial(ARDUINO_TX, ARDUINO_RX); // RX, TX
REPL_COMMAND commands[] =\
{
......@@ -19,20 +36,50 @@ REPL_COMMAND commands[] =\
{"wifiscan", REPL_WIFISCAN, "wifiscan",
"List all availables SSID in range for connection."},
{"wifistatus", REPL_WIFISTATUS, "wifistatus",
"Print WiFi debug info (connection status)."},
{"wificonnect", REPL_WIFICONNECT, "wificonnect <ssid> <password>",
"Connect to wifi network."},
{"arduino", REPL_ARDUINO, "arduino <command>",
"Sendo command to arduino."},
{NULL}
};
REPL repl(commands);
int led_state = 0;
int button_state = 0;
Ticker timer_button;
void setup()
{
Serial.begin(9600);
repl.run();
timer_button.attach(0.1, toggle_led);
arduinoSerial.begin(9600);
}
void loop()
{
repl.update();
}
void toggle_led()
{
int reading = digitalRead(PIN_BUTTON);
if (button_state != reading)
{
button_state = reading;
if (button_state == HIGH)
{
led_state = !led_state;
pinMode(PIN_LED, OUTPUT);
digitalWrite(PIN_LED, led_state);
}
}
}
......@@ -41,6 +88,17 @@ void REPL_WIFISCAN(REPL * repl)
wifiscan();
}
void REPL_WIFISTATUS(REPL * repl)
{
wifistatus();
}
void REPL_WIFICONNECT(REPL * repl)
{
wificonnect(repl->get_arg(1), repl->get_arg(2), 10);
}
void REPL_ARDUINO(REPL * repl)
{
arduinoSerial.print("baka");
}
......@@ -15,12 +15,12 @@ REPL::REPL(REPL_COMMAND * command_map)
}
void REPL::run(void)
void REPL::update(void)
{
char c;
while (1)
{
while (!Serial.available()) { yield(); }
if (!Serial.available()) { yield(); return; }
c = Serial.read();
if (c == 8)
......@@ -35,6 +35,8 @@ void REPL::run(void)
else if (c == ']')
this->_history_next();
else if (c == 32 && !this->_command_buffer.length())
return;
else
{
this->_command_buffer += c;
......@@ -47,35 +49,51 @@ void REPL::run(void)
this->_command_buffer.indexOf("\n") != -1)
{
this->_command_buffer.trim();
if (this->_command_buffer == "help") this->print_help();
else
{
if (this->_command_buffer != "")
{
this->_execute_command();
if (this->_command_buffer == "help") this->print_help();
else this->_execute_command();
this->_command_buffer = "";
}
else
{
this->_command_buffer = " ";
}
}
if (c != 13) Serial.print(this->prompt + " ");
}
Serial.flush();
}
}
void REPL::_clear_line(void)
{
Serial.print("\r "
" "
" \r");
if (this->_command_buffer.length())
{
this->_complete();
return;
}
Serial.print("\r \r");
Serial.print("> ");
this->_command_buffer = "";
}
void REPL::_complete(void)
{
String partial_name = this->get_arg(0);
REPL_COMMAND * rc = this->_fp_map;
String full_name;
while (rc->name != NULL)
{
full_name = String(rc->name);
if (full_name.startsWith(partial_name))
{
Serial.print("\n\n[HELP] " + String(rc->prototype) + "\n" +
this->prompt + " " + full_name + " ");
this->_command_buffer = full_name + " ";
}
rc++;
}
}
void REPL::_backspace(void)
{
if (this->_command_buffer.length() != 0)
......@@ -118,6 +136,7 @@ void REPL::_history_next(void)
//this->log("history next" + String(this->_history_index));
}
void REPL::_history_save(void)
{
this->_history_index = 0;
......
......@@ -19,6 +19,7 @@ typedef struct
} REPL_COMMAND;
class REPL
{
public:
......@@ -26,9 +27,9 @@ public:
String prompt=">";
void run(void);
void update(void);
void print_help(void);
void log(String msg, bool show_buffer=true);
void log(String msg, bool show_buffer=false);
String get_arg(int index);
private:
......@@ -39,6 +40,7 @@ private:
void _backspace(void);
void _clear_line(void);
void _complete(void);
void _history_previous(void);
void _history_next(void);
......
......@@ -8,72 +8,134 @@
#include "blinker.h"
#define BUILTIN_LED 2
Blinker blinker(BUILTIN_LED);
Ticker alog_ticker;
Ticker log_ticker;
typedef struct
{
char pin_type;
int pin;
int interval;
float interval;
REPL * repl;
} ALOG_args;
bool show_buffer;
} LOG_args;
void REPL_DWRITE(REPL * repl)
{
String arg_pin = repl->get_arg(1);
char pin_type = repl->get_arg(1)[0];
int pin;
String arg_value = repl->get_arg(2);
int value;
int pin = arg_pin.toInt();
int value = !arg_value.toInt();
if (pin_type < '0' || pin_type > '9')
pin = repl->get_arg(1).substring(1).toInt();
else
pin = repl->get_arg(1).toInt();
analogWrite(pin, 0);
pinMode(pin, OUTPUT);
if (arg_pin == "led") pin = BUILTIN_LED;
if (arg_value == "high") value = 0;
if (arg_value == "low") value = 1;
else if (arg_value == "low") value = 1;
else value = arg_value.toInt();
pinMode(pin, OUTPUT);
digitalWrite(pin, value);
repl->log("\n[INFO] Pin " + String(pin) + " was set to " +
value? "HIGH":"LOW");
repl->log("\n[INFO] digitalWrite(" + String(pin) + ", " +
String(value? "HIGH":"LOW") + ")");
}
void REPL_AWRITE(REPL * repl)
{
char pin_type = repl->get_arg(1)[0];
int pin;
String arg_value = repl->get_arg(2);
int value;
if (pin_type < '0' || pin_type > '9')
pin = repl->get_arg(1).substring(1).toInt();
else
pin = repl->get_arg(1).toInt();
pinMode(pin, OUTPUT);
if (arg_value == "min") value = 0;
else if (arg_value == "max") value = 1023;
else value = arg_value.toInt();
analogWrite(pin, value);
repl->log("\n[INFO] analogWrite(" + String(pin) + ", " + String(value) +
")");
}
void REPL_AREAD(REPL * repl)
{
int pin = repl->get_arg(1).toInt();
repl->log("\n[INFO] Read analog pin " + String(pin) + " value:" +
String(analogRead(pin)), false);
repl->log("\n[INFO] analogRead(" + String(pin) + ") --> " +
String(analogRead(pin)));
}
void alog_callback(ALOG_args * args)
void REPL_DREAD(REPL * repl)
{
args->repl->log("\n[LOG] a" + String(args->pin) + ":" +
String(analogRead(args->pin)));
int pin = repl->get_arg(1).toInt();
repl->log("\n[INFO] digitalRead(" + String(pin) + ") --> " +
String(digitalRead(pin)));
}
void REPL_ALOG(REPL * repl)
void log_callback(LOG_args * args)
{
ALOG_args * args = new ALOG_args;
int pin = repl->get_arg(1).toInt();
int interval = repl->get_arg(2).toInt();
String value;
if (args->pin_type == 'd')
value = String(digitalRead(args->pin));
else
value = String(analogRead(args->pin));
args->repl->log("\n[LOG] " + String(args->pin_type) + String(args->pin) +
":" + value, args->show_buffer);
args->show_buffer=true;
}
void REPL_LOG(REPL * repl)
{
LOG_args * args = new LOG_args;
args->pin_type = repl->get_arg(1)[0];
if (not args->pin_type == 'd' && not args->pin_type == 'a')
{
repl->log("\n[ERROR] Invalid pin format. Given \"" + repl->get_arg(1)
+ "\"");
}
args->pin = repl->get_arg(1).substring(1).toInt();
args->interval = repl->get_arg(2).toFloat();
args->repl = repl;
args->show_buffer = false;
if (interval == 0)
if (args->interval == 0)
{
alog_ticker.detach();
repl->log("\n[INFO] Deactivated datalog on analog pin " +
String(pin) + ".");
log_ticker.detach();
repl->log("\n[INFO] Deactivated log of pin " + repl->get_arg(1) + ".");
}
else
{
alog_callback(args);
alog_ticker.attach(interval, alog_callback, args);
args->repl->log("\n[INFO] Activated datalog on analog pin " +
String(pin) + " with " + String(interval)
+ " s interval.");
log_ticker.attach(args->interval, log_callback, args);
repl->log("\n[INFO] Activated log of pin " + repl->get_arg(1) +
" with " + String(args->interval) + " s interval.");
log_callback(args);
}
}
......@@ -85,12 +147,12 @@ void REPL_BLINK(REPL * repl)
if (interval == 0)
{
blinker.deactivate();
repl->log("\n[INFO] Deactivated blink builtin led (pin 2).");
repl->log("\n[INFO] Deactivated blink on builtin led (pin 2).");
}
else
{
blinker.activate(interval);
repl->log("\n[INFO] Activated blink builtin led (pin 2) with " +
repl->log("\n[INFO] Activated blink on builtin led (pin 2) with " +
String(interval) + " s interval.");
}
}
......
......@@ -6,33 +6,45 @@
#ifndef REPL_GPIO_H
#define REPL_GPIO_H
#define CMD_REPL_BLINK {\
"blink", REPL_BLINK, "blink <interval>", \
"Blink builtin led (pin 2) with given <interval> in seconds (int|float). " \
"Use \"blink 0\" to stop it."}
#define CMD_REPL_DWRITE {\
"dwrite", REPL_DWRITE, "dwrite <pin> <value>",\
"Digital write <value> (number|high|low) on <pin>. "}
"Perform pinMode(pin, OUTPUT) and digitalWrite(pin, value)."}
#define CMD_REPL_AWRITE {\
"awrite", REPL_AWRITE, "awrite <pin> <value>",\
"Perform analogWrite(pin, value) with 0 <= PWM value <= 255 (duty cycle)."}
#define CMD_REPL_AREAD {\
"aread", REPL_AREAD, "aread <pin>", \
"Read analogic pin and print out the value."}
"Perform analogRead(pin)"}
#define CMD_REPL_ALOG {\
"alog", REPL_ALOG, "alog <pin> <interval>", \
"Start datalog on analogic pin with given interval."}
#define CMD_REPL_DREAD {\
"dread", REPL_DREAD, "dread <pin>", \
"Perform digitalRead(pin)"}
#define CMD_REPL_LOG {\
"log", REPL_LOG, "log <pin> <interval>", \
"Start logging a pin reading with given interval (in seconds). " \
"Pin must be aX (analog) or dX (digital) where X is an integer."}
#define ALLCMD_REPL_GPIO\
CMD_REPL_BLINK,\
CMD_REPL_DWRITE,\
CMD_REPL_AREAD,\
CMD_REPL_ALOG
#define CMD_REPL_BLINK {\
"blink", REPL_BLINK, "blink <interval>", \
"Blink builtin led (pin 2) with given <interval> in seconds (int|float). " \
"Use \"blink 0\" to stop it."}
#define ALLCMD_REPL_GPIO \
CMD_REPL_DWRITE, \
CMD_REPL_AWRITE, \
CMD_REPL_AREAD, \
CMD_REPL_DREAD, \
CMD_REPL_LOG, \
CMD_REPL_BLINK
void REPL_BLINK(REPL * repl);
void REPL_DWRITE(REPL * repl);
void REPL_AWRITE(REPL * repl);
void REPL_AREAD(REPL * repl);
void REPL_ALOG(REPL * repl);
void REPL_DREAD(REPL * repl);
void REPL_LOG(REPL * repl);
void REPL_BLINK(REPL * repl);
#endif
/*-----------------------------------------------------------------------------
/*----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Provide wifi-related utilities.
......@@ -8,6 +8,7 @@
/*
* CREDITS: https://github.com/platformio/platform-espressif8266
* https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiScan/WiFiScan.ino
*/
void wifiscan(void)
{
......@@ -41,3 +42,32 @@ void wifiscan(void)
}
}
}
void wifistatus(void)
{
WiFi.printDiag(Serial);
}
void wificonnect(String ssid, String password, int threshold)
{
int t=0;
WiFi.begin(ssid.c_str(), password.c_str());
Serial.print("\nConnecting");
while (WiFi.status() != WL_CONNECTED and t <= threshold)
{
delay(500);
Serial.print(".");
t += 500;
}
if (WiFi.status() == WL_CONNECTED)
{
Serial.print("\nConnected, IP address: ");
Serial.println(WiFi.localIP());
}
else
{
Serial.print("\nUnable to connect!");
}
}
......@@ -7,5 +7,7 @@
#define WIFI_H
void wifiscan(void);
void wifistatus(void);
void wificonnect(String ssid, String password, int threshold);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment