forked from forkineye/ESPixelStick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESerialDriver.cpp
115 lines (87 loc) · 2.75 KB
/
ESerialDriver.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
/******************************************************************
*
* Project: ESPixelStick - An ESP8266 and E1.31 based pixel (And Serial!) driver
* Orginal ESPixelStickproject by 2015 Shelby Merrick
*
* Brought to you by:
* Bill Porter
* www.billporter.info
*
* See Readme for other info and version history
*
*
*This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
<http://www.gnu.org/licenses/>
*
*This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
*To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or
*send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
******************************************************************/
#include <Arduino.h>
#include "ESerialDriver.h"
int ESerialDriver::begin(HardwareSerial *theSerial, serial_t type, uint16_t length){
return begin(theSerial, type, length, 57600);
}
int ESerialDriver::begin(HardwareSerial *theSerial, serial_t type, uint16_t length, uint32_t baud){
int retval = true;
_type = type;
_serial = theSerial;
_size = length;
if (type == SERIAL_RENARD)
_serial->begin(baud);
else if (type == SERIAL_DMX)
_serial->begin(250000);
else
retval = false;
return retval;
}
void ESerialDriver::startPacket(){
//create a buffer and fill in header
if (_type == SERIAL_RENARD){
_ptr = (uint8_t*) malloc(_size+2);
_ptr[0] = 0x7E;
_ptr[1] = 0x80;
}
//create buffer
else if (_type == SERIAL_DMX)
_ptr = (uint8_t*) malloc(_size);
}
void ESerialDriver::setValue(uint16_t address, uint8_t value){
//avoid the special characters by rounding
if (_type == SERIAL_RENARD){
switch (value){
case 0x7d:
_ptr[address+2] = 0x7c;
break;
case 0x7e:
case 0x7f:
_ptr[address+2] = 0x80;
break;
default:
_ptr[address+2] = value;
break;
}
}
//set value
else if(_type == SERIAL_DMX)
_ptr[address] = value;
}
void ESerialDriver::show(){
if (_type == SERIAL_RENARD)
_serial->write(_ptr, _size+2);
else if(_type == SERIAL_DMX){
// send the break by sending a slow 0 byte
_serial->begin(125000);
_serial->write(0);
_serial->flush();
_serial->begin(250000);
_serial->write(0);
_serial->write(_ptr, _size);
}
//_serial->flush(); //this may be needed later
free(_ptr);
}