ws2812.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "pico/stdlib.h"
  9. #include "hardware/pio.h"
  10. #include "hardware/clocks.h"
  11. #include "ws2812.pio.h"
  12. #define IS_RGBW true
  13. #define NUM_PIXELS 150
  14. #ifdef PICO_DEFAULT_WS2812_PIN
  15. #define WS2812_PIN PICO_DEFAULT_WS2812_PIN
  16. #else
  17. // default to pin 2 if the board doesn't have a default WS2812 pin defined
  18. #define WS2812_PIN 2
  19. #endif
  20. static inline void put_pixel(uint32_t pixel_grb) {
  21. pio_sm_put_blocking(pio0, 0, pixel_grb << 8u);
  22. }
  23. static inline uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b) {
  24. return
  25. ((uint32_t) (r) << 8) |
  26. ((uint32_t) (g) << 16) |
  27. (uint32_t) (b);
  28. }
  29. void pattern_snakes(uint len, uint t) {
  30. for (uint i = 0; i < len; ++i) {
  31. uint x = (i + (t >> 1)) % 64;
  32. if (x < 10)
  33. put_pixel(urgb_u32(0xff, 0, 0));
  34. else if (x >= 15 && x < 25)
  35. put_pixel(urgb_u32(0, 0xff, 0));
  36. else if (x >= 30 && x < 40)
  37. put_pixel(urgb_u32(0, 0, 0xff));
  38. else
  39. put_pixel(0);
  40. }
  41. }
  42. void pattern_random(uint len, uint t) {
  43. if (t % 8)
  44. return;
  45. for (int i = 0; i < len; ++i)
  46. put_pixel(rand());
  47. }
  48. void pattern_sparkle(uint len, uint t) {
  49. if (t % 8)
  50. return;
  51. for (int i = 0; i < len; ++i)
  52. put_pixel(rand() % 16 ? 0 : 0xffffffff);
  53. }
  54. void pattern_greys(uint len, uint t) {
  55. int max = 100; // let's not draw too much current!
  56. t %= max;
  57. for (int i = 0; i < len; ++i) {
  58. put_pixel(t * 0x10101);
  59. if (++t >= max) t = 0;
  60. }
  61. }
  62. typedef void (*pattern)(uint len, uint t);
  63. const struct {
  64. pattern pat;
  65. const char *name;
  66. } pattern_table[] = {
  67. {pattern_snakes, "Snakes!"},
  68. {pattern_random, "Random data"},
  69. {pattern_sparkle, "Sparkles"},
  70. {pattern_greys, "Greys"},
  71. };
  72. //int main() {
  73. // //set_sys_clock_48();
  74. // stdio_init_all();
  75. // printf("WS2812 Smoke Test, using pin %d", WS2812_PIN);
  76. //
  77. // // todo get free sm
  78. // PIO pio = pio0;
  79. // int sm = 0;
  80. // uint offset = pio_add_program(pio, &ws2812_program);
  81. //
  82. // ws2812_program_init(pio, sm, offset, WS2812_PIN, 800000, IS_RGBW);
  83. //
  84. // int t = 0;
  85. // while (1) {
  86. // int pat = rand() % count_of(pattern_table);
  87. // int dir = (rand() >> 30) & 1 ? 1 : -1;
  88. // puts(pattern_table[pat].name);
  89. // puts(dir == 1 ? "(forward)" : "(backward)");
  90. // for (int i = 0; i < 1000; ++i) {
  91. // pattern_table[pat].pat(NUM_PIXELS, t);
  92. // sleep_ms(10);
  93. // t += dir;
  94. // }
  95. // }
  96. //}