forked from sensorium/Mozzi
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMozziGuts_impl_template.hpp
More file actions
136 lines (119 loc) · 6.9 KB
/
MozziGuts_impl_template.hpp
File metadata and controls
136 lines (119 loc) · 6.9 KB
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
/*
* MozziGuts.cpp
*
* Copyright 2012 Tim Barrass.
*
* This file is part of Mozzi.
*
* Mozzi by Tim Barrass is licensed under a Creative Commons
* Attribution-NonCommercial-ShareAlike 4.0 International License.
*
*/
/** README!
* This file is meant to be used as a template when adding support for a new platform. Please read these instructions, first.
*
* Files involved:
* 1. Modify hardware_defines.h, adding a macro to detect your target platform
* 2. Modify MozziGuts.cpp to include MozziGuts_impl_YOURPLATFORM.hpp
* 3. Modify MozziGuts.h to include AudioConfigYOURPLATFORM.h
* 4. Copy this file to MozziGuts_impl_YOURPLATFORM.hpp and adjust as necessary
* (If your platform is very similar to an existing port, it may instead be better to modify the existing MozziGuts_impl_XYZ.hpp/AudioConfigYOURPLATFORM.h,
* instead of steps 2-3.).
* Some platforms may need small modifications to other files as well, e.g. mozzi_pgmspace.h
*
* How to implement MozziGuts_impl_YOURPLATFORM.hpp:
* - Follow the NOTEs provided in this file
* - Read the doc at the top of AudioOutput.h for a better understanding of the basic audio output framework
* - Take a peek at existing implementations for other hardware (e.g. TEENSY3/4 is rather complete while also simple at the time of this writing)
* - Wait for more documentation to arrive
* - Ask when in doubt
* - Don't forget to provide a PR when done (it does not have to be perfect; e.g. many ports skip analog input, initially)
*/
// The main point of this check is to document, what platform & variants this implementation file is for.
#if !(IS_MYPLATFORM())
# error "Wrong implementation included for this platform"
#endif
// Add platform specific includes and declarations, here
////// BEGIN analog input code ////////
/** NOTE: This section deals with implementing (fast) asynchronous analog reads, which form the backbone of mozziAnalogRead(), but also of USE_AUDIO_INPUT (if enabled).
* This template provides empty/dummy implementations to allow you to skip over this section, initially. Once you have an implementation, be sure to enable the
* #define, below: */
//#define MOZZI_FAST_ANALOG_IMPLEMENTED
// Insert here code to read the result of the latest asynchronous conversion, when it is finished.
// You can also provide this as a function returning unsigned int, should it be more complex on your platform
#define getADCReading() 0
/** NOTE: On "pins" vs. "channels" vs. "indices"
* "Pin" is the pin number as would usually be specified by the user in mozziAnalogRead().
* "Channel" is an internal ADC channel number corresponding to that pin. On many platforms this is simply the same as the pin number, on others it differs.
* In other words, this is an internal representation of "pin".
* "Index" is the index of the reading for a certain pin/channel in the array of analog_readings, ranging from 0 to NUM_ANALOG_PINS. This, again may be the
* same as "channel" (e.g. on AVR), however, on platforms where ADC-capable "channels" are not numbered sequentially starting from 0, the channel needs
* to be converted to a suitable index.
*
* In summary, the semantics are roughly
* mozziAnalogRead(pin) -> _ADCimplementation_(channel) -> analog_readings[index]
* Implement adcPinToChannelNum() and channelNumToIndex() to perform the appropriate mapping.
*/
// NOTE: Theoretically, adcPinToChannelNum is public API for historical reasons, thus cannot be replaced by a define
#define channelNumToIndex(channel) channel
uint8_t adcPinToChannelNum(uint8_t pin) {
return pin;
}
/** NOTE: Code needed to trigger a conversion on a new channel */
void adcStartConversion(uint8_t channel) {
#warning Fast analog read not implemented on this platform
}
/** NOTE: Code needed to trigger a subsequent conversion on the latest channel. If your platform has no special code for it, you should store the channel from
* adcStartConversion(), and simply call adcStartConversion(previous_channel), here. */
void startSecondADCReadOnCurrentChannel() {
#warning Fast analog read not implemented on this platform
}
/** NOTE: Code needed to set up faster than usual analog reads, e.g. specifying the number of CPU cycles that the ADC waits for the result to stabilize.
* This particular function is not super important, so may be ok to leave empty, at least, if the ADC is fast enough by default. */
void setupFastAnalogRead(int8_t speed) {
#warning Fast analog read not implemented on this platform
}
/** NOTE: Code needed to initialize the ADC for asynchronous reads. Typically involves setting up an interrupt handler for when conversion is done, and
* possibly calibration. */
void setupMozziADC(int8_t speed) {
#warning Fast analog read not implemented on this platform
}
/* NOTE: Most platforms call a specific function/ISR when conversion is complete. Provide this function, here.
* From inside its body, simply call advanceADCStep(). E.g.:
void stm32_adc_eoc_handler() {
advanceADCStep();
}
*/
////// END analog input code ////////
////// BEGIN audio output code //////
/* NOTE: Some platforms rely on control returning from loop() every so often. However, updateAudio() may take too long (it tries to completely fill the output buffer,
* which of course is being drained at the same time, theoretically it may not return at all). If you set this define, it will be called once per audio frame to keep things
* running smoothly. */
//#define LOOP_YIELD yield();
/* NOTE: On some platforms, what can be called in the ISR used to output the sound is limited.
* This define can be used, for instance, to output the sound in audioHook() instead to overcome
* this limitation (see MozziGuts_impl_MBED.hpp). It can also be used if something needs to be called in audioHook() regarding
* analog reads for instance. */
//#define AUDIO_HOOK_HOOK
#if (EXTERNAL_AUDIO_OUTPUT != true) // otherwise, the last stage - audioOutput() - will be provided by the user
/** NOTE: This is the function that actually write a sample to the output. In case of EXTERNAL_AUDIO_OUTPUT == true, it is provided by the library user, instead. */
inline void audioOutput(const AudioOutput f) {
// e.g. analogWrite(AUDIO_CHANNEL_1_PIN, f.l()+AUDIO_BIAS);
#if (AUDIO_CHANNELS > 1)
// e.g. analogWrite(AUDIO_CHANNEL_2_PIN, f.r()+AUDIO_BIAS);
#endif
}
#endif
static void startAudio() {
// Add here code to get audio output going. This usually involves:
// 1) setting up some DAC mechanism (e.g. setting up a PWM pin with appropriate resolution
// 2a) setting up a timer to call defaultAudioOutput() at AUDIO_RATE
// OR 2b) setting up a buffered output queue such as I2S (see ESP32 / ESP8266 for examples for this setup)
#if (EXTERNAL_AUDIO_OUTPUT != true)
// remember that the user may configure EXTERNAL_AUDIO_OUTPUT, in which case, you'll want to provide step 2a), and only that.
#endif
}
void stopMozzi() {
// Add here code to pause whatever mechanism moves audio samples to the output
}
////// END audio output code //////