-
Notifications
You must be signed in to change notification settings - Fork 2
/
daisy.h
344 lines (288 loc) · 8.62 KB
/
daisy.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
{{license}}
#ifndef __JSON2DAISY_{{name|upper}}_H__
#define __JSON2DAISY_{{name|upper}}_H__
{% if som == 'seed' %}
#include "daisy_seed.h"
#include "dev/codec_ak4556.h"
{% elif som == 'patch_sm' %}
#include "daisy_patch_sm.h"
{% elif som == 'petal_125b_sm' %}
#include "daisy_petal_125b_sm.h"
{% endif %}
{{display_conditional}}
#define ANALOG_COUNT {{analogcount}}
namespace json2daisy {
{{non_class_declarations}}
struct Daisy{{ name|capitalize }} {
/** Initializes the board according to the JSON board description
* \param boost boosts the clock speed from 400 to 480 MHz
*/
void Init(bool boost=true)
{
{% if som == 'seed' %}
som.Configure();
som.Init(boost);
{% else %}
som.Init();
{% endif %}
{% if init != '' %}
{{init}}
{% endif %}
{% if i2c != '' %}
// i2c
{{ i2c }}
{% endif %}
{% if PCA9685 != '' %}
// LED Drivers
{{ PCA9685 }}
{% endif %}
{% if Switch != '' %}
// Switches
{{ Switch }}
{% endif %}
{% if Switch3 != '' %}
// SPDT Switches
{{ Switch3 }}
{% endif %}
{% if CD4021 != '' %}
// Muxes
{{ CD4021 }}
{% endif %}
{% if GateIn != '' %}
// Gate ins
{{ GateIn }}
{% endif %}
{% if Encoder != '' %}
// Rotary encoders
{{ Encoder }}
{% endif %}
{% if init_single != '' %}
// Single channel ADC initialization
{{ init_single }}
{% endif %}
{% if som == 'seed' %}
{% if analogcount > 0 %}
som.adc.Init(cfg, ANALOG_COUNT);
{% endif %}
{% endif %}
{% if ctrl_init != '' %}
// AnalogControl objects
{{ ctrl_init }}
{% endif %}
{% if CD4051AnalogControl != '' %}
// Multiplexed AnlogControl objects
{{ CD4051AnalogControl }}
{% endif %}
{% if Led != '' %}
// LEDs
{{ Led }}
{% endif %}
{% if RgbLed != '' %}
// RBG LEDs
{{ RgbLed }}
{% endif %}
{% if GateOut != '' %}
// Gate outs
{{ GateOut }}
{% endif %}
{% if CVOuts != '' %}
// DAC
{{ CVOuts }}
{% endif %}
{% if display != '' %}
// Display
{{ display }}
{% endif %}
{% if MotorShield != '' %}
// Adafruit Motor Shield
{{ MotorShield }}
{% endif %}
{% if StepperMotor != '' %}
// Stepper motor pointer from the Adafruit Motor Shield
{{ StepperMotor }}
{% endif %}
{% if DcMotor != '' %}
// DC motor pointer from the Adafruit Motor Shield
{{ DcMotor }}
{% endif %}
{% if Bme280 != '' %}
// BME280 pressure/temperature/humidity sensor
{{ Bme280 }}
{% endif %}
{% if HallSensor != '' %}
// Hall sensor
{{ HallSensor }}
{% endif %}
{% if Tlv493d != '' %}
// Accelerometer
{{ Tlv493d }}
{% endif %}
{% if Mpr121 != '' %}
// Capacitive sensor
{{ Mpr121 }}
{% endif %}
{% if Apds9960 != '' %}
// Gesture / color sensor
{{ Apds9960 }}
{% endif %}
{% if Bmp390 != '' %}
// Bmp390 pressure / temperature sensor
{{ Bmp390 }}
{% endif %}
{% if Dps310 != '' %}
// Dps310 pressure / temperature sensor
{{ Dps310 }}
{% endif %}
{% if Vl53l1x != '' %}
// Vl53l1x time of flight sensor
{{ Vl53l1x }}
{% endif %}
{% if Vl53l0x != '' %}
// Vl53l0x time of flight sensor
{{ Vl53l0x }}
{% endif %}
{% if NeoTrellis != '' %}
// Adafruit Neo Trellis
{{ NeoTrellis }}
{% endif %}
{% if Bno055 != '' %}
// Bno055 9-DOF omega sensor
{{ Bno055 }}
{% endif %}
{% if Icm20948 != '' %}
// Icm20948 9-DOF sensor
{{ Icm20948 }}
{% endif %}
{% if som == 'seed' and external_codecs|length > 0 %}
// External Codec Initialization
daisy::SaiHandle::Config sai_config[{{ 1 + external_codecs|length }}];
// Internal Codec
sai_config[0].periph = daisy::SaiHandle::Config::Peripheral::SAI_1;
sai_config[0].sr = daisy::SaiHandle::Config::SampleRate::SAI_48KHZ;
sai_config[0].bit_depth = daisy::SaiHandle::Config::BitDepth::SAI_24BIT;
sai_config[0].a_sync = daisy::SaiHandle::Config::Sync::MASTER;
sai_config[0].b_sync = daisy::SaiHandle::Config::Sync::SLAVE;
sai_config[0].a_dir = daisy::SaiHandle::Config::Direction::TRANSMIT;
sai_config[0].b_dir = daisy::SaiHandle::Config::Direction::RECEIVE;
sai_config[0].pin_config.fs = {DSY_GPIOE, 4};
sai_config[0].pin_config.mclk = {DSY_GPIOE, 2};
sai_config[0].pin_config.sck = {DSY_GPIOE, 5};
sai_config[0].pin_config.sa = {DSY_GPIOE, 6};
sai_config[0].pin_config.sb = {DSY_GPIOE, 3};
{% for codec in external_codecs %}
sai_config[{{loop.index}}].periph = daisy::SaiHandle::Config::Peripheral::{{codec.periph}};
sai_config[{{loop.index}}].sr = daisy::SaiHandle::Config::SampleRate::SAI_48KHZ;
sai_config[{{loop.index}}].bit_depth = daisy::SaiHandle::Config::BitDepth::SAI_24BIT;
sai_config[{{loop.index}}].a_sync = daisy::SaiHandle::Config::Sync::{{codec.a_sync}};
sai_config[{{loop.index}}].b_sync = daisy::SaiHandle::Config::Sync::{{codec.b_sync}};
sai_config[{{loop.index}}].a_dir = daisy::SaiHandle::Config::Direction::{{codec.a_dir}};
sai_config[{{loop.index}}].b_dir = daisy::SaiHandle::Config::Direction::{{codec.b_dir}};
sai_config[{{loop.index}}].pin_config.fs = som.GetPin({{codec.pin.fs}});
sai_config[{{loop.index}}].pin_config.mclk = som.GetPin({{codec.pin.mclk}});
sai_config[{{loop.index}}].pin_config.sck = som.GetPin({{codec.pin.sck}});
sai_config[{{loop.index}}].pin_config.sa = som.GetPin({{codec.pin.sa}});
sai_config[{{loop.index}}].pin_config.sb = som.GetPin({{codec.pin.sb}});
{% endfor %}
daisy::SaiHandle sai_handle[{{ 1 + external_codecs|length }}];
sai_handle[0].Init(sai_config[0]);
{% for codec in external_codecs %}
sai_handle[{{loop.index}}].Init(sai_config[{{loop.index}}]);
{% endfor %}
dsy_gpio_pin codec_reset_pin = som.GetPin(29);
daisy::Ak4556::Init(codec_reset_pin);
daisy::AudioHandle::Config cfg;
cfg.blocksize = 48;
cfg.samplerate = daisy::SaiHandle::Config::SampleRate::SAI_48KHZ;
cfg.postgain = 0.5f;
som.audio_handle.Init(
cfg,
sai_handle[0]
{% for codec in external_codecs %}
,sai_handle[{{loop.index}}]
{% endfor %}
);
{% endif %}
{% if som == 'seed' %}
{% if analogcount > 0 %}
som.adc.Start();
{% endif %}
{% endif %}
}
/** Handles all the controls processing that needs to occur at the block rate
*
*/
void ProcessAllControls()
{
{% if process != '' %}
{{ process }}
{% endif %}
{% if som != 'seed' %}
som.ProcessAllControls();
{% endif %}
}
/** Handles all the maintenance processing. This should be run last within the audio callback.
*
*/
void PostProcess()
{
{{postprocess}}
{% if som == 'petal_125b_sm' %}
som.UpdateLeds();
{% endif %}
}
/** Handles processing that shouldn't occur in the audio block, such as blocking transfers
*
*/
void LoopProcess()
{
{{loopprocess}}
}
/** Sets the audio sample rate
* \param sample_rate the new sample rate in Hz
*/
void SetAudioSampleRate(size_t sample_rate)
{
{% if som == 'seed' or som == 'petal_125b_sm' %}
daisy::SaiHandle::Config::SampleRate enum_rate;
if (sample_rate >= 96000)
enum_rate = daisy::SaiHandle::Config::SampleRate::SAI_96KHZ;
else if (sample_rate >= 48000)
enum_rate = daisy::SaiHandle::Config::SampleRate::SAI_48KHZ;
else if (sample_rate >= 32000)
enum_rate = daisy::SaiHandle::Config::SampleRate::SAI_32KHZ;
else if (sample_rate >= 16000)
enum_rate = daisy::SaiHandle::Config::SampleRate::SAI_16KHZ;
else
enum_rate = daisy::SaiHandle::Config::SampleRate::SAI_8KHZ;
som.SetAudioSampleRate(enum_rate);
{% elif som == 'patch_sm' %}
som.SetAudioSampleRate(sample_rate);
{% endif %}
{{hidupdaterates}}
}
/** Sets the audio block size
* \param block_size the new block size in words
*/
inline void SetAudioBlockSize(size_t block_size)
{
som.SetAudioBlockSize(block_size);
}
/** Starts up the audio callback process with the given callback
*
*/
inline void StartAudio(daisy::AudioHandle::AudioCallback cb)
{
som.StartAudio(cb);
}
/** This is the board's "System On Module" */
{{som_class}} som;
{% if som == 'seed' %}
daisy::AdcChannelConfig cfg[ANALOG_COUNT];
{% endif %}
// I/O Components
{{comps}}
{{dispdec}}
{{midi}}
};
} // namspace json2daisy
#endif // __JSON2DAISY_{{name|upper}}_H__