-
Notifications
You must be signed in to change notification settings - Fork 23
/
cea608buffer.c
81 lines (68 loc) · 2.11 KB
/
cea608buffer.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
/*
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][4];
volatile uint8_t buffer_head = 0;
volatile uint8_t buffer_tail = 0;
const uint8_t fill_buffer[3] = {0x00, 0x00};
void copy_packet(const uint8_t *src, uint8_t *dest)
{
int n, m;
for (n=0; n<2; n++) {
uint8_t b = *src++;
for (m=0; m<8; m++) {
*dest++ = b&1;
*dest++ = b&1;
b = b>>1;
}
}
}
void get_packet(uint8_t *desta, uint8_t *destb)
{
if (buffer_head == buffer_tail) {
copy_packet(fill_buffer, desta);
copy_packet(fill_buffer, destb);
} else {
copy_packet(tt_buffer[buffer_tail], desta);
copy_packet(tt_buffer[buffer_tail]+2, destb);
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, 4);
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], 4, 1, stdin) == 1) {
buffer_head = (buffer_head+1)%NBUFFERS;
return 1;
}
return 0;
}