repl_gpio.cpp 2.03 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*-----------------------------------------------------------------------------
 *  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.");
    }
}