-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathmodDotstar.c
311 lines (245 loc) · 7.27 KB
/
modDotstar.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Copyright (c) 2016-2018 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Runtime.
*
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Runtime 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "xsmc.h"
#include "xsHost.h"
#include "commodettoBitmap.h"
#include "commodettoPocoBlit.h"
#include "commodettoPixelsOut.h"
#include "mc.xs.h" // for xsID_ values
#include "mc.defines.h"
#include "modSPI.h"
#ifndef MODDEF_DOTSTAR_BRIGHTNESS
#define MODDEF_DOTSTAR_BRIGHTNESS (64)
#endif
#ifndef MODDEF_DOTSTAR_HZ
#define MODDEF_DOTSTAR_HZ (20000000)
#endif
#ifdef __ZEPHYR__
/* TODO: K64F Test */
#define SPI_DEVICE "SPI_0"
#elif gecko
#define AUTO_CHIP_SELECT 0
#define MOSI_PIN 0
#define CLK_PIN 2
#else // linux (raspberry pi)
// PI wiring
#endif
typedef struct {
PixelsOutDispatch dispatch;
modSPIConfigurationRecord spiConfig;
uint16_t width;
uint16_t height;
uint8_t format;
uint8_t brightness[256];
} spiDisplayRecord, *spiDisplay;
static void dotstarChipSelect(uint8_t active, modSPIConfiguration config);
static void dotstarCommand(spiDisplay sd, uint8_t command, const uint8_t *data, uint16_t count);
//@@ MODDEF
void setBrightness(spiDisplay sd, int brightness); // 0 to 255
static void dotstarBegin(void *refcon, CommodettoCoordinate x, CommodettoCoordinate y, CommodettoDimension w, CommodettoDimension h);
static void dotstarContinue(void *refcon);
static void dotstarEnd(void *refcon);
static void dotstarSend(void *refcon, PocoPixel *pixels, uint32_t byteLength);
static void dotstarAdaptInvalid(void *refcon, CommodettoRectangle r);
static void dotstarSend_16LE(PocoPixel *pixels, int byteLength, void *refcon);
static const PixelsOutDispatchRecord gPixelsOutDispatch_16LE ICACHE_RODATA_ATTR = {
dotstarBegin,
dotstarContinue,
dotstarEnd,
dotstarSend_16LE,
dotstarAdaptInvalid
};
void xs_DotStar_destructor(void *data)
{
if (data) {
spiDisplay sd = data;
modSPIUninit(&sd->spiConfig);
c_free(data);
}
}
void xs_DotStar(xsMachine *the)
{
int format;
spiDisplay sd;
int csPin;
xsmcVars(1);
if (xsmcHas(xsArg(0), xsID_pixelFormat)) {
xsmcGet(xsVar(0), xsArg(0), xsID_pixelFormat);
format = xsmcToInteger(xsVar(0));
}
else
format = kCommodettoBitmapFormat;
if (kCommodettoBitmapRGB565LE != format)
xsUnknownError("bad format");
sd = c_calloc(1, sizeof(spiDisplayRecord));
if (!sd)
xsUnknownError("no memory");
xsmcSetHostData(xsThis, sd);
modSPIConfig(sd->spiConfig, MODDEF_DOTSTAR_HZ, MODDEF_DOTSTAR_SPI_PORT,
NULL, -1, dotstarChipSelect);
modSPIInit(&sd->spiConfig);
xsmcGet(xsVar(0), xsArg(0), xsID_width);
sd->width = (uint16_t)xsmcToInteger(xsVar(0));
xsmcGet(xsVar(0), xsArg(0), xsID_height);
sd->height = (uint16_t)xsmcToInteger(xsVar(0));
sd->format = (uint8_t)format;
sd->dispatch = (PixelsOutDispatch)&gPixelsOutDispatch_16LE;
setBrightness(sd, MODDEF_DOTSTAR_BRIGHTNESS);
}
void xs_DotStar_begin(xsMachine *the)
{
spiDisplay sd = xsmcGetHostData(xsThis);
CommodettoCoordinate x = (CommodettoCoordinate)xsmcToInteger(xsArg(0));
CommodettoCoordinate y = (CommodettoCoordinate)xsmcToInteger(xsArg(1));
CommodettoDimension w = (CommodettoDimension)xsmcToInteger(xsArg(2));
CommodettoDimension h = (CommodettoDimension)xsmcToInteger(xsArg(3));
dotstarBegin(sd, x, y, w, h);
}
void xs_DotStar_send(xsMachine *the)
{
spiDisplay sd = xsmcGetHostData(xsThis);
int argc = xsmcArgc;
const uint8_t *data;
xsUnsignedValue count;
xsmcGetBufferReadable(xsArg(0), (void **)&data, &count);
if (argc > 1) {
xsIntegerValue offset = xsmcToInteger(xsArg(1));
if ((xsUnsignedValue)offset >= count)
xsUnknownError("bad offset");
data += offset;
count -= offset;
if (argc > 2) {
xsIntegerValue c = xsmcToInteger(xsArg(2));
if (c > count)
xsUnknownError("bad count");
count = c;
}
}
(sd->dispatch->doSend)((PocoPixel *)data, count, sd);
}
void xs_DotStar_end(xsMachine *the)
{
spiDisplay sd = xsmcGetHostData(xsThis);
dotstarEnd(sd);
}
void xs_DotStar_adaptInvalid(xsMachine *the)
{
spiDisplay sd = xsmcGetHostData(xsThis);
CommodettoRectangle invalid;
if (NULL == sd->dispatch->doAdaptInvalid)
return;
invalid = xsmcGetHostChunk(xsArg(0));
dotstarAdaptInvalid(sd, invalid);
}
void xs_DotStar_pixelsToBytes(xsMachine *the)
{
int count = xsmcToInteger(xsArg(0));
xsmcSetInteger(xsResult, ((count * kCommodettoPixelSize) + 7) >> 3);
}
void xs_DotStar_get_pixelFormat(xsMachine *the)
{
spiDisplay sd = xsmcGetHostData(xsThis);
xsmcSetInteger(xsResult, sd->format);
}
void xs_DotStar_get_width(xsMachine *the)
{
spiDisplay sd = xsmcGetHostData(xsThis);
xsmcSetInteger(xsResult, sd->width);
}
void xs_DotStar_get_height(xsMachine *the)
{
spiDisplay sd = xsmcGetHostData(xsThis);
xsmcSetInteger(xsResult, sd->height);
}
void xs_DotStar_get_c_dispatch(xsMachine *the)
{
xsResult = xsThis;
}
// caller provides little-endian pixels
//@@ could double buffer "buffer" to get some aysnc sending in this loop
void dotstarSend_16LE(PocoPixel *pixels, int byteLength, void *refcon)
{
spiDisplay sd = refcon;
uint8_t *brightness = sd->brightness;
if (byteLength < 0) byteLength = -byteLength;
while (byteLength > 0) {
uint8_t buffer[16 * 4];
uint8_t *bgr = buffer;
uint16_t count = (byteLength >= 32) ? 32 : byteLength;
uint16_t counter = count >> 1;
while (counter--) {
PocoPixel pixel = *pixels++;
uint8_t t;
*bgr++ = 0xff;
t = pixel & 0x1F;
*bgr++ = brightness[(t << 3) | (t >> 3)];
t = (pixel >> 5) & 0x3F;
*bgr++ = brightness[(t << 2) | (t >> 4)];
t = pixel >> 11;
*bgr++ = brightness[(t << 3) | (t >> 3)];
}
modSPITx(&sd->spiConfig, (void *)buffer, count << 1);
modSPIFlush();
byteLength -= count;
}
}
void dotstarChipSelect(uint8_t active, modSPIConfiguration config)
{
}
void dotstarBegin(void *refcon, CommodettoCoordinate x, CommodettoCoordinate y, CommodettoDimension w, CommodettoDimension h)
{
spiDisplay sd = refcon;
uint32_t zeros = 0;
modSPITx(&sd->spiConfig, (void *)&zeros, sizeof(zeros));
modSPIFlush();
}
void dotstarContinue(void *refcon)
{
dotstarEnd(refcon);
}
void dotstarEnd(void *refcon)
{
spiDisplay sd = refcon;
uint8_t bytes[16];
int count = (sd->width + 15) / 16;
c_memset(bytes, 0xff, sizeof(bytes));
while (count) {
int use = count;
if (count > sizeof(bytes))
count = sizeof(bytes);
modSPITx(&sd->spiConfig, (void *)bytes, use);
modSPIFlush();
count -= use;
}
}
void dotstarAdaptInvalid(void *refcon, CommodettoRectangle invalid)
{
spiDisplay sd = refcon;
invalid->x = 0;
invalid->y = 0;
invalid->w = sd->width;
invalid->h = sd->height;
}
void setBrightness(spiDisplay sd, int brightness) // 0 to 255
{
int i;
for (i = 0; i < 256; i++)
sd->brightness[i] = (i * brightness) / 255;
}