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

refactor REPL_FUNC and logging system

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