Guest User

Untitled

a guest
Jan 23rd, 2022
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.83 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define NUM_LEDS  32
  4. #define DATA_PIN 11
  5. #define CLOCK_PIN 13
  6. #define SPEED 115200
  7.  
  8. const int eingang1 = 2;
  9. const int eingang2 = 3;
  10. const int eingang3 = 4;
  11. int wert1  = 0;
  12. int wert2  = 0;
  13. int wert3  = 0;
  14.  
  15.  
  16. CRGB leds[NUM_LEDS];
  17. uint8_t * ledsRaw = (uint8_t *)leds;
  18.  
  19. static const uint8_t magic[] = {'A', 'd', 'a'};
  20. #define MAGICSIZE  sizeof(magic)
  21. #define HEADERSIZE (MAGICSIZE + 3)
  22.  
  23. #define MODE_HEADER 0
  24. #define MODE_DATA   2
  25.  
  26. static const unsigned long serialTimeout = 150000; // 150 seconds
  27.  
  28.  
  29.  
  30. void setup() {
  31.   FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, BGR>(leds, NUM_LEDS);
  32.   pinMode(eingang1, INPUT_PULLUP);
  33.   pinMode(eingang2, INPUT_PULLUP);
  34.   pinMode(eingang3, INPUT_PULLUP);
  35.  
  36.  
  37.  
  38. }
  39.  
  40. void loop() {
  41.   wert1 = digitalRead(eingang1);
  42.   wert2 = digitalRead(eingang2);
  43.   wert3 = digitalRead(eingang3);
  44.  
  45.   if (wert1 == LOW) {
  46.     //vollweiss
  47.     for (int i = 0; i < NUM_LEDS; i++) {
  48.       leds[i] = CHSV(255 , 0, 255);
  49.     }
  50.     FastLED.show();
  51.   }
  52.   if (wert2 == LOW) {
  53.     //halbweiss
  54.     for (int i = 0; i < NUM_LEDS; i++) {
  55.       leds[i] = CHSV(155 , 0, 155);
  56.     }
  57.     FastLED.show();
  58.   }
  59.   if (wert3 == LOW) {
  60.  
  61.  
  62.     uint8_t
  63.     buffer[256],
  64.            indexIn       = 0,
  65.            indexOut      = 0,
  66.            mode          = MODE_HEADER,
  67.            hi, lo, chk, i, spiFlag;
  68.     int16_t
  69.     bytesBuffered = 0,
  70.     hold          = 0,
  71.     c;
  72.     int32_t
  73.     bytesRemaining;
  74.     unsigned long
  75.     startTime,
  76.     lastByteTime,
  77.     lastAckTime,
  78.     t;
  79.     int32_t outPos = 0;
  80.  
  81.     Serial.begin(SPEED); // Teensy/32u4 disregards baud rate; is OK!
  82.  
  83.     Serial.print("Ada\n"); // Send ACK string to host
  84.  
  85.     startTime    = micros();
  86.     lastByteTime = lastAckTime = millis();
  87.  
  88.     // loop() is avoided as even that small bit of function overhead
  89.     // has a measurable impact on this code's overall throughput.
  90.  
  91.     for (;;) {
  92.  
  93.       // Implementation is a simple finite-state machine.
  94.       // Regardless of mode, check for serial input each time:
  95.       t = millis();
  96.       if ((bytesBuffered < 256) && ((c = Serial.read()) >= 0)) {
  97.         buffer[indexIn++] = c;
  98.         bytesBuffered++;
  99.         lastByteTime = lastAckTime = t; // Reset timeout counters
  100.       } else {
  101.         // No data received.  If this persists, send an ACK packet
  102.         // to host once every second to alert it to our presence.
  103.         if ((t - lastAckTime) > 1000) {
  104.           Serial.print("Ada\n"); // Send ACK string to host
  105.           lastAckTime = t; // Reset counter
  106.         }
  107.         // If no data received for an extended time, turn off all LEDs.
  108.         if ((t - lastByteTime) > serialTimeout) {
  109.           memset(leds, 0,  NUM_LEDS * sizeof(struct CRGB)); //filling Led array by zeroes
  110.           FastLED.show();
  111.           lastByteTime = t; // Reset counter
  112.         }
  113.       }
  114.  
  115.       switch (mode) {
  116.  
  117.         case MODE_HEADER:
  118.  
  119.           // In header-seeking mode.  Is there enough data to check?
  120.           if (bytesBuffered >= HEADERSIZE) {
  121.             // Indeed.  Check for a 'magic word' match.
  122.             for (i = 0; (i < MAGICSIZE) && (buffer[indexOut++] == magic[i++]););
  123.             if (i == MAGICSIZE) {
  124.               // Magic word matches.  Now how about the checksum?
  125.               hi  = buffer[indexOut++];
  126.               lo  = buffer[indexOut++];
  127.               chk = buffer[indexOut++];
  128.               if (chk == (hi ^ lo ^ 0x55)) {
  129.                 // Checksum looks valid.  Get 16-bit LED count, add 1
  130.                 // (# LEDs is always > 0) and multiply by 3 for R,G,B.
  131.                 bytesRemaining = 3L * (256L * (long)hi + (long)lo + 1L);
  132.                 bytesBuffered -= 3;
  133.                 outPos = 0;
  134.                 memset(leds, 0,  NUM_LEDS * sizeof(struct CRGB));
  135.                 mode           = MODE_DATA; // Proceed to latch wait mode
  136.               } else {
  137.                 // Checksum didn't match; search resumes after magic word.
  138.                 indexOut  -= 3; // Rewind
  139.               }
  140.             } // else no header match.  Resume at first mismatched byte.
  141.             bytesBuffered -= i;
  142.           }
  143.           break;
  144.  
  145.         case MODE_DATA:
  146.  
  147.           if (bytesRemaining > 0) {
  148.             if (bytesBuffered > 0) {
  149.               if (outPos < sizeof(leds))
  150.                 ledsRaw[outPos++] = buffer[indexOut++];   // Issue next byte
  151.               bytesBuffered--;
  152.               bytesRemaining--;
  153.             }
  154.             // If serial buffer is threatening to underrun, start
  155.             // introducing progressively longer pauses to allow more
  156.             // data to arrive (up to a point).
  157.           } else {
  158.             // End of data -- issue latch:
  159.             startTime  = micros();
  160.             mode       = MODE_HEADER; // Begin next header search
  161.             FastLED.show();
  162.           }
  163.       } // end switch
  164.     } // end for(;;)
  165.   }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment