Mozzi  version 2016-12-11-17:03
sound synthesis library for Arduino
mozzi_analog.h
1 /*
2  * mozzi_analog.h
3  *
4  * Copyright 2012 Tim Barrass.
5  *
6  * This file is part of Mozzi.
7  *
8  * Mozzi is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
9  *
10  */
11 
12 #ifndef MOZZI_ANALOG_H_
13 #define MOZZI_ANALOG_H_
14 
15  #if ARDUINO >= 100
16  #include "Arduino.h"
17 #else
18  #include "WProgram.h"
19 #endif
20 
21 // required from http://github.com/pedvide/ADC for Teensy 3.1
22 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO)
23 #include <ADC.h>
24 #endif
25 
26 
27 // these are declared in Mozziguts.cpp, and used in mozzi_analog.cpp... crazy but it compiles
28 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO) // teensy 3, 3.1
29  extern ADC *adc; // adc object
30  extern uint8_t teensy_pin;
31 #endif
32 
33 #if (USE_AUDIO_INPUT==true)
34 #warning "Using AUDIO_INPUT_PIN defined in mozzi_config.h for audio input."
35 #endif
36 
37 
38 
39 void adcReadSelectedChannels();
40 //void receiveFirstControlADC();
41 void startSecondControlADC();
42 void receiveSecondControlADC();
43 
44 // hack for Teensy 2 (ATmega32U4), which has "adc_mapping" instead of "analog_pin_to_channel_PGM"
45 #if defined(__AVR_ATmega32U4__) && defined(CORE_TEENSY)
46 //pasted from hardware/arduino/variants/leonardo/pins_arduino.h, doesn't work as of mozzi 0.01.2a
47 // __AVR_ATmega32U4__ has an unusual mapping of pins to channels
48 //extern const uint8_t PROGMEM analog_pin_to_channel_PGM[];
49 //#define analogPinToChannel(P) ( pgm_read_byte( analog_pin_to_channel_PGM + (P) ) )
50 
51 // look at Arduino.app/Contents/Resources/Java/hardware/teensy/cores/teensy/pins_teensy.c - analogRead
52 // adc_mapping is already declared in pins_teensy.c, but it's static there so we can't access it
53 static const uint8_t PROGMEM adc_mapping[] = {
54 // 0, 1, 4, 5, 6, 7, 13, 12, 11, 10, 9, 8
55  0, 1, 4, 5, 6, 7, 13, 12, 11, 10, 9, 8, 10, 11, 12, 13, 7, 6, 5, 4, 1, 0, 8
56 };
57 #define analogPinToChannel(P) ( pgm_read_byte( adc_mapping + (P) ) )
58 #endif
59 
60 
61 // include this although already in teensy 3 analog.c, because it is static there
62 #if defined(__MK20DX128__)
63 static const uint8_t channel2sc1a[] = {
64  5, 14, 8, 9, 13, 12, 6, 7, 15, 4,
65  0, 19, 3, 21, 26, 22, 23
66 };
67 #elif defined(__MK20DX256__)
68 static const uint8_t channel2sc1a[] = {
69  5, 14, 8, 9, 13, 12, 6, 7, 15, 4,
70  0, 19, 3, 19+128, 26, 18+128, 23,
71  5+192, 5+128, 4+128, 6+128, 7+128, 4+192
72 // A15 26 E1 ADC1_SE5a 5+64
73 // A16 27 C9 ADC1_SE5b 5
74 // A17 28 C8 ADC1_SE4b 4
75 // A18 29 C10 ADC1_SE6b 6
76 // A19 30 C11 ADC1_SE7b 7
77 // A20 31 E0 ADC1_SE4a 4+64
78 };
79 #endif
80 
81 
82 // for setupFastAnalogRead()
83 enum ANALOG_READ_SPEED {FAST_ADC,FASTER_ADC,FASTEST_ADC};
84 
105 void setupFastAnalogRead(int8_t speed=FAST_ADC);
106 
107 
108 
109 /* @ingroup analog
110 Set up for asynchronous analog input, which enables analog reads to take
111 place in the background without blocking the processor.
112 @param speed FAST_ADC, FASTER_ADC or FASTEST_ADC. See setupFastAnalogRead();
113 */
114 void setupMozziADC(int8_t speed=FAST_ADC);
115 
116 
117 
141 void disconnectDigitalIn(uint8_t channel_num);
142 
143 
149 void reconnectDigitalIn(uint8_t channel_num);
150 
151 
157 
158 
164 
165 
166 
167 /* @ingroup analog
168 Starts an analog to digital conversion of the voltage on a specified channel. Unlike
169 Arduino's analogRead() function which waits until a conversion is complete before
170 returning, adcStartConversion() only sets the conversion to begin, so you can use
171 the cpu for other things and call for the result later with adcGetResult().
172 @param channel is the analog channel number (0 to ....), which is not necessarily the same as the pin number
173 Use adcPinToChannelNum() to convert the pin number to its channel number.
174 @note Timing: about 1us when used in updateControl() with CONTROL_RATE 64.
175 */
176 void adcStartConversion(uint8_t channel);
177 
178 
179 
188 int mozziAnalogRead(uint8_t pin);
189 
190 
191 /* Used in MozziGuts.cpp, in updateControlWithAutoADC() to kick off any mozziAnalogReads waiting on the stack
192 */
193 void adcStartReadCycle();
194 
195 
196 uint8_t adcPinToChannelNum(uint8_t pin);
197 
198 #endif /* MOZZI_ANALOG_H_ */
int mozziAnalogRead(uint8_t pin)
Reads the analog input of a chosen channel, without blocking other operations from running...
void adcReconnectAllDigitalIns()
Reconnect the digital input buffers for analog input channels which have been set for analog input wi...
void reconnectDigitalIn(uint8_t channel_num)
Reconnect the digital input buffer for an analog input channel which has been set for analog input wi...
void disconnectDigitalIn(uint8_t channel_num)
Prepare an analog input channel by turning off its digital input buffer.
void setupFastAnalogRead(int8_t speed=FAST_ADC)
This is automatically called in startMozzi.
void adcDisconnectAllDigitalIns()
Prepare all analog input channels by turning off their digital input buffers.