forked from haumduino/24h
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.cpp
206 lines (186 loc) · 4.57 KB
/
io.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "io.h"
#include "my_def.h"
#include <Arduino.h>
/*
* Pour chaque bit dans la frame (1 + address + data):
* - s'il est a 1, on met la sortie à 1 et temps = 8 (8*5 = 40 ms)
* - s'il est a 0, on met la sortie à 1 et temps = 4 (4*5=20ms)
* Si temps de bit = 0 reset sortie et temps = 1 (5ms)
*
* si fin de trame temps = 1 (5ms)
*/
IODevice::IODevice(const int output_pin, int input_pin) :
_encountered_frame(false),
_unconnected(false),
_tick_toogle(false),
_output_frame(0x0000),
_output_remainingTicks(0),
_output_currentBit(0),
_output_state(IDLE),
_input_frame(0x0000),
_input_received_frame_is_available(false),
_input_received_frame(0x0000),
_input_current_bit(9),
_input_time_at_level(0), // 0 == IDLE
_input_name(0)
{
if (input_pin==in_left) _input_state=RIGHT_TO_LEFT;
if (input_pin==in_right) _input_state=LEFT_TO_RIGHT;
_output_pin = output_pin;
pinMode(_output_pin, OUTPUT);
_input_pin = input_pin;
}
void IODevice::sendFrame(const byte address, const byte data)
{
while(_output_state!=IDLE) {}
_output_frame = (1 << 9) | ((uint16_t)address << 2) | ((uint16_t)data & 0x03);
Serial.print("SENT frame: ");
printFrame(_output_frame);
_output_currentBit = 9;
_output_state = SENDING;
}
uint16_t IODevice::receiveFrame()
{
_input_received_frame_is_available = false;
_input_name=(_input_received_frame >> 2) & 0x07f;
_input_state=_input_received_frame & 0x03;
Serial.print("RECV frame: ");
printFrame(_input_received_frame);
}
void IODevice::tick2500us()
{
input_level_detect();
// Prescale /2
_tick_toogle = ! _tick_toogle;
if(_tick_toogle) tick5ms();
_tick_big+=1;
if (_tick_big%20==0){
tick50ms();
}
}
void IODevice::tick5ms()
{
switch(_output_state) {
case SENDING:
if(_output_remainingTicks==0) {
bool output = (_output_frame & (1 << _output_currentBit));
digitalWrite(_output_pin, HIGH);
if (output) {
_output_remainingTicks = 8 + 1;
} else {
_output_remainingTicks = 4 + 1;
}
} else {
if(_output_remainingTicks==1) {
}
if(_output_remainingTicks==1) {
digitalWrite(_output_pin, LOW);
if(_output_currentBit==0) {
_output_state = END_FRAME;
} else {
_output_currentBit--;
}
}
}
_output_remainingTicks--;
break;
case END_FRAME:
_output_state = IDLE;
break;
case IDLE:
// nothing to do
break;
}
}
void IODevice::tick50ms()
{
if (_encountered_frame) _unconnected=false;
}
void IODevice::input_bitshift(const int8_t bit)
{
if(bit == -1) {
// Invalid bit == invalid frame
_input_frame = 0x0000;
_input_current_bit = 9;
} else {
if(bit == 1) _input_frame |= (1 << _input_current_bit);
if(_input_current_bit != 0) {
_input_current_bit--;
} else {
_input_received_frame = _input_frame;
_input_received_frame_is_available = true;
_input_frame = 0x0000;
_input_current_bit = 9;
}
}
}
void IODevice::input_level_push(int _input_time_at_level)
{
bool level = (_input_time_at_level>0);
if(!level) _input_time_at_level = -_input_time_at_level;
if(level) {
if(_input_time_at_level < 7) {
// Too short frame
input_bitshift(-1);
} else if(_input_time_at_level < 9) {
input_bitshift(0);
} else if(_input_time_at_level < 17) {
input_bitshift(1);
} else {
// Too long frame
input_bitshift(-1);
}
}
}
void IODevice::input_level_detect()
{
bool in = digitalRead(_input_pin);
if(_input_time_at_level > 0) {
if(!in) {
// lvl changed
input_level_push(_input_time_at_level);
_input_time_at_level = 0;
} else {
_input_time_at_level++;
}
} else if(_input_time_at_level < 0) {
if(in) {
// lvl changed
input_level_push(_input_time_at_level);
_input_time_at_level = 0;
} else {
_input_time_at_level--;
}
} else {
// _input_time_at_level == 0
if(in) {
_input_time_at_level++;
} else {
_input_time_at_level--;
}
}
}
void IODevice::set_encountered_frame()
{
_encountered_frame=true;
}
void IODevice::printFrame(const uint16_t frame)
{
for(int8_t i=9; i>=0; i--) {
char c = (frame & (1<<i))?'1':'0';
Serial.print(c);
}
//Serial.print("\r\n");
}
/*
boolean isConnected(IODevice* left, IODevice* right){
return (left._unconnected || right._unconnected)
}
*/
void io_setup(void)
{
pinMode(loc_pin, OUTPUT);
pinMode(glob_pin, OUTPUT);
digitalWrite(loc_pin,HIGH);
digitalWrite(glob_pin,HIGH);
}