Mozzi  version v2.0
sound synthesis library for Arduino
mozzi_analog.h
1 /*
2  * mozzi_analog.h
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2012-2024 Tim Barrass and the Mozzi Team
7  *
8  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
9  *
10  */
11 
12 
13 #ifndef MOZZI_ANALOG_H_
14 #define MOZZI_ANALOG_H_
15 
16 #include "Arduino.h"
17 
18 #include "hardware_defines.h"
19 
20 // for setupFastAnalogRead()
21 enum ANALOG_READ_SPEED {FAST_ADC,FASTER_ADC,FASTEST_ADC};
22 
50 void setupFastAnalogRead(int8_t speed=FAST_ADC);
51 
52 
53 
54 /* @ingroup analog
55 Set up for asynchronous analog input, which enables analog reads to take
56 place in the background without blocking the processor.
57 @param speed FAST_ADC, FASTER_ADC or FASTEST_ADC. See setupFastAnalogRead();
58 */
59 void setupMozziADC(int8_t speed=FAST_ADC);
60 
61 
62 
86 inline void disconnectDigitalIn(uint8_t channel_num) {
87 #if IS_AVR()
88  DIDR0 |= 1<<channel_num;
89 #else
90  (void) channel_num; // unused, suppress warning
91 #endif
92 }
93 
99 inline void reconnectDigitalIn(uint8_t channel_num) {
100 #if IS_AVR()
101  DIDR0 &= ~(1<<channel_num);
102 #else
103  (void) channel_num; // unused, suppress warning
104 #endif
105 }
106 
112 #if IS_AVR()
113  for (uint8_t i = 0; i<NUM_ANALOG_INPUTS; i++){
114  DIDR0 |= 1<<i;
115  }
116 #endif
117 }
118 
119 
125 #if IS_AVR()
126  for (uint8_t i = 0; i<NUM_ANALOG_INPUTS; i++){
127  DIDR0 &= ~(1<<i);
128  }
129 #endif
130 }
131 
132 /* @ingroup analog
133 Starts an analog to digital conversion of the voltage on a specified channel. Unlike
134 Arduino's analogRead() function which waits until a conversion is complete before
135 returning, adcStartConversion() only sets the conversion to begin, so you can use
136 the cpu for other things and call for the result later with adcGetResult().
137 @param channel is the analog channel number (0 to ....), which is not necessarily the same as the pin number
138 Use adcPinToChannelNum() to convert the pin number to its channel number.
139 @note Timing: about 1us when used in updateControl() with MOZZI_CONTROL_RATE 64.
140 */
141 void adcStartConversion(uint8_t channel);
142 
146 template<byte RES> uint16_t mozziAnalogRead(uint8_t pin);
147 
152 inline uint16_t mozziAnalogRead16(uint8_t pin) { return mozziAnalogRead<16>(pin); };
153 
154 #if defined(FOR_DOXYGEN_ONLY) || defined(MOZZI_ANALOG_READ_RESOLUTION)
174 inline uint16_t mozziAnalogRead(uint8_t pin) { return mozziAnalogRead<MOZZI_ANALOG_READ_RESOLUTION>(pin); }
175 #else
176 MOZZI_DEPRECATED("2.0", "This use of mozziAnalogRead() is not portable. Refer to the API documentation for suggested alternatives.") inline uint16_t mozziAnalogRead(uint8_t pin) { return mozziAnalogRead<MOZZI__INTERNAL_ANALOG_READ_RESOLUTION>(pin); }
177 #endif
178 
179 uint8_t adcPinToChannelNum(uint8_t pin);
180 
181 
182 #endif /* MOZZI_ANALOG_H_ */
void reconnectDigitalIn(uint8_t channel_num)
Reconnect the digital input buffer for an analog input channel which has been set for analog input wi...
Definition: mozzi_analog.h:99
void adcDisconnectAllDigitalIns()
Prepare all analog input channels by turning off their digital input buffers.
Definition: mozzi_analog.h:111
void disconnectDigitalIn(uint8_t channel_num)
Prepare an analog input channel by turning off its digital input buffer.
Definition: mozzi_analog.h:86
void adcReconnectAllDigitalIns()
Reconnect the digital input buffers for analog input channels which have been set for analog input wi...
Definition: mozzi_analog.h:124
uint16_t mozziAnalogRead(uint8_t pin)
See mozziAnalogRead().
Definition: mozzi_analog.h:174
void setupFastAnalogRead(int8_t speed=FAST_ADC)
This is automatically called in startMozzi.
uint16_t mozziAnalogRead16(uint8_t pin)
See mozziAnalogRead() but always returns the value shifted to 16 bit range.
Definition: mozzi_analog.h:152