Commit 95f610b1 authored by Nelso Jost's avatar Nelso Jost

DEVEL: reestructuring repl files and usage

parent 1bab7777
......@@ -14,9 +14,13 @@ install-platformio:
sudo python get-pip.py && rm get-pip.py
sudo pip install platformio
firmware:
firmware: build upload monitor
build:
@ echo "$$PIO_BUILD" | sh
@ $(MAKE) monitor
upload:
cd $(PIODIR) && platformio run -t upload
monitor:
platformio device monitor
......@@ -83,6 +87,6 @@ define PIO_BUILD
rm -rf $(PIODIR)/src
platformio init --board $(PIOBOARD) -d $(PIODIR)
cp -rf $(PIOPROJ)/* $(PIODIR)/src/
cd $(PIODIR) && platformio run -t upload
cd $(PIODIR) && platformio run
endef
export PIO_BUILD
......@@ -5,47 +5,21 @@
*---------------------------------------------------------------------------*/
#include <Arduino.h>
#include "repl.h"
#include "repl_gpio.h"
#include "repl_sdcard.h"
#include "wifi.h"
#include "blinker.h"
#include "sdcard.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);
void REPL_SDCARDINFO(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."},
{"sdcardinfo", REPL_SDCARDINFO,
"sdcardinfo",
"Show a summary of the SD card info."},
{NULL, NULL}
ALLCMD_REPL_GPIO,
ALLCMD_REPL_SDCARD,
{"wifiscan", REPL_WIFISCAN, "wifiscan",
"List all availables SSID in range for connection."},
{NULL}
};
REPL repl(commands);
......@@ -62,112 +36,11 @@ void loop()
}
void REPL_WIFISCAN(String command)
void REPL_WIFISCAN(REPL * repl)
{
wifiscan();
}
void REPL_BLINK(String command)
{
float interval = get_arg(command, 1).toFloat();
if (interval == 0)
{
blinker.deactivate();
if (LOGGING)
{
Serial.print("\n[INFO] Deactivated blink builtin led (pin 2).");
}
}
else
{
blinker.activate(interval);
if (LOGGING)
{
Serial.print("\n[INFO] Activated blink builtin led (pin 2) with ");
Serial.print(interval);
Serial.print(" s interval.");
}
}
}
void REPL_DWRITE(String command)
{
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)
{
Serial.print("\n[INFO] Pin ");
Serial.print(pin);
Serial.print(" was set to ");
Serial.print(value? "HIGH":"LOW");
}
}
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
{
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.");
}
}
}
void REPL_SDCARDINFO(String command)
{
print_sdcard_info();
}
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Read-Eval-Print-Loop functionality
* Purpose: Provide basic GPIO control commands for the REPL.
*---------------------------------------------------------------------------*/
#include <Arduino.h>
......@@ -11,6 +11,7 @@
REPL::REPL(REPL_COMMAND * command_map)
{
this->_fp_map = command_map;
this->_history[0] = "";
}
......@@ -31,25 +32,25 @@ void REPL::run(void)
else if (c == '[')
this->_history_previous();
else if (c == '[')
else if (c == ']')
this->_history_next();
else
{
this->_command += c;
this->_command_buffer += c;
if (c != 13) Serial.write(c);
}
//Serial.println(int(c));
if (this->_command.indexOf("\r") != -1 ||
this->_command.indexOf("\n") != -1)
if (this->_command_buffer.indexOf("\r") != -1 ||
this->_command_buffer.indexOf("\n") != -1)
{
this->_command.trim();
if (this->_command == "help") this->print_help();
else if (this->_command != "") this->_execute_command();
this->_command_buffer.trim();
if (this->_command_buffer == "help") this->print_help();
else if (this->_command_buffer != "") this->_execute_command();
if (c != 13) Serial.print(this->prompt + " ");
this->_command = "";
this->_command_buffer = "";
}
Serial.flush();
}
......@@ -62,36 +63,68 @@ void REPL::_clear_line(void)
" "
" \r");
Serial.print("> ");
this->_command = "";
this->_command_buffer = "";
}
void REPL::_backspace(void)
{
if (this->_command.length() != 0)
if (this->_command_buffer.length() != 0)
{
Serial.write(8);
Serial.write(32);
Serial.write(8);
this->_command.remove(this->_command.length() - 1);
this->_history_index = 0;
this->_command_buffer.remove(this->_command_buffer.length() - 1);
}
}
void REPL::_history_previous(void)
{
if (this->_history_index < HISTORY_MAX - 1 && \
this->_history[this->_history_index] != "")
{
this->_command_buffer = this->_history[this->_history_index];
this->_history_index++;
Serial.print("\r \r");
Serial.print(this->prompt + " [" + this->_history_index + "] ");
Serial.print(this->_command_buffer);
}
//this->log("history previous" + String(this->_history_index));
}
void REPL::_history_next(void)
{
if (this->_history_index > 0 && \
this->_history[this->_history_index] != "")
{
this->_command_buffer = this->_history[this->_history_index];
this->_history_index--;
Serial.print("\r \r");
Serial.print(this->prompt + " [" + this->_history_index + "] ");
Serial.print(this->_command_buffer);
}
//this->log("history next" + String(this->_history_index));
}
void REPL::_history_save(void)
{
this->_history_index = 0;
if (this->_history[0] != this->_command_buffer)
{
for (int i=0; i < HISTORY_MAX - 2; i++)
{
this->_history[i+1] = this->_history[i];
}
this->_history[0] = this->_command_buffer;
}
}
int REPL::_execute_command(void)
{
String command_name = get_arg(this->_command, 0);
String command_name = get_arg(this->_command_buffer, 0);
bool show_help=false;
if (command_name.endsWith("?"))
......@@ -118,7 +151,8 @@ int REPL::_execute_command(void)
Serial.println(rc->help);
return 0;
}
rc->func(this->_command);
rc->func(this);
this->_history_save();
return 0;
}
rc++;
......@@ -153,9 +187,9 @@ void REPL::print_help(void)
void REPL::log(String msg)
{
Serial.print("\n[LOG] " + msg + "\n" + this->prompt + " ");
if (this->_command.length() > 0)
if (this->_command_buffer.length() > 0)
{
Serial.print(this->_command);
Serial.print(this->_command_buffer);
}
}
......
......@@ -6,11 +6,14 @@
#ifndef REPL_H
#define REPL_H
#define HISTORY_MAX 10
class REPL {};
typedef struct
{
const char *name;
void (*func)(String);
void (*func)(REPL *);
const char *prototype;
const char *help;
} REPL_COMMAND;
......@@ -26,20 +29,22 @@ public:
void run(void);
void print_help(void);
void log(String msg);
String get_arg(int index);
private:
REPL_COMMAND * _fp_map;
String _command="";
String _command_buffer="";
String _history[HISTORY_MAX];
int _history_index=-1;
void _backspace(void);
void _clear_line(void);
void _history_previous(void);
void _history_next(void);
void _history_save(void);
int _execute_command(void);
};
String get_arg(String command, int index);
#endif
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Read-Eval-Print-Loop functionality
*---------------------------------------------------------------------------*/
#include <Arduino.h>
#include "repl.h"
#include "blinker.h"
#define BUILTIN_LED 2
Blinker blinker(BUILTIN_LED);
Ticker alog_ticker;
void REPL_DWRITE(REPL * repl)
{
String arg_pin = repl->get_arg(1);
String arg_value = repl->get_arg(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);
repl->log("\n[INFO] Pin " + String(pin) + " was set to " +
value? "HIGH":"LOW");
}
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));
}
void alog_callback(int pin, REPL * repl)
{
repl.log("a" + String(pin) + ":" + String(analogRead(pin)));
}
void REPL_ALOG(REPL * repl)
{
int pin = get_arg(1).toInt();
int interval = get_arg(2).toInt();
if (interval == 0)
{
alog_ticker.detach();
repl->log("\n[INFO] Deactivated datalog on analog pin " +
String(pin) + ".");
}
else
{
alog_ticker.attach(interval, alog_callback, pin, repl);
repl->log("\n[INFO] Activated datalog on analog pin " +
String(pin) + " with " + String(interval) + " s interval.");
}
}
void REPL_BLINK(REPL * repl)
{
float interval = repl->get_arg(1).toFloat();
if (interval == 0)
{
blinker.deactivate();
repl->log("\n[INFO] Deactivated blink builtin led (pin 2).");
}
else
{
blinker.activate(interval);
repl->log("\n[INFO] Activated blink builtin led (pin 2) with "
String(interval) + " s interval.");
}
}
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Provide basic GPIO control commands for the REPL.
*---------------------------------------------------------------------------*/
#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>. "}
#define CMD_REPL_AREAD {\
"aread", REPL_AREAD, "aread <pin>", \
"Read analogic pin and print out the value."}
#define CMD_REPL_ALOG {\
"alog", REPL_ALOG, "alog <pin> <interval>", \
"Start datalog on analogic pin with given interval."}
#define ALLCMD_REPL_GPIO\
CMD_REPL_BLINK,\
CMD_REPL_DWRITE,\
CMD_REPL_AREAD,\
CMD_REPL_ALOG
void REPL_BLINK(REPL * repl);
void REPL_DWRITE(REPL * repl);
void REPL_AREAD(REPL * repl);
void REPL_ALOG(REPL * repl);
#endif
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Provide basic SD card management commands for the REPL.
*---------------------------------------------------------------------------*/
#include <Arduino.h>
#include "repl.h"
#include "sdcard.h"
void REPL_SDCARDINFO(REPL * repl)
{
print_sdcard_info();
}
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* GPIO controlurpose: Provide basic SD card management commands for the REPL.
*---------------------------------------------------------------------------*/
#ifndef REPL_GPIO_H
#define REPL_GPIO_H
#define CMD_REPL_SDCARDINFO {\
"sdcardinfo", REPL_SDCARDINFO, "sdcardinfo", \
"Show a summary of the SD card info."}
#define ALLCMD_REPL_SDCARD\
CMD_REPL_SDCARDINFO,\
void REPL_SDCARDINFO(REPL * repl);
#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