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


13
14
Blinker blinker(BUILTIN_LED);

15
16
17

Ticker log_ticker;

18

19
20
typedef struct
{
21
    char pin_type;
22
    int pin;
23
    float interval;
24
    REPL * repl;
25
26
27
    bool show_buffer;
} LOG_args;

28

29
30
void REPL_DWRITE(REPL * repl)
{
31
32
    char pin_type = repl->get_arg(1)[0];
    int pin;
33
    String arg_value = repl->get_arg(2);
34
    int value;
35

36
37
38
39
40
41
42
    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);
43
44

    if (arg_value == "high") value = 0;
45
46
    else if (arg_value == "low") value = 1;
    else value = arg_value.toInt();
47
48
49

    digitalWrite(pin, value);

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
    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) +
              ")");
77
78
79
}


80

81
82
83
void REPL_AREAD(REPL * repl)
{
    int pin = repl->get_arg(1).toInt();
84
85
    repl->log("\n[INFO] analogRead(" + String(pin) + ") --> " +
              String(analogRead(pin)));
86
87
88
}


89
void REPL_DREAD(REPL * repl)
90
{
91
92
93
    int pin = repl->get_arg(1).toInt();
    repl->log("\n[INFO] digitalRead(" + String(pin) + ") --> " +
              String(digitalRead(pin)));
94
95
}

96

97
void log_callback(LOG_args * args)
98
{
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    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();
125
    args->repl = repl;
126
    args->show_buffer = false;
127

128
    if (args->interval == 0)
129
    {
130
131
        log_ticker.detach();
        repl->log("\n[INFO] Deactivated log of pin " + repl->get_arg(1) + ".");
132
133
134
    }
    else
    {
135
136
137
138
        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);
139
140
141
142
143
144
145
146
147
148
149
    }
}


void REPL_BLINK(REPL * repl)
{
    float interval = repl->get_arg(1).toFloat();

    if (interval == 0)
    {
        blinker.deactivate();
150
        repl->log("\n[INFO] Deactivated blink on builtin led (pin 2).");
151
152
153
154
    }
    else
    {
        blinker.activate(interval);
155
        repl->log("\n[INFO] Activated blink on builtin led (pin 2) with " +
156
157
158
159
                  String(interval) + " s interval.");
    }
}