ws2812_effects.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. #include "module.h"
  2. #include "lauxlib.h"
  3. #include "lmem.h"
  4. #include "platform.h"
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include <string.h>
  8. #include "user_interface.h"
  9. #include "driver/uart.h"
  10. #include "osapi.h"
  11. #include "pm/swtimer.h"
  12. #include "ws2812.h"
  13. #include "color_utils.h"
  14. #define CANARY_VALUE 0x32372132
  15. #define DEFAULT_MODE 0
  16. #define DEFAULT_COLOR 0xFF0000
  17. #define SPEED_MIN 0
  18. #define SPEED_MAX 255
  19. #define SPEED_DEFAULT 150
  20. #define DELAY_DEFAULT 100
  21. #define BRIGHTNESS_MIN 0
  22. #define BRIGHTNESS_MAX 255
  23. #define BRIGHTNESS_DEFAULT 100
  24. #define EFFECT_PARAM_INVALID -10000
  25. #define LIBRARY_NOT_INITIALIZED_ERROR_MSG "please call init() first"
  26. #define min(a,b) ((a) < (b) ? (a) : (b))
  27. #define max(a,b) ((a) > (b) ? (a) : (b))
  28. #define abs(a) ((a) > 0 ? (a) : (0-a))
  29. #define min3(a,b, c) min((a), min((b), (c)))
  30. #define max3(a,b, c) max((a), max((b), (c)))
  31. #define IDX_R 1
  32. #define IDX_G 0
  33. #define IDX_B 2
  34. #define IDX_W 3
  35. typedef struct {
  36. ws2812_buffer *buffer;
  37. int buffer_ref;
  38. uint32_t mode_delay;
  39. uint32_t counter_mode_call;
  40. uint32_t counter_mode_step;
  41. uint8_t mode_color_index;
  42. uint8_t speed;
  43. uint8_t brightness;
  44. os_timer_t os_t;
  45. uint8_t running;
  46. uint8_t effect_type;
  47. uint8_t color[4];
  48. int effect_int_param1;
  49. ws2812_buffer_shift_prepare* prepare;
  50. } ws2812_effects;
  51. enum ws2812_effects_type {
  52. WS2812_EFFECT_STATIC,
  53. WS2812_EFFECT_BLINK,
  54. WS2812_EFFECT_GRADIENT,
  55. WS2812_EFFECT_GRADIENT_RGB,
  56. WS2812_EFFECT_RANDOM_COLOR,
  57. WS2812_EFFECT_RAINBOW,
  58. WS2812_EFFECT_RAINBOW_CYCLE,
  59. WS2812_EFFECT_FLICKER,
  60. WS2812_EFFECT_FIRE_FLICKER,
  61. WS2812_EFFECT_FIRE_FLICKER_SOFT,
  62. WS2812_EFFECT_FIRE_FLICKER_INTENSE,
  63. WS2812_EFFECT_HALLOWEEN,
  64. WS2812_EFFECT_CIRCUS_COMBUSTUS,
  65. WS2812_EFFECT_LARSON_SCANNER,
  66. WS2812_EFFECT_CYCLE,
  67. WS2812_EFFECT_COLOR_WIPE,
  68. WS2812_EFFECT_RANDOM_DOT
  69. };
  70. static ws2812_effects *state;
  71. //-----------------
  72. // UTILITY METHODS
  73. //-----------------
  74. static int ws2812_write(ws2812_buffer* buffer) {
  75. size_t length1, length2;
  76. const char *buffer1, *buffer2;
  77. buffer1 = buffer->values;
  78. length1 = buffer->colorsPerLed*buffer->size;
  79. buffer2 = 0;
  80. length2 = 0;
  81. // Send the buffers
  82. ws2812_write_data(buffer1, length1, buffer2, length2);
  83. return 0;
  84. }
  85. static int ws2812_set_pixel(int pixel, uint32_t color) {
  86. ws2812_buffer * buffer = state->buffer;
  87. uint8_t g = ((color & 0x00FF0000) >> 16);
  88. uint8_t r = ((color & 0x0000FF00) >> 8);
  89. uint8_t b = (color & 0x000000FF);
  90. uint8_t w = buffer->colorsPerLed == 4 ? ((color & 0xFF000000) >> 24) : 0;
  91. int offset = pixel * buffer->colorsPerLed;
  92. buffer->values[offset+IDX_R] = r;
  93. buffer->values[offset+IDX_G] = g;
  94. buffer->values[offset+IDX_B] = b;
  95. if (buffer->colorsPerLed == 4) {
  96. buffer->values[offset+IDX_W] = w;
  97. }
  98. return 0;
  99. }
  100. /*
  101. * Returns a new, random color wheel index with a minimum distance of 42 from pos.
  102. */
  103. static uint8_t get_random_wheel_index(uint8_t pos)
  104. {
  105. uint8_t r = 0;
  106. uint8_t x = 0;
  107. uint8_t y = 0;
  108. uint8_t d = 0;
  109. while(d < 42) {
  110. r = rand() % 360;
  111. x = abs(pos - r);
  112. y = 360 - x;
  113. d = min(x, y);
  114. }
  115. return r;
  116. }
  117. //-----------------
  118. // EFFECTS LIBRARY
  119. //-----------------
  120. /**
  121. * initialized ws2812_effects with the buffer to use
  122. */
  123. static int ws2812_effects_init(lua_State *L) {
  124. platform_print_deprecation_note("ws2812_effects",
  125. "soon; please see https://github.com/nodemcu/nodemcu-firmware/issues/3122");
  126. ws2812_buffer * buffer = (ws2812_buffer*)luaL_checkudata(L, 1, "ws2812.buffer");
  127. luaL_argcheck(L, buffer != NULL, 1, "no valid buffer provided");
  128. // get rid of old state
  129. if (state != NULL) {
  130. if (state->prepare) {
  131. luaM_free(L, state->prepare);
  132. }
  133. luaL_unref(L, LUA_REGISTRYINDEX, state->buffer_ref);
  134. free((void *) state);
  135. }
  136. // Allocate memory and set all to zero
  137. state = (ws2812_effects *) calloc(1,sizeof(ws2812_effects));
  138. // initialize
  139. state->speed = SPEED_DEFAULT;
  140. state->mode_delay = DELAY_DEFAULT;
  141. state->brightness = BRIGHTNESS_DEFAULT;
  142. state->buffer = buffer;
  143. state->buffer_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  144. return 0;
  145. }
  146. /*
  147. * set color for single color effects
  148. */
  149. static int ws2812_effects_set_color(lua_State* L) {
  150. luaL_argcheck(L, state != NULL, 1, LIBRARY_NOT_INITIALIZED_ERROR_MSG);
  151. uint8_t g = luaL_checkinteger(L, 1);
  152. uint8_t r = luaL_checkinteger(L, 2);
  153. uint8_t b = luaL_checkinteger(L, 3);
  154. uint8_t w = luaL_optinteger(L, 4, 0 );
  155. state->color[0] = g;
  156. state->color[1] = r;
  157. state->color[2] = b;
  158. state->color[3] = w;
  159. return 0;
  160. }
  161. static int ws2812_effects_get_speed(lua_State* L) {
  162. luaL_argcheck(L, state != NULL, 1, LIBRARY_NOT_INITIALIZED_ERROR_MSG);
  163. lua_pushinteger(L, state->speed);
  164. return 1;
  165. }
  166. static int ws2812_effects_set_speed(lua_State* L) {
  167. int speed = luaL_checkinteger(L, 1);
  168. luaL_argcheck(L, state != NULL, 1, LIBRARY_NOT_INITIALIZED_ERROR_MSG);
  169. luaL_argcheck(L, speed >= SPEED_MIN && speed <= SPEED_MAX, 1, "should be 0-255");
  170. state->speed = (uint8_t)speed;
  171. state->mode_delay = 10;
  172. return 0;
  173. }
  174. static int ws2812_effects_get_delay(lua_State* L) {
  175. luaL_argcheck(L, state != NULL, 1, LIBRARY_NOT_INITIALIZED_ERROR_MSG);
  176. lua_pushinteger(L, state->mode_delay);
  177. return 1;
  178. }
  179. static int ws2812_effects_set_delay(lua_State* L) {
  180. luaL_argcheck(L, state != NULL, 1, LIBRARY_NOT_INITIALIZED_ERROR_MSG);
  181. const int delay = luaL_checkinteger(L, 1);
  182. luaL_argcheck(L, delay >= 10, 1, "must be equal / larger than 10");
  183. state->mode_delay = delay;
  184. state->speed = 0;
  185. return 1;
  186. }
  187. static int ws2812_effects_set_brightness(lua_State* L) {
  188. int brightness = luaL_checkint(L, 1);
  189. luaL_argcheck(L, state != NULL, 1, LIBRARY_NOT_INITIALIZED_ERROR_MSG);
  190. luaL_argcheck(L, brightness >= BRIGHTNESS_MIN && brightness <= BRIGHTNESS_MAX, 1, "should be 0-255");
  191. state->brightness = (uint8_t) brightness;
  192. return 0;
  193. }
  194. static void ws2812_effects_fill_buffer(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
  195. ws2812_buffer * buffer = state->buffer;
  196. uint8_t bright_g = g * state->brightness / BRIGHTNESS_MAX;
  197. uint8_t bright_r = r * state->brightness / BRIGHTNESS_MAX;
  198. uint8_t bright_b = b * state->brightness / BRIGHTNESS_MAX;
  199. uint8_t bright_w = w * state->brightness / BRIGHTNESS_MAX;
  200. // Fill buffer
  201. int i;
  202. uint8_t * p = &buffer->values[0];
  203. for(i = 0; i < buffer->size; i++) {
  204. *p++ = bright_g;
  205. *p++ = bright_r;
  206. *p++ = bright_b;
  207. if (buffer->colorsPerLed == 4) {
  208. *p++ = bright_w;
  209. }
  210. }
  211. }
  212. //------------------
  213. // basic methods
  214. //------------------
  215. /*
  216. * Cycles all LEDs at once through a rainbow.
  217. */
  218. static int ws2812_effects_fill_color() {
  219. uint8_t g = state->color[0];
  220. uint8_t r = state->color[1];
  221. uint8_t b = state->color[2];
  222. uint8_t w = state->color[3];
  223. ws2812_effects_fill_buffer(r, g, b, w);
  224. return 0;
  225. }
  226. //-----------------
  227. // EFFECFTS
  228. //-----------------
  229. /*
  230. * blink with set color
  231. */
  232. static int ws2812_effects_mode_blink() {
  233. if(state->counter_mode_call % 2 == 1) {
  234. // on
  235. ws2812_effects_fill_color();
  236. }
  237. else {
  238. // off
  239. ws2812_buffer * buffer = state->buffer;
  240. memset(&buffer->values[0], 0, buffer->size * buffer->colorsPerLed);
  241. }
  242. return 0;
  243. }
  244. static int ws2812_effects_gradient(const char *gradient_spec, size_t length1) {
  245. ws2812_buffer * buffer = state->buffer;
  246. int segments = (length1 / buffer->colorsPerLed) - 1;
  247. int segmentSize = buffer->size / segments;
  248. uint8_t g1, r1, b1, g2, r2, b2;
  249. int i,j,k;
  250. g2 = *gradient_spec++;
  251. r2 = *gradient_spec++;
  252. b2 = *gradient_spec++;
  253. // skip non-rgb components
  254. for (j = 3; j < buffer->colorsPerLed; j++)
  255. {
  256. *gradient_spec++;
  257. }
  258. // reference to buffer memory
  259. uint8_t * p = &buffer->values[0];
  260. uint16_t h1,h2;
  261. uint8_t s,v,s1,v1,s2,v2;
  262. for (k = 0; k < segments; k++) {
  263. g1 = g2;
  264. r1 = r2;
  265. b1 = b2;
  266. uint32_t hsv1 = grb2hsv(g1, r1, b1);
  267. h1 = (hsv1 & 0xFFFF0000) >> 16;
  268. s1 = (hsv1 & 0x0000FF00) >> 8;
  269. v1 = (hsv1 & 0x000000FF);
  270. g2 = *gradient_spec++;
  271. r2 = *gradient_spec++;
  272. b2 = *gradient_spec++;
  273. for (j = 3; j < buffer->colorsPerLed; j++)
  274. {
  275. *gradient_spec++;
  276. }
  277. uint32_t hsv2 = grb2hsv(g2, r2, b2);
  278. h2 = (hsv2 & 0xFFFF0000) >> 16;
  279. s2 = (hsv1 & 0x0000FF00) >> 8;
  280. v2 = (hsv1 & 0x000000FF);
  281. // get distance and direction to use
  282. int maxCCW = h1 > h2 ? h1 - h2 : 360 + h1 - h2;
  283. int maxCW = h1 > h2 ? 360 + h2 - h1 : h2 - h1;
  284. // Fill buffer
  285. int numPixels = segmentSize;
  286. // make sure we fill the strip correctly in case of rounding errors
  287. if (k == segments - 1) {
  288. numPixels = buffer->size - (segmentSize * (segments - 1));
  289. }
  290. int steps = numPixels - 1;
  291. for(i = 0; i < numPixels; i++) {
  292. // calculate HSV values
  293. //h = h1 + ((h2-h1) * i / fillSize);
  294. int h = maxCCW > maxCW ? h1 + ((maxCW * i / steps) % 360) : h1 - (maxCCW * i / steps);
  295. if (h < 0) h = h + 360;
  296. if (h > 359) h = h - 360;
  297. s = s1 + ((s2-s1) * i / steps);
  298. v = v1 + ((v2-v1) * i / steps);
  299. // convert to RGB
  300. uint32_t grb = hsv2grb(h, s, v);
  301. *p++ = ((grb & 0x00FF0000) >> 16) * state->brightness / BRIGHTNESS_MAX;
  302. *p++ = ((grb & 0x0000FF00) >> 8) * state->brightness / BRIGHTNESS_MAX;
  303. *p++ = (grb & 0x000000FF) * state->brightness / BRIGHTNESS_MAX;
  304. for (j = 3; j < buffer->colorsPerLed; j++) {
  305. *p++ = 0;
  306. }
  307. }
  308. }
  309. return 0;
  310. }
  311. static int ws2812_effects_gradient_rgb(const char *buffer1, size_t length1) {
  312. ws2812_buffer * buffer = state->buffer;
  313. int segments = (length1 / buffer->colorsPerLed) - 1;
  314. int segmentSize = buffer->size / segments;
  315. uint8_t g1, r1, b1, g2, r2, b2;
  316. int i,j,k;
  317. g2 = *buffer1++;
  318. r2 = *buffer1++;
  319. b2 = *buffer1++;
  320. // skip non-rgb components
  321. for (j = 3; j < buffer->colorsPerLed; j++)
  322. {
  323. *buffer1++;
  324. }
  325. // reference to buffer memory
  326. uint8_t * p = &buffer->values[0];
  327. for (k = 0; k < segments; k++) {
  328. g1 = g2;
  329. r1 = r2;
  330. b1 = b2;
  331. g2 = *buffer1++;
  332. r2 = *buffer1++;
  333. b2 = *buffer1++;
  334. for (j = 3; j < buffer->colorsPerLed; j++) {
  335. *buffer1++;
  336. }
  337. // Fill buffer
  338. int numPixels = segmentSize;
  339. // make sure we fill the strip correctly in case of rounding errors
  340. if (k == segments - 1) {
  341. numPixels = buffer->size - (segmentSize * (segments - 1));
  342. }
  343. int steps = numPixels - 1;
  344. for(i = 0; i < numPixels; i++) {
  345. *p++ = (g1 + ((g2-g1) * i / steps)) * state->brightness / BRIGHTNESS_MAX;
  346. *p++ = (r1 + ((r2-r1) * i / steps)) * state->brightness / BRIGHTNESS_MAX;
  347. *p++ = (b1 + ((b2-b1) * i / steps)) * state->brightness / BRIGHTNESS_MAX;
  348. for (j = 3; j < buffer->colorsPerLed; j++)
  349. {
  350. *p++ = 0;
  351. }
  352. }
  353. }
  354. return 0;
  355. }
  356. /*
  357. * Lights all LEDs in one random color up. Then switches them
  358. * to the next random color.
  359. */
  360. static int ws2812_effects_mode_random_color() {
  361. state->mode_color_index = get_random_wheel_index(state->mode_color_index);
  362. ws2812_buffer * buffer = state->buffer;
  363. uint32_t color = color_wheel(state->mode_color_index);
  364. uint8_t r = ((color & 0x00FF0000) >> 16) * state->brightness / BRIGHTNESS_MAX;
  365. uint8_t g = ((color & 0x0000FF00) >> 8) * state->brightness / BRIGHTNESS_MAX;
  366. uint8_t b = ((color & 0x000000FF) >> 0) * state->brightness / BRIGHTNESS_MAX;
  367. // Fill buffer
  368. int i,j;
  369. uint8_t * p = &buffer->values[0];
  370. for(i = 0; i < buffer->size; i++) {
  371. *p++ = g;
  372. *p++ = r;
  373. *p++ = b;
  374. for (j = 3; j < buffer->colorsPerLed; j++)
  375. {
  376. *p++ = 0;
  377. }
  378. }
  379. }
  380. /*
  381. * Cycles all LEDs at once through a rainbow.
  382. */
  383. static int ws2812_effects_mode_rainbow() {
  384. ws2812_buffer * buffer = state->buffer;
  385. uint32_t color = color_wheel(state->counter_mode_step);
  386. uint8_t r = (color & 0x00FF0000) >> 16;
  387. uint8_t g = (color & 0x0000FF00) >> 8;
  388. uint8_t b = (color & 0x000000FF) >> 0;
  389. // Fill buffer
  390. int i,j;
  391. uint8_t * p = &buffer->values[0];
  392. for(i = 0; i < buffer->size; i++) {
  393. *p++ = g * state->brightness / BRIGHTNESS_MAX;
  394. *p++ = r * state->brightness / BRIGHTNESS_MAX;
  395. *p++ = b * state->brightness / BRIGHTNESS_MAX;
  396. for (j = 3; j < buffer->colorsPerLed; j++)
  397. {
  398. *p++ = 0;
  399. }
  400. }
  401. state->counter_mode_step = (state->counter_mode_step + 1) % 360;
  402. return 0;
  403. }
  404. /*
  405. * Cycles a rainbow over the entire string of LEDs.
  406. */
  407. static int ws2812_effects_mode_rainbow_cycle(int repeat_count) {
  408. ws2812_buffer * buffer = state->buffer;
  409. int i,j;
  410. uint8_t * p = &buffer->values[0];
  411. for(i = 0; i < buffer->size; i++) {
  412. uint16_t wheel_index = (i * 360 / buffer->size * repeat_count) % 360;
  413. uint32_t color = color_wheel(wheel_index);
  414. uint8_t r = ((color & 0x00FF0000) >> 16) * state->brightness / BRIGHTNESS_MAX;
  415. uint8_t g = ((color & 0x0000FF00) >> 8) * state->brightness / BRIGHTNESS_MAX;
  416. uint8_t b = ((color & 0x000000FF) >> 0) * state->brightness / BRIGHTNESS_MAX;
  417. *p++ = g;
  418. *p++ = r;
  419. *p++ = b;
  420. for (j = 3; j < buffer->colorsPerLed; j++)
  421. {
  422. *p++ = 0;
  423. }
  424. }
  425. return 0;
  426. }
  427. /*
  428. * Random flickering.
  429. */
  430. static int ws2812_effects_mode_flicker_int(uint8_t max_flicker) {
  431. ws2812_buffer * buffer = state->buffer;
  432. uint8_t p_g = state->color[0];
  433. uint8_t p_r = state->color[1];
  434. uint8_t p_b = state->color[2];
  435. // Fill buffer
  436. int i,j;
  437. uint8_t * p = &buffer->values[0];
  438. for(i = 0; i < buffer->size; i++) {
  439. int flicker = rand() % (max_flicker > 0 ? max_flicker : 1);
  440. int r1 = p_r-flicker;
  441. int g1 = p_g-flicker;
  442. int b1 = p_b-flicker;
  443. if(g1<0) g1=0;
  444. if(r1<0) r1=0;
  445. if(b1<0) b1=0;
  446. *p++ = g1 * state->brightness / BRIGHTNESS_MAX;
  447. *p++ = r1 * state->brightness / BRIGHTNESS_MAX;
  448. *p++ = b1 * state->brightness / BRIGHTNESS_MAX;
  449. for (j = 3; j < buffer->colorsPerLed; j++) {
  450. *p++ = 0;
  451. }
  452. }
  453. return 0;
  454. }
  455. /**
  456. * Halloween effect
  457. */
  458. static int ws2812_effects_mode_halloween() {
  459. ws2812_buffer * buffer = state->buffer;
  460. int g1 = 50 * state->brightness / BRIGHTNESS_MAX;
  461. int r1 = 255 * state->brightness / BRIGHTNESS_MAX;
  462. int b1 = 0 * state->brightness / BRIGHTNESS_MAX;
  463. int g2 = 0 * state->brightness / BRIGHTNESS_MAX;
  464. int r2 = 255 * state->brightness / BRIGHTNESS_MAX;
  465. int b2 = 130 * state->brightness / BRIGHTNESS_MAX;
  466. // Fill buffer
  467. int i,j;
  468. uint8_t * p = &buffer->values[0];
  469. for(i = 0; i < buffer->size; i++) {
  470. *p++ = (i % 4 < 2) ? g1 : g2;
  471. *p++ = (i % 4 < 2) ? r1 : r2;
  472. *p++ = (i % 4 < 2) ? b1 : b2;
  473. for (j = 3; j < buffer->colorsPerLed; j++)
  474. {
  475. *p++ = 0;
  476. }
  477. }
  478. return 0;
  479. }
  480. static int ws2812_effects_mode_circus_combustus() {
  481. ws2812_buffer * buffer = state->buffer;
  482. int g1 = 0 * state->brightness / BRIGHTNESS_MAX;
  483. int r1 = 255 * state->brightness / BRIGHTNESS_MAX;
  484. int b1 = 0 * state->brightness / BRIGHTNESS_MAX;
  485. int g2 = 255 * state->brightness / BRIGHTNESS_MAX;
  486. int r2 = 255 * state->brightness / BRIGHTNESS_MAX;
  487. int b2 = 255 * state->brightness / BRIGHTNESS_MAX;
  488. // Fill buffer
  489. int i,j;
  490. uint8_t * p = &buffer->values[0];
  491. for(i = 0; i < buffer->size; i++) {
  492. if (i % 6 < 2) {
  493. *p++ = g1;
  494. *p++ = r1;
  495. *p++ = b1;
  496. }
  497. else if (i % 6 < 4) {
  498. *p++ = g2;
  499. *p++ = r2;
  500. *p++ = b2;
  501. }
  502. else {
  503. *p++ = 0;
  504. *p++ = 0;
  505. *p++ = 0;
  506. }
  507. for (j = 3; j < buffer->colorsPerLed; j++)
  508. {
  509. *p++ = 0;
  510. }
  511. }
  512. return 0;
  513. }
  514. /*
  515. * K.I.T.T.
  516. */
  517. static int ws2812_effects_mode_larson_scanner() {
  518. ws2812_buffer * buffer = state->buffer;
  519. int led_index = 0;
  520. ws2812_buffer_fade(buffer, 2, FADE_OUT);
  521. uint16_t pos = 0;
  522. if(state->counter_mode_step < buffer->size) {
  523. pos = state->counter_mode_step;
  524. } else {
  525. pos = (buffer->size * 2) - state->counter_mode_step - 2;
  526. }
  527. pos = pos * buffer->colorsPerLed;
  528. buffer->values[pos + 1] = state->color[1];
  529. buffer->values[pos] = state->color[0];
  530. buffer->values[pos + 2] = state->color[2];
  531. state->counter_mode_step = (state->counter_mode_step + 1) % ((buffer->size * 2) - 2);
  532. }
  533. static int ws2812_effects_mode_color_wipe() {
  534. ws2812_buffer * buffer = state->buffer;
  535. int led_index = (state->counter_mode_step % buffer->size) * buffer->colorsPerLed;
  536. if (state->counter_mode_step >= buffer->size)
  537. {
  538. buffer->values[led_index] = 0;
  539. buffer->values[led_index + 1] = 0;
  540. buffer->values[led_index + 2] = 0;
  541. }
  542. else
  543. {
  544. uint8_t px_r = state->color[1] * state->brightness / BRIGHTNESS_MAX;
  545. uint8_t px_g = state->color[0] * state->brightness / BRIGHTNESS_MAX;
  546. uint8_t px_b = state->color[2] * state->brightness / BRIGHTNESS_MAX;
  547. buffer->values[led_index] = px_g;
  548. buffer->values[led_index + 1] = px_r;
  549. buffer->values[led_index + 2] = px_b;
  550. }
  551. state->counter_mode_step = (state->counter_mode_step + 1) % (buffer->size * 2);
  552. }
  553. static int ws2812_effects_mode_random_dot(uint8_t dots) {
  554. ws2812_buffer * buffer = state->buffer;
  555. // fade out
  556. for(int i=0; i < buffer->size * buffer->colorsPerLed; i++) {
  557. buffer->values[i] = buffer->values[i] >> 1;
  558. }
  559. for(int i=0; i < dots; i++) {
  560. // pick random pixel
  561. int led_index = rand() % buffer->size;
  562. uint32_t color = (state->color[0] << 16) | (state->color[1] << 8) | state->color[2];
  563. if (buffer->colorsPerLed == 4) {
  564. color = color | (state->color[3] << 24);
  565. }
  566. ws2812_set_pixel(led_index, color);
  567. }
  568. state->counter_mode_step = (state->counter_mode_step + 1) % ((buffer->size * 2) - 2);
  569. }
  570. static uint32_t ws2812_effects_mode_delay()
  571. {
  572. // check if delay has been set explicitly
  573. if (state->speed == 0 && state->mode_delay > 0)
  574. {
  575. return state->mode_delay;
  576. }
  577. uint32_t delay = 10;
  578. switch (state->effect_type) {
  579. case WS2812_EFFECT_BLINK:
  580. case WS2812_EFFECT_RAINBOW:
  581. case WS2812_EFFECT_RAINBOW_CYCLE:
  582. delay = 10 + ((1000 * (uint32_t)(SPEED_MAX - state->speed)) / SPEED_MAX);
  583. break;
  584. case WS2812_EFFECT_FLICKER:
  585. case WS2812_EFFECT_FIRE_FLICKER:
  586. case WS2812_EFFECT_FIRE_FLICKER_SOFT:
  587. case WS2812_EFFECT_FIRE_FLICKER_INTENSE:
  588. delay = 30 + (rand() % 100) + (200 * (SPEED_MAX - state->speed) / SPEED_MAX);
  589. break;
  590. case WS2812_EFFECT_RANDOM_COLOR:
  591. case WS2812_EFFECT_HALLOWEEN:
  592. case WS2812_EFFECT_CIRCUS_COMBUSTUS:
  593. case WS2812_EFFECT_LARSON_SCANNER:
  594. case WS2812_EFFECT_CYCLE:
  595. case WS2812_EFFECT_COLOR_WIPE:
  596. case WS2812_EFFECT_RANDOM_DOT:
  597. delay = 10 + ((1000 * (uint32_t)(SPEED_MAX - state->speed)) / SPEED_MAX);
  598. break;
  599. }
  600. return delay;
  601. }
  602. /**
  603. * run loop for the effects.
  604. */
  605. static void ws2812_effects_loop(void* p)
  606. {
  607. if (state->effect_type == WS2812_EFFECT_BLINK)
  608. {
  609. ws2812_effects_mode_blink();
  610. }
  611. else if (state->effect_type == WS2812_EFFECT_RAINBOW)
  612. {
  613. ws2812_effects_mode_rainbow();
  614. }
  615. else if (state->effect_type == WS2812_EFFECT_RAINBOW_CYCLE)
  616. {
  617. // the rainbow cycle effect can be achieved by shifting the buffer
  618. ws2812_buffer_shift_prepared(state->prepare);
  619. }
  620. else if (state->effect_type == WS2812_EFFECT_FLICKER)
  621. {
  622. int flicker_value = state->effect_int_param1 != EFFECT_PARAM_INVALID ? state->effect_int_param1 : 100;
  623. if (flicker_value == 0) {
  624. flicker_value = 50;
  625. }
  626. ws2812_effects_mode_flicker_int(flicker_value);
  627. state->counter_mode_step = (state->counter_mode_step + 1) % 256;
  628. }
  629. else if (state->effect_type == WS2812_EFFECT_FIRE_FLICKER)
  630. {
  631. ws2812_effects_mode_flicker_int(110);
  632. state->counter_mode_step = (state->counter_mode_step + 1) % 256;
  633. }
  634. else if (state->effect_type == WS2812_EFFECT_FIRE_FLICKER_SOFT)
  635. {
  636. ws2812_effects_mode_flicker_int(70);
  637. state->counter_mode_step = (state->counter_mode_step + 1) % 256;
  638. }
  639. else if (state->effect_type == WS2812_EFFECT_FIRE_FLICKER_INTENSE)
  640. {
  641. ws2812_effects_mode_flicker_int(170);
  642. state->counter_mode_step = (state->counter_mode_step + 1) % 256;
  643. }
  644. else if (state->effect_type == WS2812_EFFECT_RANDOM_COLOR)
  645. {
  646. ws2812_effects_mode_random_color();
  647. }
  648. else if (state->effect_type == WS2812_EFFECT_HALLOWEEN)
  649. {
  650. ws2812_buffer_shift_prepared(state->prepare);
  651. }
  652. else if (state->effect_type == WS2812_EFFECT_CIRCUS_COMBUSTUS)
  653. {
  654. ws2812_buffer_shift_prepared(state->prepare);
  655. }
  656. else if (state->effect_type == WS2812_EFFECT_LARSON_SCANNER)
  657. {
  658. ws2812_effects_mode_larson_scanner();
  659. }
  660. else if (state->effect_type == WS2812_EFFECT_CYCLE)
  661. {
  662. ws2812_buffer_shift_prepared(state->prepare);
  663. }
  664. else if (state->effect_type == WS2812_EFFECT_COLOR_WIPE)
  665. {
  666. ws2812_effects_mode_color_wipe();
  667. }
  668. else if (state->effect_type == WS2812_EFFECT_RANDOM_DOT)
  669. {
  670. uint8_t dots = state->effect_int_param1 != EFFECT_PARAM_INVALID ? state->effect_int_param1 : 1;
  671. ws2812_effects_mode_random_dot(dots);
  672. }
  673. // set the new delay for this effect
  674. state->mode_delay = ws2812_effects_mode_delay();
  675. // call count
  676. state->counter_mode_call = (state->counter_mode_call + 1) % UINT32_MAX;
  677. // write the buffer
  678. ws2812_write(state->buffer);
  679. // set the timer
  680. if (state->running == 1 && state->mode_delay >= 10)
  681. if (state->running == 1 && state->mode_delay >= 10)
  682. {
  683. os_timer_disarm(&(state->os_t));
  684. os_timer_arm(&(state->os_t), state->mode_delay, FALSE);
  685. }
  686. }
  687. void prepare_shift(lua_State* L, ws2812_buffer * buffer, int shiftValue, unsigned shift_type, int pos_start, int pos_end){
  688. // deinit old effect
  689. if (state->prepare) {
  690. luaM_free(L, state->prepare);
  691. }
  692. state->prepare = ws2812_buffer_get_shift_prepare(L, buffer, shiftValue, shift_type, pos_start, pos_end);
  693. }
  694. /**
  695. * Set the active effect mode
  696. */
  697. static int ws2812_effects_set_mode(lua_State* L) {
  698. luaL_argcheck(L, state != NULL, 1, LIBRARY_NOT_INITIALIZED_ERROR_MSG);
  699. // opts must be same order as effect type enum
  700. static const char * const opts[] = {"static", "blink", "gradient", "gradient_rgb", "random_color", "rainbow",
  701. "rainbow_cycle", "flicker", "fire", "fire_soft", "fire_intense", "halloween", "circus_combustus",
  702. "larson_scanner", "cycle", "color_wipe", "random_dot", NULL};
  703. int type = luaL_checkoption(L, 1, NULL, opts);
  704. state->effect_type = type;
  705. int effect_param = EFFECT_PARAM_INVALID;
  706. // check additional int parameter
  707. // First mandatory parameter
  708. int arg_type = lua_type(L, 2);
  709. if (arg_type == LUA_TNONE || arg_type == LUA_TNIL)
  710. {
  711. // we don't have a second parameter
  712. }
  713. else if(arg_type == LUA_TNUMBER)
  714. {
  715. effect_param = luaL_optinteger( L, 2, EFFECT_PARAM_INVALID );
  716. }
  717. // initialize the effect
  718. state->counter_mode_step = 0;
  719. switch (state->effect_type) {
  720. case WS2812_EFFECT_STATIC:
  721. // fill with currently set color
  722. ws2812_effects_fill_color();
  723. state->mode_delay = 250;
  724. break;
  725. case WS2812_EFFECT_BLINK:
  726. ws2812_effects_mode_blink();
  727. break;
  728. case WS2812_EFFECT_GRADIENT:
  729. if(arg_type == LUA_TSTRING)
  730. {
  731. size_t length1;
  732. const char *buffer1 = lua_tolstring(L, 2, &length1);
  733. if ((length1 / state->buffer->colorsPerLed < 2) || (length1 % state->buffer->colorsPerLed != 0))
  734. {
  735. luaL_argerror(L, 2, "must be at least two colors and same size as buffer colors");
  736. }
  737. ws2812_effects_gradient(buffer1, length1);
  738. ws2812_write(state->buffer);
  739. }
  740. else
  741. {
  742. luaL_argerror(L, 2, "string expected");
  743. }
  744. break;
  745. case WS2812_EFFECT_GRADIENT_RGB:
  746. if(arg_type == LUA_TSTRING)
  747. {
  748. size_t length1;
  749. const char *buffer1 = lua_tolstring(L, 2, &length1);
  750. if ((length1 / state->buffer->colorsPerLed < 2) || (length1 % state->buffer->colorsPerLed != 0))
  751. {
  752. luaL_argerror(L, 2, "must be at least two colors and same size as buffer colors");
  753. }
  754. ws2812_effects_gradient_rgb(buffer1, length1);
  755. ws2812_write(state->buffer);
  756. }
  757. else
  758. {
  759. luaL_argerror(L, 2, "string expected");
  760. }
  761. break;
  762. case WS2812_EFFECT_RANDOM_COLOR:
  763. ws2812_effects_mode_random_color();
  764. break;
  765. case WS2812_EFFECT_RAINBOW:
  766. ws2812_effects_mode_rainbow();
  767. break;
  768. case WS2812_EFFECT_RAINBOW_CYCLE:
  769. ws2812_effects_mode_rainbow_cycle(effect_param != EFFECT_PARAM_INVALID ? effect_param : 1);
  770. prepare_shift(L, state->buffer, 1, SHIFT_CIRCULAR, 1, -1);
  771. break;
  772. case WS2812_EFFECT_FLICKER:
  773. state->effect_int_param1 = effect_param;
  774. break;
  775. case WS2812_EFFECT_FIRE_FLICKER:
  776. case WS2812_EFFECT_FIRE_FLICKER_SOFT:
  777. case WS2812_EFFECT_FIRE_FLICKER_INTENSE:
  778. state->color[0] = 255-40;
  779. state->color[1] = 255;
  780. state->color[2] = 40;
  781. state->color[3] = 0;
  782. break;
  783. case WS2812_EFFECT_HALLOWEEN:
  784. ws2812_effects_mode_halloween();
  785. prepare_shift(L, state->buffer, 1, SHIFT_CIRCULAR, 1, -1);
  786. break;
  787. case WS2812_EFFECT_CIRCUS_COMBUSTUS:
  788. ws2812_effects_mode_circus_combustus();
  789. prepare_shift(L, state->buffer, 1, SHIFT_CIRCULAR, 1, -1);
  790. break;
  791. case WS2812_EFFECT_LARSON_SCANNER:
  792. ws2812_effects_mode_larson_scanner();
  793. break;
  794. case WS2812_EFFECT_CYCLE:
  795. if (effect_param != EFFECT_PARAM_INVALID) {
  796. state->effect_int_param1 = effect_param;
  797. }
  798. prepare_shift(L, state->buffer, state->effect_int_param1, SHIFT_CIRCULAR, 1, -1);
  799. break;
  800. case WS2812_EFFECT_COLOR_WIPE:
  801. // fill buffer with black. r,g,b,w = 0
  802. ws2812_effects_fill_buffer(0, 0, 0, 0);
  803. ws2812_effects_mode_color_wipe();
  804. break;
  805. case WS2812_EFFECT_RANDOM_DOT:
  806. // check if more than 1 dot shall be set
  807. state->effect_int_param1 = effect_param;
  808. // fill buffer with black. r,g,b,w = 0
  809. ws2812_effects_fill_buffer(0, 0, 0, 0);
  810. break;
  811. }
  812. }
  813. /*
  814. * Start the effect execution
  815. */
  816. static int ws2812_effects_start(lua_State* L) {
  817. //NODE_DBG("pin:%d, level:%d \n", pin, level);
  818. luaL_argcheck(L, state != NULL, 1, LIBRARY_NOT_INITIALIZED_ERROR_MSG);
  819. if (state != NULL) {
  820. os_timer_disarm(&(state->os_t));
  821. state->running = 1;
  822. state->counter_mode_call = 0;
  823. state->counter_mode_step = 0;
  824. // set the timer
  825. os_timer_setfn(&(state->os_t), ws2812_effects_loop, NULL);
  826. os_timer_arm(&(state->os_t), state->mode_delay, FALSE);
  827. }
  828. return 0;
  829. }
  830. /*
  831. * Stop the effect execution
  832. */
  833. static int ws2812_effects_stop(lua_State* L) {
  834. luaL_argcheck(L, state != NULL, 1, LIBRARY_NOT_INITIALIZED_ERROR_MSG);
  835. if (state != NULL) {
  836. os_timer_disarm(&(state->os_t));
  837. state->running = 0;
  838. }
  839. return 0;
  840. }
  841. static int ws2812_effects_tostring(lua_State* L) {
  842. luaL_Buffer result;
  843. luaL_buffinit(L, &result);
  844. luaL_addchar(&result, '[');
  845. luaL_addstring(&result, "effects");
  846. luaL_addchar(&result, ']');
  847. luaL_pushresult(&result);
  848. return 1;
  849. }
  850. LROT_BEGIN(ws2812_effects_map, NULL, 0)
  851. LROT_FUNCENTRY( __tostring, ws2812_effects_tostring )
  852. LROT_FUNCENTRY( init, ws2812_effects_init )
  853. LROT_FUNCENTRY( set_brightness, ws2812_effects_set_brightness )
  854. LROT_FUNCENTRY( set_color, ws2812_effects_set_color )
  855. LROT_FUNCENTRY( set_speed, ws2812_effects_set_speed )
  856. LROT_FUNCENTRY( set_delay, ws2812_effects_set_delay )
  857. LROT_FUNCENTRY( set_mode, ws2812_effects_set_mode )
  858. LROT_FUNCENTRY( start, ws2812_effects_start )
  859. LROT_FUNCENTRY( stop, ws2812_effects_stop )
  860. LROT_FUNCENTRY( get_delay, ws2812_effects_get_delay )
  861. LROT_FUNCENTRY( get_speed, ws2812_effects_get_speed )
  862. LROT_END(ws2812_effects_map, NULL, 0)
  863. NODEMCU_MODULE(WS2812_EFFECTS, "ws2812_effects", ws2812_effects_map, NULL);