-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathavr_pocsag.ino
161 lines (137 loc) · 2.99 KB
/
avr_pocsag.ino
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
#include <Pocsag.h>
#include <TimerOne.h>
#define FSK_PIN 11
void setup() {
Serial.begin(9600);
pinMode(FSK_PIN, OUTPUT);
initTimer();
}
int SPEED = 512;
bool invert = false;
Pocsag pocsag;
int state = 0;
int pointer = 0;
int point_bit = -1;
uint8_t *current_bits = (uint8_t*) 0x0;
void loop() {
// data
long int address;
int addresssource;
int repeat;
char textmsg[42]; // to store text message;
int msgsize;
// init var
int rc;
int state=1;
address=0;
addresssource=0;
msgsize=0;
while (state >= 0 and state < 8) {
char c;
// loop until we get a character from serial input
while (!Serial.available()) {
}; // end while
c=Serial.read();
if (state == 1) {
if ((c >= '0') && (c <= '9')) {
// digit -> first digit of address. Go to state 2 and process
state=2;
// continue to state 2 without fetching next char
} else {
state=-1;
continue;
}; // end else - if
}
// state 2: address ("0" to "9")
if (state == 2) {
if ((c >= '0') && (c <= '9')) {
long int newaddress;
newaddress = address * 10 + (c - '0');
if (newaddress <= 0x1FFFFF) {
// valid address
address=newaddress;
} else {
// address to high.
state=-2;
}; // end else - if
} else if (c == ' ') {
// received space, go to next field (address source)
state=3;
} else {
state=-3;
};
// get next char
continue;
}; // end state 2
// state 3: address source: one single digit from 0 to 3
if (state == 3) {
if ((c >= '0') && (c <= '3')) {
addresssource= c - '0';
state=4;
} else {
state=-4;
}; // end if
continue;
}; // end state 3
if (state == 4) {
if ((c >= '0') && (c <= '9')) {
repeat=c - '0';
// move to next state
state=5;
} else {
state=-5;
};
continue;
};
if(state == 5) {
rc = pocsag.CreatePocsag(address,addresssource,textmsg,0,1);
if (!rc) {
state = -6;
delay(10000);
} else {
for (int l=-1; l < repeat; l++) {
state = 6;
while(state == 6) {
delay(100);
}
delay(3000);
};
}
}
}
Serial.write(state);
}
void initTimer()
{
Timer1.stop();
long bitPeriod = (long) ((1.0f/SPEED) * 1000000); //micros
Timer1.initialize(bitPeriod);
Timer1.attachInterrupt(sender);
}
void sender() {
if(state != 6)
return;
if(point_bit == -1 && pointer < pocsag.GetSize()) {
point_bit = 0;
if(current_bits == (uint8_t*) 0x0)
current_bits = (uint8_t *)pocsag.GetMsgPointer();
else
current_bits++;
pointer++;
}
if(pointer >= pocsag.GetSize()) {
point_bit=-1;
pointer = 0;
current_bits = (uint8_t*) 0x0;
state = 7;
digitalWrite(FSK_PIN, 0);
return;
}
if(point_bit > -1) {
digitalWrite(FSK_PIN, ((*current_bits) >> point_bit) & 0x1);
point_bit++;
}
if(point_bit >= 8){
point_bit = -1;
}
}