forked from corrados/edrumulus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedrumulus_hardware.h
179 lines (144 loc) · 7.11 KB
/
edrumulus_hardware.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
/******************************************************************************\
* Copyright (c) 2020-2022
* Author(s): Volker Fischer
******************************************************************************
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\******************************************************************************/
#pragma once
#include "Arduino.h"
#include "EEPROM.h"
// Global hardware enums and definitions ---------------------------------------
enum Espikestate
{
ST_NOISE,
ST_SPIKE_HIGH,
ST_SPIKE_LOW,
ST_OTHER
};
#define MAX_NUM_PADS 12 // a maximum of 12 pads are supported
#define MAX_NUM_PAD_INPUTS 2 // a maximum of 2 sensors per pad is supported
#define MAX_EEPROM_SIZE 512 // bytes (Teensy 4.0: max 1024 bytes)
#define MAX_NUM_SET_PER_PAD 30 // maximum number of settings which can be stored per pad
// -----------------------------------------------------------------------------
// Teensy 4.0/4.1/3.6 ----------------------------------------------------------
// -----------------------------------------------------------------------------
#ifdef TEENSYDUINO
#include <ADC.h>
#define BOARD_LED_PIN 13 // pin number of the LED on the Teensy 4.0 board
#ifdef ARDUINO_TEENSY36
# define ADC_MAX_RANGE 65536 // Teensy 3.6 ADC has 16 bits -> 0..65535
# define ADC_MAX_NOISE_AMPL 20 // highest assumed ADC noise amplitude in the ADC input range unit (measured)
#else
# define ADC_MAX_RANGE 4096 // Teensy 4.0/4.1 ADC has 12 bits -> 0..4095
# define ADC_MAX_NOISE_AMPL 8 // highest assumed ADC noise amplitude in the ADC input range unit (measured)
#endif
class Edrumulus_hardware
{
public:
Edrumulus_hardware();
void setup ( const int conf_Fs,
const int number_pads,
const int number_inputs[],
int analog_pin[][MAX_NUM_PAD_INPUTS] );
void capture_samples ( const int number_pads,
const int number_inputs[],
int analog_pin[][MAX_NUM_PAD_INPUTS],
int sample_org[][MAX_NUM_PAD_INPUTS] );
float cancel_ADC_spikes ( const float input,
const int pad_index,
const int input_channel_index,
const int level );
void write_setting ( const int pad_index, const int address, const byte value );
byte read_setting ( const int pad_index, const int address );
protected:
int Fs;
IntervalTimer myTimer;
static void on_timer();
volatile bool timer_ready;
ADC adc_obj;
int total_number_inputs;
int input_pin[MAX_NUM_PADS * MAX_NUM_PAD_INPUTS];
uint16_t input_sample[MAX_NUM_PADS * MAX_NUM_PAD_INPUTS];
Espikestate prev1_input_state[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
Espikestate prev2_input_state[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
Espikestate prev3_input_state[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
Espikestate prev4_input_state[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
Espikestate prev5_input_state[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
float prev_input1[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
float prev_input2[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
float prev_input3[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
float prev_input4[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
};
#endif
// -----------------------------------------------------------------------------
// ESP32 Dual Core -------------------------------------------------------------
// -----------------------------------------------------------------------------
#ifdef ESP_PLATFORM
#include "soc/sens_reg.h"
#include "driver/dac.h"
#define BOARD_LED_PIN 2 // pin number of the LED on the ESP32 board
#define ADC_MAX_RANGE 4096 // ESP32 ADC has 12 bits -> 0..4095
#define ADC_MAX_NOISE_AMPL 8 // highest assumed ADC noise amplitude in the ADC input range unit (measured)
class Edrumulus_hardware
{
public:
Edrumulus_hardware();
void setup ( const int conf_Fs,
const int number_pads,
const int number_inputs[],
int analog_pin[][MAX_NUM_PAD_INPUTS] );
void capture_samples ( const int number_pads,
const int number_inputs[],
int analog_pin[][MAX_NUM_PAD_INPUTS],
int sample_org[][MAX_NUM_PAD_INPUTS] );
float cancel_ADC_spikes ( const float input,
const int pad_index,
const int input_channel_index,
const int level );
void write_setting ( const int pad_index, const int address, const byte value );
byte read_setting ( const int pad_index, const int address );
protected:
int Fs;
EEPROMClass eeprom_settings;
volatile SemaphoreHandle_t timer_semaphore;
hw_timer_t* timer = nullptr;
static void IRAM_ATTR on_timer();
static void start_timer_core0_task ( void* param );
void setup_timer ( const bool clear_timer = false );
void init_my_analogRead();
uint16_t my_analogRead ( const uint8_t pin );
void my_analogRead_parallel ( const uint32_t channel_adc1_bitval,
const uint32_t channel_adc2_bitval,
uint16_t& out_adc1,
uint16_t& out_adc2 );
int total_number_inputs;
int input_pin[MAX_NUM_PADS * MAX_NUM_PAD_INPUTS];
uint16_t input_sample[MAX_NUM_PADS * MAX_NUM_PAD_INPUTS];
int num_pin_pairs;
int adc1_index[MAX_NUM_PADS * MAX_NUM_PAD_INPUTS];
int adc2_index[MAX_NUM_PADS * MAX_NUM_PAD_INPUTS];
uint32_t channel_adc1_bitval[MAX_NUM_PADS * MAX_NUM_PAD_INPUTS];
uint32_t channel_adc2_bitval[MAX_NUM_PADS * MAX_NUM_PAD_INPUTS];
int num_pin_single;
int single_index[MAX_NUM_PADS * MAX_NUM_PAD_INPUTS];
Espikestate prev1_input_state[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
Espikestate prev2_input_state[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
Espikestate prev3_input_state[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
Espikestate prev4_input_state[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
Espikestate prev5_input_state[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
float prev_input1[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
float prev_input2[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
float prev_input3[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
float prev_input4[MAX_NUM_PADS][MAX_NUM_PAD_INPUTS];
};
#endif