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