repl_gpio.cpp 2.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*-----------------------------------------------------------------------------
 *  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;

15 16 17 18 19 20 21
typedef struct
{
    int pin;
    int interval;
    REPL * repl;
} ALOG_args;

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
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();
45 46
    repl->log("\n[INFO] Read analog pin " + String(pin) + " value:" +
              String(analogRead(pin)), false);
47 48 49
}


50
void alog_callback(ALOG_args * args)
51
{
52 53
    args->repl->log("\n[LOG] a" + String(args->pin) + ":" +
                    String(analogRead(args->pin)));
54 55
}

56

57 58
void REPL_ALOG(REPL * repl)
{
59 60 61 62
    ALOG_args * args = new ALOG_args;
    int pin = repl->get_arg(1).toInt();
    int interval = repl->get_arg(2).toInt();
    args->repl = repl;
63 64 65 66 67 68 69 70 71

    if (interval == 0)
    {
        alog_ticker.detach();
        repl->log("\n[INFO] Deactivated datalog on analog pin " +
                  String(pin) + ".");
    }
    else
    {
72 73 74 75 76
        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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    }
}


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);
93
        repl->log("\n[INFO] Activated blink builtin led (pin 2) with " +
94 95 96 97
                  String(interval) + " s interval.");
    }
}