/*----------------------------------------------------------------------------- * Author: Nelso G. Jost (nelsojost@gmail.com) * License: GPLv2 * Purpose: Read-Eval-Print-Loop functionality *---------------------------------------------------------------------------*/ #include #include "repl.h" #include "blinker.h" #define BUILTIN_LED 2 Blinker blinker(BUILTIN_LED); Ticker log_ticker; typedef struct { char pin_type; int pin; float interval; REPL * repl; bool show_buffer; } LOG_args; void REPL_DWRITE(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(); analogWrite(pin, 0); pinMode(pin, OUTPUT); if (arg_value == "high") value = 0; else if (arg_value == "low") value = 1; else value = arg_value.toInt(); digitalWrite(pin, value); 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] analogRead(" + String(pin) + ") --> " + String(analogRead(pin))); } void REPL_DREAD(REPL * repl) { int pin = repl->get_arg(1).toInt(); repl->log("\n[INFO] digitalRead(" + String(pin) + ") --> " + String(digitalRead(pin))); } void log_callback(LOG_args * args) { 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 (args->interval == 0) { log_ticker.detach(); repl->log("\n[INFO] Deactivated log of pin " + repl->get_arg(1) + "."); } else { 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); } } void REPL_BLINK(REPL * repl) { float interval = repl->get_arg(1).toFloat(); if (interval == 0) { blinker.deactivate(); repl->log("\n[INFO] Deactivated blink on builtin led (pin 2)."); } else { blinker.activate(interval); repl->log("\n[INFO] Activated blink on builtin led (pin 2) with " + String(interval) + " s interval."); } }