-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial_cli_controller.cpp
121 lines (112 loc) · 3.18 KB
/
serial_cli_controller.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "serial_cli_controller.h"
#include <Arduino.h>
#include <EEPROM.h>
#include "fade_effect.h"
SerialCLIController::SerialCLIController()
{
}
void SerialCLIController::init()
{
// Initialise serial connection
Serial.begin(115200);
// A nice prompt for input output
Serial.println("Wellcome to LEDOS");
Serial.println("How can I be of assistance?");
Serial.print(">");
}
void SerialCLIController::update()
{
while( Serial.available() > 0 ) {
char byte;
Serial.readBytes(&byte, 1);
if( byte == '\n' )
{
execCommand();
} else {
command += byte;
// Discard commands longer than 255 symbols
if( command.length() > 255 )
command = "";
}
}
}
void SerialCLIController::execCommand()
{
if ( command[0] == 'b' ) {
LEDColor::R( hex2bin( command.substring(1, 3).c_str() ) );
LEDColor::G( hex2bin( command.substring(3, 5).c_str() ) );
LEDColor::B( hex2bin( command.substring(5, 7).c_str() ) );
}
else if (command == "shutdown") // SHUTDOWN
{
currentEffect->stop();
currentEffect->reset();
EEPROM.write(2, 255);
}
else if (command == "effect start") // EFFECT START
{
currentEffect->start();
}
else if (command == "effect stop") // EFFECT STOP
{
currentEffect->stop();
}
else if ( command.startsWith("effect set duration ") ) // EFFECT SET DURATION
{
currentEffect->duration(command.substring(20).toFloat() * 1000);
}
else if ( command.startsWith("c ") ) {
int offset = 0, temp;
LEDColor::R( temp = command.substring(2).toInt() );
do { ++offset; temp /= 10; } while (temp);
LEDColor::G( temp = command.substring(3 + offset).toInt() );
do { ++offset; temp /= 10; } while (temp);
LEDColor::B( command.substring(4 + offset).toInt() );
}
else if ( command.startsWith("w ") ) {
int offset = 0;
float temp;
LEDColor::RFactor( temp = command.substring(2).toFloat() );
do { ++offset; temp /= 10; } while (temp);
LEDColor::GFactor( temp = command.substring(3 + offset).toFloat() );
do { ++offset; temp /= 10; } while (temp);
LEDColor::BFactor( command.substring(4 + offset).toFloat() );
}
else
{
bool error = true;
for(int i = 0; i < effectCount; ++i) // EFFECT NAME
{
if( command == (String("effect ") + effects[i]->name()) )
{
currentEffect = effects[i];
currentEffect->init();
currentEffect->start();
EEPROM.write(2, i);
error = false;
}
}
// If an effect is not found display an error
if( error ) Serial.println("Unrecognized command.");
}
// Discard the current command
command = "";
}
int SerialCLIController::hex2bin( const char *s )
{
int ret = 0;
int i;
for( i=0; i<2; i++ )
{
char c = *s++;
int n=0;
if( '0'<=c && c<='9' )
n = c-'0';
else if( 'a'<=c && c<='f' )
n = 10 + c-'a';
else if( 'A'<=c && c<='F' )
n = 10 + c-'A';
ret = n + ret*16;
}
return ret;
}