forked from muccc/formica_rev1_muc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ir-tx-data.c
146 lines (123 loc) · 3.15 KB
/
ir-tx-data.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
/* Copyright 2008 Stephen English, Jeffrey Gough, Alexis Johnson,
Robert Spanton and Joanna A. Sun.
This file is part of the Formica robot firmware.
The Formica robot firmware 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.
The Formica robot firmware 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.
You should have received a copy of the GNU General Public License
along with the Formica robot firmware.
If not, see <http://www.gnu.org/licenses/>. */
#include "ir-tx-data.h"
#include "freq.h"
#include "device.h"
#include "types.h"
#include "net-tx.h"
#include <stddef.h>
#include "smbus_pec.h"
#include "ir.h"
/* Generates the next byte to transmit */
static bool next_byte( uint8_t* b );
#if CONF_TX_SEQ==1
uint8_t ir_tx_next_symbol( void )
{
static uint8_t last_sym = 0;
uint8_t sym;
if( last_sym == NFREQ-1 )
sym = 0;
else
sym = last_sym+1;
last_sym = sym;
return sym;
}
#else
uint8_t ir_tx_next_symbol( void )
{
static uint8_t curbyte;
/* How many symbols have been transmitted from the current byte */
static uint8_t cb_pos = 0;
static uint8_t last_sym = 255;
uint8_t sym;
if( cb_pos == 0 ) {
/* Send the start byte symbol */
sym = 0;
if( !next_byte( &curbyte ) )
return INV_SYM;
} else {
/* Grab the data from the byte */
sym = curbyte & SYM_MASK;
curbyte >>= NBITS;
/* Now munge up the symbol so that we don't
transmit the same one again!
(You know... the rotating thing) */
/* First, add 1 because symbol 0 is the 'start bit' symbol */
sym++;
/* Avoid the symbol that was transmitted last */
if( sym >= last_sym )
sym++;
}
cb_pos++;
if( cb_pos == SYMBOLS_PER_BYTE+1 )
cb_pos = 0;
last_sym = sym;
return sym;
}
#endif
static bool next_byte( uint8_t* b )
{
/* The current packet that's being transmitted */
static const uint8_t *cur_packet = NULL;
/* The length of the current packet */
static uint8_t cp_len = 0;
if( cur_packet == NULL )
cur_packet = net_tx_get_next_packet( &cp_len );
if( cur_packet != NULL ) {
static uint8_t p = 0;
static uint8_t checksum = 0;
static bool escaped = FALSE;
if( p == 0 ) {
/* Send the 'start of frame' byte */
*b = 0x7E;
checksum = 0;
}
else {
if( p == 1 ) {
*b = cp_len;
if( !escaped )
checksum_add(checksum, *b);
}
else if( p < (cp_len + 2) ) {
*b = cur_packet[ p - 2 ];
if( !escaped )
checksum_add(checksum, *b);
}
else
/* The checksum */
*b = checksum;
if( !escaped && (*b == 0x7e || *b == 0x7d) ) {
escaped = TRUE;
*b = 0x7d;
}
else if( escaped ) {
escaped = FALSE;
*b ^= 0x20;
}
}
if( !escaped ) {
if( p == cp_len + 2 )
{
p = 0;
cur_packet = net_tx_get_next_packet( &cp_len );
}
else
p++;
}
return TRUE;
}
else
return FALSE;
}