-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfc_usb.h
157 lines (130 loc) · 4.14 KB
/
fc_usb.h
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
/*
* 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 "WProgram.h"
#include "usb_dev.h"
#include "fc_defs.h"
// Enforce inlining, so we can take advantage of inter-procedural optimization
#define ALWAYS_INLINE __attribute__ ((always_inline))
/*
* A buffer of references to USB packets.
*/
template <unsigned tSize>
struct fcPacketBuffer
{
usb_packet_t *packets[tSize];
uint32_t timestamp;
fcPacketBuffer()
{
// Allocate packets. They'll have zero'ed contents initially.
for (unsigned i = 0; i < tSize; ++i) {
usb_packet_t *p = usb_malloc();
for (unsigned j = 0; j < sizeof p->buf; j++)
p->buf[j] = 0;
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>
{
ALWAYS_INLINE 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>
{
ALWAYS_INLINE const unsigned entry(unsigned index)
{
const uint8_t *p = &packets[index / LUTENTRIES_PER_PACKET]->buf[2 + (index % LUTENTRIES_PER_PACKET) * 2];
return *(uint16_t*)p;
}
};
/*
* Configuration flag values
*/
#define CFLAG_NO_DITHERING (1 << 0)
#define CFLAG_NO_INTERPOLATION (1 << 1)
#define CFLAG_NO_ACTIVITY_LED (1 << 2)
#define CFLAG_LED_CONTROL (1 << 3)
/*
* Data type for current color LUT
*/
union fcLinearLUT
{
uint16_t entries[LUT_TOTAL_SIZE];
struct {
uint16_t r[LUT_CH_SIZE];
uint16_t g[LUT_CH_SIZE];
uint16_t b[LUT_CH_SIZE];
};
};
/*
* All USB-writable buffers
*/
struct fcBuffers
{
fcFramebuffer *fbPrev; // Frame we're interpolating from
fcFramebuffer *fbNext; // Frame we're interpolating to
fcFramebuffer *fbNew; // Partial frame, getting ready to become fbNext
fcFramebuffer fb[3]; // Triple-buffered video frames
fcColorLUT lutNew; // Partial LUT, not yet finalized
static fcLinearLUT lutCurrent; // Active LUT, linearized for efficiency
uint8_t flags; // Configuration flags
uint8_t packetIndexHigh;
fcBuffers()
{
fbPrev = &fb[0];
fbNext = &fb[1];
fbNew = &fb[2];
}
// Interrupt context
bool handleUSB(usb_packet_t *packet);
// Main loop context
void finalizeFrame();
private:
void finalizeFramebuffer();
void finalizeLUT();
// Status communicated between handleUSB() and finalizeFrame()
bool handledAnyPacketsThisFrame;
bool pendingFinalizeFrame;
bool pendingFinalizeLUT;
};