Commit 4fbbb322 authored by Nelso Jost's avatar Nelso Jost
Browse files

refactor REPL_FUNC and logging system

parent 95f610b1
......@@ -6,6 +6,7 @@
*.json
*.log
.build
Session.vim
get-pip.py*
pid_*
__pycache__
......@@ -9,7 +9,7 @@
#include "repl_sdcard.h"
#include "wifi.h"
void REPL_WIFISCAN(String command);
void REPL_WIFISCAN(REPL * repl);
REPL_COMMAND commands[] =\
{
......
......@@ -48,10 +48,20 @@ void REPL::run(void)
{
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 + " ");
else
{
if (this->_command_buffer != "")
{
this->_execute_command();
this->_command_buffer = "";
}
else
{
this->_command_buffer = " ";
}
}
if (c != 13) Serial.print(this->prompt + " ");
}
Serial.flush();
}
}
......@@ -124,7 +134,7 @@ void REPL::_history_save(void)
int REPL::_execute_command(void)
{
String command_name = get_arg(this->_command_buffer, 0);
String command_name = get_arg(0);
bool show_help=false;
if (command_name.endsWith("?"))
......@@ -184,27 +194,27 @@ void REPL::print_help(void)
}
void REPL::log(String msg)
void REPL::log(String msg, bool show_buffer)
{
Serial.print("\n[LOG] " + msg + "\n" + this->prompt + " ");
if (this->_command_buffer.length() > 0)
Serial.print(msg);
if (this->_command_buffer.length() > 0 and show_buffer)
{
Serial.print(this->_command_buffer);
Serial.print("\n" + this->prompt + " " + this->_command_buffer);
}
}
String get_arg(String command, int index)
String REPL::get_arg(int index)
{
String arg="";
int count=-1;
bool found=false;
for (int i=0; i < command.length(); i++)
for (int i=0; i < this->_command_buffer.length(); i++)
{
if (command[i] != ' ')
if (this->_command_buffer[i] != ' ')
{
found = true;
arg += command[i];
arg += this->_command_buffer[i];
}
else if (found == true)
{
......
......@@ -8,7 +8,7 @@
#define HISTORY_MAX 10
class REPL {};
class REPL;
typedef struct
{
......@@ -28,7 +28,7 @@ public:
void run(void);
void print_help(void);
void log(String msg);
void log(String msg, bool show_buffer=true);
String get_arg(int index);
private:
......
......@@ -12,6 +12,13 @@ Blinker blinker(BUILTIN_LED);
Ticker alog_ticker;
typedef struct
{
int pin;
int interval;
REPL * repl;
} ALOG_args;
void REPL_DWRITE(REPL * repl)
{
String arg_pin = repl->get_arg(1);
......@@ -35,20 +42,24 @@ void REPL_DWRITE(REPL * repl)
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));
repl->log("\n[INFO] Read analog pin " + String(pin) + " value:" +
String(analogRead(pin)), false);
}
void alog_callback(int pin, REPL * repl)
void alog_callback(ALOG_args * args)
{
repl.log("a" + String(pin) + ":" + String(analogRead(pin)));
args->repl->log("\n[LOG] a" + String(args->pin) + ":" +
String(analogRead(args->pin)));
}
void REPL_ALOG(REPL * repl)
{
int pin = get_arg(1).toInt();
int interval = get_arg(2).toInt();
ALOG_args * args = new ALOG_args;
int pin = repl->get_arg(1).toInt();
int interval = repl->get_arg(2).toInt();
args->repl = repl;
if (interval == 0)
{
......@@ -58,9 +69,11 @@ void REPL_ALOG(REPL * repl)
}
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.");
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.");
}
}
......@@ -77,7 +90,7 @@ void REPL_BLINK(REPL * repl)
else
{
blinker.activate(interval);
repl->log("\n[INFO] Activated blink builtin led (pin 2) with "
repl->log("\n[INFO] Activated blink builtin led (pin 2) with " +
String(interval) + " s interval.");
}
}
......
......@@ -3,8 +3,8 @@
* License: GPLv2
* GPIO controlurpose: Provide basic SD card management commands for the REPL.
*---------------------------------------------------------------------------*/
#ifndef REPL_GPIO_H
#define REPL_GPIO_H
#ifndef REPL_SDCARD_H
#define REPL_SDCARD_H
#define CMD_REPL_SDCARDINFO {\
......@@ -13,7 +13,7 @@
#define ALLCMD_REPL_SDCARD\
CMD_REPL_SDCARDINFO,\
CMD_REPL_SDCARDINFO\
void REPL_SDCARDINFO(REPL * repl);
......
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