-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver.c
217 lines (169 loc) · 4.48 KB
/
receiver.c
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
207
208
209
210
211
212
213
214
215
216
217
/*
* klawiatura.c
*
* Created: 2011-11-15 16:44:01
* Author: pn264889
*/
#include "config.h"
#include <avr/io.h>
#include <avr/delay.h>
#include <avr/interrupt.h>
#include "commons.h"
#include "lcd.h"
#include "mic.h"
#include "counter.h"
#include "morse.h"
#define EMA_RATE 4
#define EMA_THRESHOLD_OFFSET 10;
uint16_t EmaThreshold = 0x1FF;
enum ReceiverState {
Idle = 0,
Reading = 1,
Space = 2,
Message = 3,
OverFlow = 4
};
volatile enum ReceiverState _state = Idle;
volatile uint16_t _highRepeatCount = 0;
volatile uint16_t _lowRepeatCount = 0;
uint8_t WorkMode = 0;
volatile uint16_t _avgVal=0;
uint16_t _previousAvgVal=0;
#define IN_BUFFER_SIZE 16
uint8_t _bufferStart = 0;
uint8_t _bufferEnd = 0;
uint8_t _bufferWriteBit = 0; // next bit read position is _inputBuffer[_bufferEnd][_bufferWriteBit]
char _inputBuffer[IN_BUFFER_SIZE][MORSE_CODE_SIZE];
uint8_t incrementBuffer() {
_bufferEnd = (_bufferEnd + 1) % IN_BUFFER_SIZE;
_bufferWriteBit = 0;
if (_bufferEnd == _bufferStart) {
_state = OverFlow;
return 0;
}
return 1;
}
void incrementBufferAndHandleState(enum ReceiverState nextState, uint8_t c) {
_inputBuffer[_bufferEnd][_bufferWriteBit] = c;
if (incrementBuffer()) {
_lowRepeatCount = 0;
_state = nextState;
}
}
ISR(TIMER0_COMP_vect) {
}
ISR(ADC_vect) {
const uint16_t micVal = micRead();
_previousAvgVal = _avgVal;
if (_avgVal < micVal)
_avgVal = micVal;
else {
uint16_t diffVal = (_avgVal >> EMA_RATE);
// if AvgVal >> EMA_RATE would be underflow
if (diffVal == 0) {
// asserting that never diffVal > AvgVal
diffVal = _avgVal > EMA_RATE ? EMA_RATE : _avgVal;
}
// avg_n+1 = (avg_n*(E-1))/E + mic/E
// avg_n+1 = avg_n - avg_n/E + mic/E
_avgVal = _avgVal - diffVal + (micVal >> EMA_RATE);
}
if (CHECK(WorkMode, DETECT_MAX_MODE) && _avgVal > EmaThreshold)
EmaThreshold = _avgVal + EMA_THRESHOLD_OFFSET;
if (CHECK(WorkMode, RESET_MAX_MODE)) {
_state = Idle;
_bufferStart = _bufferEnd = 0;
EmaThreshold = 0;
}
// DEBUG led output
if (_avgVal > EmaThreshold)
LD_DDR = 0x00;
else
LD_DDR = 0xFF;
if (!CHECK(WorkMode, SETUP_MODE)) {
if (_state == OverFlow)
return;
if (_avgVal > EmaThreshold) {
++_highRepeatCount;
_lowRepeatCount = 0;
}
else if (_previousAvgVal > EmaThreshold) {
// Previously we were over the threshold, now we are under
// so we have finished receiving a bit
int8_t receivedBit = -1;
if (_highRepeatCount >= _dahRepeat) {
receivedBit = 1;
}
else if (_highRepeatCount >= _ditRepeat) {
receivedBit = 0;
}
if (receivedBit >= 0) {
_inputBuffer[_bufferEnd][_bufferWriteBit++] = receivedBit ? '-' : '.';
_state = Reading;
}
_highRepeatCount = 0;
_lowRepeatCount = 0;
}
else if (_state == Reading) {
++_lowRepeatCount;
if (_lowRepeatCount >= _charSpaceRepeat) {
incrementBufferAndHandleState(Space, 0);
}
}
else if (_state == Space) {
++_lowRepeatCount;
if (_lowRepeatCount >= _wordSpaceRepeat) {
incrementBufferAndHandleState(Message, Space);
}
}
else if (_state == Message) {
++_lowRepeatCount;
if (_lowRepeatCount >= _messageSpaceRepeat) {
incrementBufferAndHandleState(Idle, Message);
}
}
}
}
int main(void)
{
morseInit();
lcdInit();
micInit();
counterInit();
LD_DDR = 0xFF;
SWITCH_DDR = 0x00;
SWITCH_PORT = 0xFF;
lcdString("Hello world!\0");
while(1)
{
WorkMode = ~SWITCH_PIN;
if (CHECK(WorkMode, SETUP_MODE))
{
lcdClear();
if (WorkMode == 0)
lcdInt(_avgVal);
else
lcdInt(EmaThreshold);
lcdData('X');
}
else {
if (_state == OverFlow) {
lcdData('X');
}
// if there is something to read, receive it
else if (_bufferStart != _bufferEnd) {
//lcdData('#');
//lcdString(_inputBuffer[_bufferStart]);
const char *message = _inputBuffer[_bufferStart];
if (message[0] == Space)
lcdData(' ');
else if (message[0] == Message)
lcdClear();
else if (message[0] != 0)
lcdData(getMorseChar(message));
_bufferStart = (_bufferStart + 1) % IN_BUFFER_SIZE;
}
}
_delay_ms(50);
}
}