-
Notifications
You must be signed in to change notification settings - Fork 0
/
midiflower_wifi.ino
207 lines (154 loc) · 5.04 KB
/
midiflower_wifi.ino
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
MIT License
Copyright (c) 2022 S Godin (Climate Change Lab)
Copyright (c) 2016 electricityforprogress
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
This project is based on https://github.com/electricityforprogress/MIDIsprout great
work about biodata sonification
*/
#include <Arduino.h>
#include "board.h"
#include "midinote.h"
#include "flower_music.h"
#include "sequence.h"
#include "flower_sensor.h"
#include "MidiFlowerSequencer.h"
#include "webserver_config.h"
#include "wifiap.h"
#include "config.h"
#include "activity.h"
#include "activity_dmx.h"
#define NBNOTE_FOR_BETTER_MEASURE 15
static uint32_t chipId = 0;
// get the sequencer from flower_music
extern CMidiFlowerSequencer sequencer;
void flowersensor_measure (uint32_t min, uint32_t max, uint32_t averg, uint32_t delta, float stdevi, float stdevical);
void flowersensor_measure_light (uint32_t min, uint32_t max, uint32_t averg, uint32_t delta, float stdevi, float stdevical);
// microcontroleur initialisation
void setup()
{
// initialize led output
pinMode(LED, OUTPUT);
digitalWrite(LED, LED_ON); // set led ON
for (int i = 0; i < 17; i = i + 8)
{
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
}
// start Serial if you want to debug
//Serial.begin(115200); //initialize Serial for debug
config_init ();
sequencer.Init (chipId);
// start NEOPIXEL
#if NEO_PIXEL_ENABLE
#ifdef PIN_NEOPIXEL
activity_begin ();
#endif
#endif
// start wifi Access Point
wifiap_init (chipId);
// start web server
webserver_config_init ();
// start music sequencer
flower_music_init ();
#if DMX_ENABLE
activity_dmx_begin ();
#endif
// start flower sensor
flower_sensor_init(FLOWER_SENSOR_PIN);
flower_sensor_set_analyse_short(1);
// define a function to get measures
flower_sensor_set_callback (flowersensor_measure);
flower_sensor_set_callback_light (flowersensor_measure_light);
// load config
config_load ();
// enable sequencer
sequencer.setLock(false);
}
//
static uint8_t cnt = 0;
static uint32_t last_activity = 0;
void loop()
{
digitalWrite(LED, (++cnt) & 0x01 == 0 ? LED_ON : LED_OFF); // set led ON
// good music is good rythm. You must call this very often
// a least one time by 10 millisec
sequencer.Loop ();
// build flower measure
// must be called often
flower_sensor_build_mes();
// do some control stuff
ControlMusic ();
// if we have some music notes, change configuration for better measure
if (flower_music_get_loop() ==0)
{
flower_sensor_set_analyse_short(2);
}
else if (sequencer.get_track_nbnote(0) > NBNOTE_FOR_BETTER_MEASURE )
{
flower_sensor_set_analyse_short(0);
}
#if NEOPIXEL_ENABLE
#ifdef PIN_NEOPIXEL
if (millis () - last_activity > 25)
{
activity_process ();
activity_show ();
last_activity = millis ();
}
#endif
#endif
#if DMX_ENABLE
if (millis () - last_activity > 25)
{
activity_dmx_process ();
activity_dmx_show ();
last_activity = millis ();
}
#endif
}
static uint32_t lastaverag = 0;
static uint16_t color = 0;
// Flower sensor measure callback. receive flower measures
// min min value
// max max value
// averg average
// delta max - min
// stdevi standard deviation
// stdevical standard deviation * threshold
void flowersensor_measure (uint32_t min, uint32_t max, uint32_t averg, uint32_t delta, float stdevi, float stdevical)
{
// give all the measure to flower music generation code
BuildNoteFromMeasure (millis(), min, max, averg, delta, stdevi, stdevical);
}
// Flower sensor measure callback. receive flower measures
// min min value
// max max value
// averg average
// delta max - min
// stdevi standard deviation
// stdevical standard deviation * threshold
void flowersensor_measure_light (uint32_t min, uint32_t max, uint32_t averg, uint32_t delta, float stdevi, float stdevical)
{
#if NEOPIXEL_ENABLE
#ifdef PIN_NEOPIXEL
activity_event (delta%64);
#endif
#endif
#if DMX_ENABLE
activity_dmx_event (delta%255);
#endif
}