forked from ali1234/raspi-teletext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.c
87 lines (73 loc) · 2.31 KB
/
buffer.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
/*
buffer.c - teletext packet fifo buffer
Copyright 2015 Alistair Buxton <[email protected]>
This file is part of raspi-teletext.
raspi-teletext 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.
raspi-teletext 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 raspi-teletext. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#define NBUFFERS 64
uint8_t tt_buffer[NBUFFERS][42];
volatile uint8_t buffer_head = 0;
volatile uint8_t buffer_tail = 0;
// Filler: Blank row in magazine 8 row 25
const uint8_t fill_buffer[42] = {
0xd0, 0xa1, /* Magazine 8, packet 25 */
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
};
void copy_packet(const uint8_t *src, uint8_t *dest)
{
int n, m;
for (n=0; n<42; n++) {
uint8_t b = *src++;
for (m=0; m<8; m++) {
*dest++ = b&1;
b = b>>1;
}
}
}
void get_packet(uint8_t *dest)
{
if (buffer_head == buffer_tail) {
copy_packet(fill_buffer, dest);
} else {
copy_packet(tt_buffer[buffer_tail], dest);
buffer_tail = (buffer_tail+1)%NBUFFERS;
}
}
void push_packet(uint8_t *src)
{
while (((buffer_head+1)%NBUFFERS) == buffer_tail) {
usleep(20000);
}
memcpy(tt_buffer[buffer_head], src, 42);
buffer_head = (buffer_head+1)%NBUFFERS;
}
int read_packets(void)
{
if (((buffer_head+1)%NBUFFERS) == buffer_tail) {
// wait one field for fifo to empty
usleep(20000);
return 1;
}
if (fread(tt_buffer[buffer_head], 42, 1, stdin) == 1) {
buffer_head = (buffer_head+1)%NBUFFERS;
return 1;
}
return 0;
}