01_leds.ino 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <Adafruit_DotStar.h>
  2. #define NUM_DOTSTAR 5
  3. #define PROGRESS_NUM_STATES 10
  4. Adafruit_DotStar pixels(
  5. NUM_DOTSTAR,
  6. PIN_DOTSTAR_DATA,
  7. PIN_DOTSTAR_CLOCK,
  8. DOTSTAR_BRG
  9. );
  10. boolean ledsShowingProgress = false;
  11. int ledProgress = 0;
  12. const uint32_t PROGRESS_BLUE = pixels.Color(0, 0, 255),
  13. PROGRESS_GREEN = pixels.Color(255, 0, 0),
  14. PROGRESS_RED = pixels.Color(0, 255, 0);
  15. // Which LEDs should be lit up for a given position in the progress.
  16. // This could be calculated, but I have memory to spare, and this is
  17. // easier at the moment.
  18. uint16_t ledProgressMap[PROGRESS_NUM_STATES] = {
  19. 0b00000,
  20. 0b00001,
  21. 0b00011,
  22. 0b00111,
  23. 0b01111,
  24. 0b11111,
  25. 0b11110,
  26. 0b11100,
  27. 0b11000,
  28. 0b10000
  29. };
  30. void initLeds() {
  31. pixels.begin();
  32. pixels.show();
  33. pixels.setBrightness(2);
  34. hideProgress();
  35. }
  36. // Update LEDs to show loading, such as connecting to wifi
  37. void showProgress(uint32_t colour) {
  38. if(!ledsShowingProgress) {
  39. // Reset the progress display
  40. ledProgress = 0;
  41. ledsShowingProgress = true;
  42. }
  43. ledProgress = (ledProgress + 1) % PROGRESS_NUM_STATES;
  44. for(byte i = 0; i < NUM_DOTSTAR; i++) {
  45. boolean on = ((1 << i) & ledProgressMap[ledProgress]) > 0;
  46. pixels.setPixelColor(
  47. i,
  48. on ? colour : 0
  49. );
  50. }
  51. pixels.show();
  52. }
  53. // Turn off the LEDs
  54. void hideProgress() {
  55. ledsShowingProgress = false;
  56. for(byte i = 0; i < NUM_DOTSTAR; i++) {
  57. pixels.setPixelColor(i, 0);
  58. }
  59. pixels.show();
  60. }
  61. void showError() {
  62. pixels.fill(PROGRESS_RED);
  63. pixels.show();
  64. }