Newer
Older
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
/*
* Fadecandy Firmware - USB Support
*
* Copyright (c) 2013 Micah Elizabeth Scott
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <string.h>
#include "usb_dev.h"
#include "fc_defs.h"
/*
* A buffer of references to USB packets.
*/
template <unsigned tSize>
struct fcPacketBuffer
{
usb_packet_t *packets[tSize];
fcPacketBuffer()
{
// Allocate packets. They'll have zero'ed contents initially.
for (unsigned i = 0; i < tSize; ++i) {
usb_packet_t *p = usb_malloc();
memset(p->buf, 0, sizeof p->buf);
packets[i] = p;
}
}
void store(unsigned index, usb_packet_t *packet)
{
if (index < tSize) {
// Store a packet, holding a reference to it.
usb_packet_t *prev = packets[index];
packets[index] = packet;
usb_free(prev);
} else {
// Error; ignore this packet.
usb_free(packet);
}
}
};
/*
* Framebuffer
*/
struct fcFramebuffer : public fcPacketBuffer<PACKETS_PER_FRAME>
{
const uint8_t *pixel(unsigned index)
{
return &packets[index / PIXELS_PER_PACKET]->buf[1 + (index % PIXELS_PER_PACKET) * 3];
}
};
/*
* Color Lookup table
*/
struct fcColorLUT : public fcPacketBuffer<PACKETS_PER_LUT>
{
const unsigned entry(unsigned channel, unsigned value)
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
{
unsigned index = (channel << 8) | value;
const uint8_t *p = &packets[index / LUTENTRIES_PER_PACKET]->buf[2 + (index % LUTENTRIES_PER_PACKET) * 2];
return *(uint16_t*)p;
}
};
/*
* All USB-writable buffers
*/
struct fcBuffers
{
fcFramebuffer fb[3]; // Triple-buffered video frames
fcColorLUT lut[2]; // Double-buffered color LUTs
fcFramebuffer *fbPrev; // Frame we're interpolating from
fcFramebuffer *fbNext; // Frame we're interpolating to
fcFramebuffer *fbNew; // Partial frame, getting ready to become fbNext
fcColorLUT *lutCurrent; // Active LUT
fcColorLUT *lutNew; // Partial LUT, not yet finalized
fcBuffers()
{
fbPrev = &fb[0];
fbNext = &fb[1];
fbNew = &fb[2];
lutCurrent = &lut[0];
lutNew = &lut[1];
}
void handleUSB();
private:
void finalizeFramebuffer();
void finalizeLUT();
};