8 #include <driver/i2c.h>
22 const uint8_t startregister = 0x3B;
24 ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_read_device(I2C_NUM_0,
conf::I2C_GYRO_ADDR, &startregister,
sizeof(startregister), rx_data,
sizeof(rx_data),
conf::I2C_TIMEOUT_MS/portTICK_RATE_MS));
26 for (
int i = 0; i < 7; i++,rxvalues++) {
27 *rxvalues = htons(*((int16_t*) rx_data+i));
29 ESP_LOG_LEVEL(ESP_LOG_VERBOSE,
"readGyro",
"accelx: %7d, accely: %7d, accelz: %7d, temp: %7d, gyrox: %7d, gyroy: %7d, gyroz: %7d\r", (rxvalues[0]), (rxvalues[1]), (rxvalues[2]), (rxvalues[3]), (rxvalues[4]), (rxvalues[5]), (rxvalues[6]));
39 const int sign = rxvalues[0] < 0 ? (-1) : 1;
40 return sign * sqrtf(pow(rxvalues[0], 2) + pow(rxvalues[1], 2) + pow(rxvalues[2], 2));
55 float mapfb(
float in,
float in_min,
float in_max,
float out_min,
float out_max) {
60 return (in - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
72 template<CRGB* targetArray, u
int numToFill, u
int PERIOD_LENGTH>
74 ESP_LOG_LEVEL(ESP_LOG_DEBUG, __func__,
"new mode");
76 const int mydelay = PERIOD_LENGTH * 4;
78 fill_rainbow(targetArray, numToFill, hue);
81 vTaskDelay(pdMS_TO_TICKS(mydelay));
92 template<CRGB* targetArray, u
int numToFill, u
int32_t colorcode>
94 ESP_LOG_LEVEL(ESP_LOG_DEBUG, __func__,
"new mode, color: %06X", colorcode);
95 fill_solid(targetArray, numToFill, CRGB(colorcode));
107 template<CRGB* targetArray, u
int numToFill>
110 static uint8_t r, g, b;
111 const float alpha = 1.0/16.0;
114 r = ((1-alpha) * r ) + (alpha * (abs(rxvalues[0]) >> 7));
115 g = ((1-alpha) * g ) + (alpha * (abs(rxvalues[1]) >> 7));
116 b = ((1-alpha) * b ) + (alpha * (abs(rxvalues[2]) >> 7));
117 fill_solid(targetArray, numToFill, CRGB(r, g, b));
131 template<CRGB* targetArray, u
int numToFill, TProgmemRGBGradientPalette_
bytes heatmap>
134 const float alpha = 1.0/16.0;
135 float norm_accel_max = 0, norm_reading = 0, norm_prev = 0;
136 static float norm_accel = 0;
137 CRGBPalette16 palette = heatmap;
141 norm_reading = sqrtf(pow(rxvalues[0], 2) + pow(rxvalues[1], 2) + pow(rxvalues[2], 2)) -
conf::G_VAL;
142 if (norm_accel_max < norm_accel)
143 norm_accel_max = norm_accel;
144 ESP_LOGV(__func__,
"norm_accel_max: %f", norm_accel_max);
145 if (norm_reading < norm_accel + 200) {
146 norm_accel = (norm_reading * alpha) + (norm_accel * (1.0 - alpha));
148 norm_accel = norm_reading;
150 ESP_LOGV(__func__,
"norm_accel: %f", norm_accel);
151 ESP_LOGV(__func__,
"color: %06X", ColorFromPalette(palette, 0xff * (norm_accel/norm_accel_max)));
152 fill_solid(targetArray, numToFill, ColorFromPalette(palette, 0xff * (norm_accel/norm_accel_max)));
160 template<CRGB* targetArray, u
int numToFill>
163 unsigned long time_now, time_prev;
164 time_now = time_prev = millis();
165 float norm_accel_max = 0, norm_reading = 0, norm_prev = 0;
167 static float jerk_prev = 0, jerk_max = 0, jerk_min = 0;
171 jerk = (norm_reading - norm_prev)/(time_now-time_prev);
172 time_prev = time_now;
173 norm_prev = norm_reading;
175 jerk_max = jerk_max * 0.99;
176 jerk_min = jerk_min * 0.99;
177 if (jerk > jerk_max) jerk_max = jerk;
178 else if (jerk < jerk_min) jerk_min = jerk;
179 ESP_LOGV(__func__,
"jerk: %f", jerk);
180 fill_solid(targetArray, numToFill, CRGB::White);
187 template<CRGB* targetArray, u
int numToFill>
189 float directednormaccel = 0;
192 if (directednormaccel < -10000) {
193 fill_solid(targetArray, numToFill, CRGB::Green);
195 fill_solid(targetArray, numToFill, CRGB::Black);
197 ESP_LOGV(__func__,
"acceleration norm: %f\r", directednormaccel);
203 template<CRGB* targetArray, u
int numToFill>
206 float directednormaccel = 0;
207 uint8_t brightness = 0;
210 ESP_LOGV(__func__,
"accelx: %7d, accely: %7d, accelz: %7d, temp: %7d, gyrox: %7d, gyroy: %7d, gyroz: %7d\r", (rxvalues[0]), (rxvalues[1]), (rxvalues[2]), (rxvalues[3]), (rxvalues[4]), (rxvalues[5]), (rxvalues[6]));
212 if (directednormaccel < -10000) {
214 fill_solid(targetArray, numToFill, CRGB::Cyan);
216 brightness = brightness * 0.95;
218 ESP_LOGV(__func__,
"acceleration norm: %f\r", directednormaccel);
void task_rainbow(void *)
task to apply FastLEDs fill_rainbow to targetArray with changing start hue.
Definition: effects.h:73
void task_gyroToHeatmap(void *)
task maps acceleration to heatmap.
Definition: effects.h:132
void task_effect_threashold_exp_decrease(void *)
Definition: effects.h:204
void task_effect_jerk_test(void *)
Definition: effects.h:161
void task_gyroSimple(void *)
task maps x,y,z-readings form gyro to r,g,b of targetArray
Definition: effects.h:108
void readGyro(int16_t *rxvalues)
fill array with values from gyro sensor
Definition: effects.h:21
float directedAccelNorm()
Definition: effects.h:36
void task_effect_threashold(void *)
Definition: effects.h:188
void task_staticColor(void *)
task to fill targetArray with a color, then delete task
Definition: effects.h:93
float mapfb(float in, float in_min, float in_max, float out_min, float out_max)
scales a float variable in [in_min, in_max] linearly to [out_min, out_max]
Definition: effects.h:55
TaskHandle_t task_local
Definition: main.cpp:24
const uint8_t MAX_BRIGHTNESS
maximum led brightness value. from 0 (always off) to 255 (maximum possible brightness).
Definition: projectConfig.h:38
const int G_VAL
Definition: projectConfig.h:98
const int I2C_GYRO_ADDR
Definition: projectConfig.h:94
const int I2C_TIMEOUT_MS
Definition: projectConfig.h:40
project specific configuration parameters