GyroLights  0.1
Oakleaf
effects.h
Go to the documentation of this file.
1 #pragma once
2 #if defined(ARDUINO)
3 #include <Arduino.h>
4 #endif // ARDUINO
5 #include <FastLED.h>
6 #include <esp_log.h>
7 #include <lwip/def.h>
8 #include <driver/i2c.h>
9 
10 #include <projectConfig.h>
11 
12 extern TaskHandle_t task_local;
13 
14 
15 
21 void readGyro(int16_t *rxvalues) {
22  const uint8_t startregister = 0x3B;
23  uint8_t rx_data[14];
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));
25  //ESP_LOG_BUFFER_HEX_LEVEL("i2c_gyro_rx", rx_data, sizeof(rx_data), ESP_LOG_VERBOSE);
26  for (int i = 0; i < 7; i++,rxvalues++) {
27  *rxvalues = htons(*((int16_t*) rx_data+i));
28  }
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]));
30  //rxvalues = reinterpret_cast<int16_t*>(rx_data);
31 }
32 
37  int16_t rxvalues[7];
38  readGyro(rxvalues);
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));
41 }
42 
55 float mapfb(float in, float in_min, float in_max, float out_min, float out_max) {
56  if (in <= in_min)
57  return out_min;
58  if (in_min >= in_max)
59  return out_max;
60  return (in - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
61 }
62 
63 
64 
72 template<CRGB* targetArray, uint numToFill, uint PERIOD_LENGTH>
73 void task_rainbow(void*) {
74  ESP_LOG_LEVEL(ESP_LOG_DEBUG, __func__, "new mode");
75  uint8_t hue = 0;
76  const int mydelay = PERIOD_LENGTH * 4; // approx. PERIOD_LENGTH * 1000 / 256. (nach 256 schleifen soll wieder die start-hue sein. das soll Period length dauern)
77  while(true) {
78  fill_rainbow(targetArray, numToFill, hue);
79  hue++;
80  FastLED.show(conf::MAX_BRIGHTNESS);
81  vTaskDelay(pdMS_TO_TICKS(mydelay));
82  }
83 }
84 
92 template<CRGB* targetArray, uint numToFill, uint32_t colorcode>
93 void task_staticColor(void*) {
94  ESP_LOG_LEVEL(ESP_LOG_DEBUG, __func__, "new mode, color: %06X", colorcode);
95  fill_solid(targetArray, numToFill, CRGB(colorcode));
96  FastLED.show(conf::MAX_BRIGHTNESS);
97  task_local = nullptr;
98  vTaskDelete(NULL);
99 }
100 
107 template<CRGB* targetArray, uint numToFill>
108 void task_gyroSimple(void*) {
109  int16_t rxvalues[7];
110  static uint8_t r, g, b;
111  const float alpha = 1.0/16.0;
112  while (true) {
113  readGyro(rxvalues);
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));
118  FastLED.show(conf::MAX_BRIGHTNESS);
119  vTaskDelay(34);
120  }
121 }
122 
123 
131 template<CRGB* targetArray, uint numToFill, TProgmemRGBGradientPalette_bytes heatmap>
132 void task_gyroToHeatmap(void*) {
133  int16_t rxvalues[7];
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;
138  while (true) {
139  readGyro(rxvalues);
140 
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));
147  } else {
148  norm_accel = norm_reading;
149  }
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)));
153  FastLED.show(conf::MAX_BRIGHTNESS);
154  vTaskDelay(34);
155  }
156 }
157 
158 
159 
160 template<CRGB* targetArray, uint numToFill>
162  /* ---------------------------------- jerk ---------------------------------- */
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;
166  float jerk = 0;
167  static float jerk_prev = 0, jerk_max = 0, jerk_min = 0;
168  while (true) {
169  time_now = millis();
170  norm_reading = directedAccelNorm();
171  jerk = (norm_reading - norm_prev)/(time_now-time_prev);
172  time_prev = time_now;
173  norm_prev = norm_reading;
174  // bounds
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);
181  FastLED.show(mapfb(jerk, jerk_min, jerk_max, 0, conf::MAX_BRIGHTNESS));
182  //FastLED.show(255 * (jerk > 100.0));
183  vTaskDelay(34);
184  }
185 }
186 
187 template<CRGB* targetArray, uint numToFill>
189  float directednormaccel = 0;
190  while (true) {
191  directednormaccel = directedAccelNorm();
192  if (directednormaccel < -10000) {
193  fill_solid(targetArray, numToFill, CRGB::Green);
194  } else {
195  fill_solid(targetArray, numToFill, CRGB::Black);
196  }
197  ESP_LOGV(__func__, "acceleration norm: %f\r", directednormaccel);
198  FastLED.show(conf::MAX_BRIGHTNESS);
199  vTaskDelay(34);
200  }
201 }
202 
203 template<CRGB* targetArray, uint numToFill>
205  int16_t rxvalues[7];
206  float directednormaccel = 0;
207  uint8_t brightness = 0;
208  while (true) {
209  readGyro(rxvalues);
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]));
211  directednormaccel = directedAccelNorm();
212  if (directednormaccel < -10000) {
213  brightness = conf::MAX_BRIGHTNESS;
214  fill_solid(targetArray, numToFill, CRGB::Cyan);
215  } else {
216  brightness = brightness * 0.95;
217  }
218  ESP_LOGV(__func__, "acceleration norm: %f\r", directednormaccel);
219  FastLED.show(conf::MAX_BRIGHTNESS);
220  vTaskDelay(34);
221  }
222 }
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