Skip to content

Commit 7b64024

Browse files
committed
add RGB example
1 parent 8625daa commit 7b64024

File tree

6 files changed

+181
-12
lines changed

6 files changed

+181
-12
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void setup() {
5050
```
5151

5252
>SerialBLE Modules:
53-
>**Blinker.begin()** will config SoftWareSerial with default settings.
53+
>**Blinker.begin()** will config Serial with default settings.
5454
>
5555
>Blinker.begin();// default settings digital pins 2(RX) 3(TX) and baudrate 9600 bps
5656
>Blinker.begin(4, 5);// config digital pins 4(RX) 5(TX) with default baudrate 9600 bps
@@ -201,6 +201,13 @@ String result_LAT = Blinker.gps(LAT);
201201
> LONG for longitude
202202
> LAT for latitude
203203
204+
### Blinker.rgb()
205+
Return the latest update of **RGB** value from app
206+
```
207+
uint8_t result_R = Blinker.rgb(R);
208+
uint8_t result_G = Blinker.rgb(G);
209+
uint8_t result_B = Blinker.rgb(B);
210+
```
204211
### Blinker.vibrate()
205212
Send vibrate commond to Blinker, default vibration time is 500 milliseconds
206213
```
@@ -286,7 +293,7 @@ void setup() {
286293
```
287294

288295
>串口蓝牙模块:
289-
>**Blinker.begin()** 将使用默认设置配置 SoftWareSerial
296+
>**Blinker.begin()** 将使用默认设置配置 Serial
290297
>
291298
>Blinker.begin();// 默认设置: 数字IO 2(RX) 3(TX), 波特率 9600 bps
292299
>Blinker.begin(4, 5);// 设置数字IO 4(RX) 5(TX), 默认波特率 9600 bps
@@ -434,6 +441,13 @@ String result_LAT = Blinker.gps(LAT);
434441
> LONG 经度
435442
> LAT 维度
436443
444+
### Blinker.rgb()
445+
读取 **RGB** 数据
446+
```
447+
uint8_t result_R = Blinker.rgb(R);
448+
uint8_t result_G = Blinker.rgb(G);
449+
uint8_t result_B = Blinker.rgb(B);
450+
```
437451
### Blinker.vibrate()
438452
发送手机振动指令, 震动时间, 单位ms 毫秒, 数值范围0-1000, 默认为500
439453
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#define BLINKER_PRINT Serial
2+
#define BLINKER_BLE
3+
4+
#include <Blinker.h>
5+
6+
void setup()
7+
{
8+
Serial.begin(115200);
9+
10+
pinMode(LED_BUILTIN, OUTPUT);
11+
digitalWrite(LED_BUILTIN, LOW);
12+
13+
Blinker.begin();
14+
}
15+
16+
void loop()
17+
{
18+
Blinker.run();
19+
20+
if (Blinker.available()) {
21+
BLINKER_LOG2("Blinker.readString(): ", Blinker.readString());
22+
23+
Blinker.vibrate();
24+
25+
uint32_t BlinkerTime = millis();
26+
Blinker.print(BlinkerTime);
27+
Blinker.print("millis", BlinkerTime);
28+
}
29+
30+
BLINKER_LOG2("Red color: ", Blinker.rgb(R));
31+
BLINKER_LOG2("Green color: ", Blinker.rgb(G));
32+
BLINKER_LOG2("Blue color: ", Blinker.rgb(B));
33+
34+
Blinker.delay(2000);
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#define BLINKER_PRINT Serial
2+
#define BLINKER_WIFI
3+
4+
#include <Blinker.h>
5+
6+
char ssid[] = "<Your WiFi network SSID or name>";
7+
char pswd[] = "<Your WiFi network WPA password or WEP key>";
8+
9+
void setup()
10+
{
11+
Serial.begin(115200);
12+
13+
pinMode(LED_BUILTIN, OUTPUT);
14+
digitalWrite(LED_BUILTIN, LOW);
15+
16+
Blinker.begin(ssid, pswd);
17+
}
18+
19+
void loop()
20+
{
21+
Blinker.run();
22+
23+
if (Blinker.available()) {
24+
BLINKER_LOG2("Blinker.readString(): ", Blinker.readString());
25+
26+
Blinker.vibrate();
27+
28+
uint32_t BlinkerTime = millis();
29+
Blinker.print(BlinkerTime);
30+
Blinker.print("millis", BlinkerTime);
31+
}
32+
33+
BLINKER_LOG2("Red color: ", Blinker.rgb(R));
34+
BLINKER_LOG2("Green color: ", Blinker.rgb(G));
35+
BLINKER_LOG2("Blue color: ", Blinker.rgb(B));
36+
37+
Blinker.delay(2000);
38+
}

src/Adapters/BlinkerSerial.h

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
#define BlinkerSerial_H
33

44
#include <SoftwareSerial.h>
5+
#include "HardwareSerial.h"
56
#include <Blinker/BlinkerProtocol.h>
67

7-
SoftwareSerial *SerialBLE;
8+
SoftwareSerial *SSerialBLE;
9+
// HardwareSerial *HSerialBLE;
810

911
class BlinkerTransportStream
1012
{
@@ -15,9 +17,11 @@ class BlinkerTransportStream
1517

1618
bool available()
1719
{
18-
if (!SerialBLE->isListening()) {
19-
SerialBLE->listen();
20-
::delay(100);
20+
if (!isHWS) {
21+
if (!SSerialBLE->isListening()) {
22+
SSerialBLE->listen();
23+
::delay(100);
24+
}
2125
}
2226

2327
if (stream->available()) {
@@ -32,10 +36,11 @@ class BlinkerTransportStream
3236
}
3337
}
3438

35-
void begin(Stream& s)
39+
void begin(Stream& s, bool state)
3640
{
3741
stream = &s;
3842
stream->setTimeout(BLINKER_STREAM_TIMEOUT);
43+
isHWS = state;
3944
}
4045

4146
String lastRead() { return STRING_format(streamData); }
@@ -72,6 +77,7 @@ class BlinkerTransportStream
7277
Stream* stream;
7378
char streamData[BLINKER_MAX_READ_SIZE];
7479
bool isConnect;
80+
bool isHWS = false;
7581
};
7682

7783
class BlinkerSerail
@@ -88,11 +94,58 @@ class BlinkerSerail
8894
uint8_t ss_tx_pin = 3,
8995
uint32_t ss_baud = 9600)
9096
{
91-
Base::begin();
92-
SerialBLE = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
93-
SerialBLE->begin(ss_baud);
94-
this->conn.begin(*SerialBLE);
95-
BLINKER_LOG1("SerialBLE Initialled...");
97+
#if defined (__AVR__)
98+
if (ss_rx_pin == 0 && ss_tx_pin == 1){
99+
Base::begin();
100+
#if defined (__AVR_ATmega32U4__)
101+
Serial1.begin(ss_baud);
102+
this->conn.begin(Serial1, true);
103+
#else
104+
Serial.begin(ss_baud);
105+
this->conn.begin(Serial, true);
106+
#endif
107+
BLINKER_LOG1("SerialBLE Initialled...");
108+
return;
109+
}
110+
#if defined (__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__SAM3X8E__)
111+
else if (ss_rx_pin == 19 && ss_tx_pin == 18){
112+
Base::begin();
113+
Serial1.begin(ss_baud);
114+
this->conn.begin(Serial1, true);
115+
BLINKER_LOG1("SerialBLE Initialled...");
116+
return;
117+
}
118+
else if (ss_rx_pin == 17 && ss_tx_pin == 16){
119+
Base::begin();
120+
Serial2.begin(ss_baud);
121+
this->conn.begin(Serial2, true);
122+
BLINKER_LOG1("SerialBLE Initialled...");
123+
return;
124+
}
125+
else if (ss_rx_pin == 15 && ss_tx_pin == 14){
126+
Base::begin();
127+
Serial3.begin(ss_baud);
128+
this->conn.begin(Serial3, true);
129+
BLINKER_LOG1("SerialBLE Initialled...");
130+
return;
131+
}
132+
#endif
133+
else {
134+
Base::begin();
135+
SSerialBLE = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
136+
SSerialBLE->begin(ss_baud);
137+
this->conn.begin(*SSerialBLE, false);
138+
BLINKER_LOG1("SerialBLE Initialled...");
139+
}
140+
#else
141+
else {
142+
Base::begin();
143+
SSerialBLE = new SoftwareSerial(ss_rx_pin, ss_tx_pin);
144+
SSerialBLE->begin(ss_baud);
145+
this->conn.begin(*SSerialBLE, false);
146+
BLINKER_LOG1("SerialBLE Initialled...");
147+
}
148+
#endif
96149
}
97150
};
98151

src/Blinker/BlinkerApi.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ enum b_gps_t {
2727
LAT
2828
};
2929

30+
enum b_rgb_t {
31+
R,
32+
G,
33+
B
34+
};
35+
3036
static class BlinkerButton * _Button[BLINKER_MAX_WIDGET_SIZE];
3137
static class BlinkerSlider * _Slider[BLINKER_MAX_WIDGET_SIZE];
3238
static class BlinkerToggle * _Toggle[BLINKER_MAX_WIDGET_SIZE];
@@ -108,6 +114,9 @@ class BlinkerApi
108114
ahrsValue[Pitch] = 0;
109115
gpsValue[LONG] = "0.000000";
110116
gpsValue[LAT] = "0.000000";
117+
rgbValue[R] = 0;
118+
rgbValue[G] = 0;
119+
rgbValue[B] = 0;
111120
}
112121

113122
void wInit(const String & _name, b_widgettype_t _type) {
@@ -162,6 +171,7 @@ class BlinkerApi
162171
joystick(J_Xaxis);
163172
ahrs(Yaw);
164173
gps(LONG, true);
174+
rgb(R);
165175

166176
if (_fresh) {
167177
static_cast<Proto*>(this)->isParsed();
@@ -424,6 +434,22 @@ class BlinkerApi
424434
}
425435
}
426436

437+
uint8_t rgb(b_rgb_t color) {
438+
int16_t colorValue = STRING_find_array_numberic_value(static_cast<Proto*>(this)->dataParse(), BLINKER_CMD_RGB, color);
439+
440+
if (colorValue != FIND_KEY_VALUE_FAILED) {
441+
rgbValue[R] = STRING_find_array_numberic_value(static_cast<Proto*>(this)->dataParse(), BLINKER_CMD_RGB, R);
442+
rgbValue[G] = STRING_find_array_numberic_value(static_cast<Proto*>(this)->dataParse(), BLINKER_CMD_RGB, G);
443+
rgbValue[B] = STRING_find_array_numberic_value(static_cast<Proto*>(this)->dataParse(), BLINKER_CMD_RGB, B);
444+
445+
_fresh = true;
446+
return colorValue;
447+
}
448+
else {
449+
return rgbValue[color];
450+
}
451+
}
452+
427453
// void freshGPS()
428454
// {
429455
// static_cast<Proto*>(this)->print(BLINKER_CMD_GPS, "");
@@ -458,6 +484,7 @@ class BlinkerApi
458484
uint8_t joyValue[2];
459485
int16_t ahrsValue[3];
460486
String gpsValue[2];
487+
uint8_t rgbValue[3];
461488
bool _fresh = false;
462489

463490
bool buttonParse(const String & _bName)

src/Blinker/BlinkerConfig.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141

4242
#define BLINKER_CMD_GPS "gps"
4343

44+
#define BLINKER_CMD_RGB "rgb"
45+
4446
#define BLINKER_CMD_VIBRATE "vibrate"
4547

4648
#define BLINKER_CMD_BUTTON_TAP "tap"

0 commit comments

Comments
 (0)