Commit 6d0abebb authored by Nelso Jost's avatar Nelso Jost

NEW: major refactor of esplogger firmware; NEW: created REPL functionality

parent 52f47943
......@@ -7,7 +7,7 @@ PIODIR = .pio
.PHONY: firmware monitor reset getport
all: help
all: firmware
install-platformaio:
pip install platformio
......@@ -16,8 +16,8 @@ firmware:
@ echo "$$PIO_BUILD" | sh
@ $(MAKE) monitor
monitor: getport
picocom /dev/$(PORT) -b $(BAUD)
monitor:
platformio device monitor
reset: getport
@ echo "-----------------------------------------"
......@@ -46,6 +46,9 @@ example-sdcardinfo:
help:
@ echo "$$HELP_MAKEFILE"
clean:
rm -rfv $(PIODIR)
define HELP_MAKEFILE
Usage: make <target> where <target> can be:
......
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Provide the Blinker class for scheduling a blink process.
*---------------------------------------------------------------------------*/
#include <Arduino.h>
#include "blinker.h"
Blinker::Blinker(int pin)
{
this->_pin = pin;
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
}
void Blinker::_update(Blinker *b)
{
b->_state = !b->_state;
digitalWrite(b->_pin, b->_state);
}
void Blinker::activate(float interval)
{
this->_interval = interval;
digitalWrite(this->_pin, LOW);
this->ticker.attach(interval, this->_update, this);
}
void Blinker::deactivate(void)
{
this->ticker.detach();
digitalWrite(this->_pin, HIGH);
}
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Provide the Blinker class for scheduling a blink process.
*---------------------------------------------------------------------------*/
#ifndef BLINKER_H
#define BLINKER_H
#include <Ticker.h>
class Blinker
{
public:
Blinker(int pin);
Ticker ticker;
void activate(float interval);
void deactivate(void);
protected:
int _pin;
bool _state = true;
float _interval;
static void _update(Blinker *b);
};
#endif
/*
*/
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Entry point for the Arduino Toolchain.
*---------------------------------------------------------------------------*/
#include <Arduino.h>
#include "repl.h"
#include "wifi.h"
#include "blinker.h"
#define BUILTIN_LED 2
Blinker blinker(BUILTIN_LED);
Ticker alog_ticker;
bool LOGGING=true;
void REPL_WIFISCAN(String command);
void REPL_BLINK(String command);
void REPL_DWRITE(String command);
void REPL_AREAD(String command);
void REPL_ALOG(String command);
REPL_COMMAND commands[] =\
{
{"wifiscan", REPL_WIFISCAN,
"wifiscan",
"List all availables SSID in range for connection."},
{"blink", REPL_BLINK,
"blink <interval>",
"Blink builtin led (pin 2) with given <interval> in seconds (int|float). "
"Use \"blink 0\" to stop it."},
{"dwrite", REPL_DWRITE,
"dwrite <pin> <value>",
"Digital write <value> (number|high|low) on <pin>. "},
{"aread", REPL_AREAD,
"aread <pin>",
"Read analogic pin and print out the value."},
{"alog", REPL_ALOG,
"alog <pin> <interval>",
"Start datalog on analogic pin with given interval."},
{NULL, NULL}
};
REPL repl(commands);
char read_char = 0;
String command = "";
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.print("> ");
Serial.begin(9600);
repl.run();
}
void loop()
{
if ((read_char == 13) || (command.endsWith(";")))
}
void REPL_WIFISCAN(String command)
{
wifiscan();
}
void REPL_BLINK(String command)
{
float interval = get_arg(command, 1).toFloat();
if (interval == 0)
{
blinker.deactivate();
if (LOGGING)
{
execute_board_command(command);
Serial.println();
Serial.print("> ");
command = "";
Serial.print("\n[INFO] Deactivated blink builtin led (pin 2).");
}
}
else
{
Serial.print(read_char);
command += read_char;
};
Serial.flush();
while (!Serial.available()) {};
read_char = Serial.read();
blinker.activate(interval);
if (LOGGING)
{
Serial.print("\n[INFO] Activated blink builtin led (pin 2) with ");
Serial.print(interval);
Serial.print(" s interval.");
}
}
}
int execute_board_command(String command)
void REPL_DWRITE(String command)
{
if (command == "wifiscan;")
String arg_pin = get_arg(command, 1);
String arg_value = get_arg(command, 2);
int pin = arg_pin.toInt();
int value = !arg_value.toInt();
if (arg_pin == "led") pin = BUILTIN_LED;
if (arg_value == "high") value = 0;
if (arg_value == "low") value = 1;
pinMode(pin, OUTPUT);
digitalWrite(pin, value);
if (LOGGING)
{
wifiscan();
Serial.print("\n[INFO] Pin ");
Serial.print(pin);
Serial.print(" was set to ");
Serial.print(value? "HIGH":"LOW");
}
else if (command == "ls")
}
void REPL_AREAD(String command)
{
int pin = get_arg(command, 1).toInt();
if (LOGGING)
{
Serial.print("\n[INFO] Reading analog pin ");
Serial.print(pin);
Serial.println(":");
}
Serial.print(analogRead(pin));
}
void alog_update(int pin)
{
repl.log("a" + String(pin) + ":" + String(analogRead(pin)));
}
void REPL_ALOG(String command)
{
int pin = get_arg(command, 1).toInt();
int interval = get_arg(command, 2).toInt();
if (interval == 0)
{
alog_ticker.detach();
if (LOGGING)
{
Serial.print("\n[INFO] Deactivated datalog on analog pin ");
Serial.print(pin);
Serial.print(".");
}
}
else
{
Serial.println("");
Serial.print("[Error] Invalid command: \"");
Serial.print(command);
Serial.println("\"");
alog_ticker.attach(interval, alog_update, pin);
if (LOGGING)
{
Serial.print("\n[INFO] Activated datalog on analog pin ");
Serial.print(pin);
Serial.print(" with ");
Serial.print(interval);
Serial.print(" s interval.");
}
}
}
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Read-Eval-Print-Loop functionality
*---------------------------------------------------------------------------*/
#include <Arduino.h>
#include "repl.h"
REPL::REPL(REPL_COMMAND * command_map)
{
this->_fp_map = command_map;
}
void REPL::run(void)
{
char c;
while (1)
{
while (!Serial.available()) { yield(); }
c = Serial.read();
if (c == 8)
this->_backspace();
else if (c == 9)
this->_clear_line();
else if (c == '[')
this->_history_previous();
else if (c == '[')
this->_history_next();
else
{
this->_command += c;
if (c != 13) Serial.write(c);
}
//Serial.println(int(c));
if (this->_command.indexOf("\r") != -1 ||
this->_command.indexOf("\n") != -1)
{
this->_command.trim();
if (this->_command == "help") this->print_help();
else if (this->_command != "") this->_execute_command();
if (c != 13) Serial.print(this->prompt + " ");
this->_command = "";
}
Serial.flush();
}
}
void REPL::_clear_line(void)
{
Serial.print("\r "
" "
" \r");
Serial.print("> ");
this->_command = "";
}
void REPL::_backspace(void)
{
if (this->_command.length() != 0)
{
Serial.write(8);
Serial.write(32);
Serial.write(8);
this->_command.remove(this->_command.length() - 1);
}
}
void REPL::_history_previous(void)
{
}
void REPL::_history_next(void)
{
}
int REPL::_execute_command(void)
{
String command_name = get_arg(this->_command, 0);
bool show_help=false;
if (command_name.endsWith("?"))
{
show_help = true;
command_name.remove(command_name.length() - 1);
}
//Serial.print("\n[INFO] executing command \"");
//Serial.print(command_str);
//Serial.print("\"");
REPL_COMMAND * rc = this->_fp_map;
while (rc->name != NULL)
{
if (command_name == rc->name)
{
if (show_help)
{
Serial.print("\n\n[HELP] Syntax: ");
Serial.println(rc->prototype);
Serial.print("\n ");
Serial.println(rc->help);
return 0;
}
rc->func(this->_command);
return 0;
}
rc++;
}
Serial.print("\n[ERROR] invalid command \"");
Serial.print(command_name);
Serial.print("\"");
return -1;
}
void REPL::print_help(void)
{
REPL_COMMAND * rc = this->_fp_map;
Serial.print("\n------------------");
Serial.print("\nAvailable commands");
Serial.print("\n------------------");
while (rc->name != NULL)
{
Serial.print("\n ");
Serial.print(rc->prototype);
rc++;
}
Serial.print("\n------------------");
Serial.print("\nType \"command?\" for more details on the command.");
Serial.print("\n------------------");
}
void REPL::log(String msg)
{
Serial.print("\n[LOG] " + msg + "\n" + this->prompt + " ");
if (this->_command.length() > 0)
{
Serial.print(this->_command);
}
}
String get_arg(String command, int index)
{
String arg="";
int count=-1;
bool found=false;
for (int i=0; i < command.length(); i++)
{
if (command[i] != ' ')
{
found = true;
arg += command[i];
}
else if (found == true)
{
count += 1;
if (count == index) return arg;
found = false;
arg = "";
}
}
}
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Read-Eval-Print-Loop functionality
*---------------------------------------------------------------------------*/
#ifndef REPL_H
#define REPL_H
typedef struct
{
const char *name;
void (*func)(String);
const char *prototype;
const char *help;
} REPL_COMMAND;
class REPL
{
public:
REPL(REPL_COMMAND command_map[]);
String prompt=">";
void run(void);
void print_help(void);
void log(String msg);
private:
REPL_COMMAND * _fp_map;
String _command="";
void _backspace(void);
void _clear_line(void);
void _history_previous(void);
void _history_next(void);
int _execute_command(void);
};
String get_arg(String command, int index);
#endif
/*
* CREDITS: https://github.com/platformio/platform-espressif8266
*
* This sketch demonstrates how to scan WiFi networks.
* The API is almost the same as with the WiFi Shield library,
* the most obvious difference being the different file you need to include:
*/
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Provide wifi-related utilities.
*---------------------------------------------------------------------------*/
#include <Arduino.h>
#include "ESP8266WiFi.h"
/*
* CREDITS: https://github.com/platformio/platform-espressif8266
*/
void wifiscan(void)
{
Serial.println();
......
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Provide wifi-related utilities.
*---------------------------------------------------------------------------*/
#ifndef WIFI_H
#define WIFI_H
......
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