Advertisement
cvalente

Ground

Aug 21st, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.94 KB | None | 0 0
  1. #include "FastLED/FastLED.h"
  2. FASTLED_USING_NAMESPACE;
  3.  
  4. /*******************************************************************************
  5.  *  GROUND code
  6.  *  minimalistic colour animation based on hex colour palette
  7.  *  animation is affected by external "ping" (groundPingOnChange)
  8.  *
  9.  *  Publishes: NA
  10.  *  Subscribes: ping
  11.  *
  12.  *  Setters:
  13.  *  - set_bright(String val)
  14.  *  - set_speed(String val)
  15.  *  Convention for setters, return 1 if successful, -1 otherwise
  16.  *  
  17.  *  Built for Photon, depends on FastLED library
  18.  *
  19.  *******************************************************************************/
  20. // KEEP HANDY
  21. // https://docs.particle.io/support/troubleshooting/troubleshooting-support/photon/
  22.  
  23. #define LED_PIN D6
  24. #define STATUS_LED D7
  25. #define NUM_LEDS (2 + 2) + (12 +12) + (20 + 20) + (30 + 30) + (38 + 38) + (46 + 46) + 56
  26. #define COLOR_ORDER GRB
  27. #define BRIGHTNESS  64
  28. #define LED_TYPE WS2812B
  29. #define TIMEOUT 5000
  30.  
  31. /* Initialize Variables */
  32. int UPDATES_PER_SECOND =  40; // refresh rate
  33. int fluidity; // affects animation
  34. int palette;
  35.  
  36. /* Debug */
  37. int signalStrength;
  38.  
  39. /* Initialize LED objects */
  40. CRGBPalette16 black, currentPalette, targetPalette;
  41. TBlendType currentBlending;
  42. CRGB leds[NUM_LEDS];
  43.  
  44. int block1 = 7;
  45. int block2 = 13;
  46. int block3 = 17;
  47. int block4 = 21;
  48.  
  49. CRGB colours[]  = { 0x2F00D0, 0x5500AB, 0x84007C, 0xB5004B,  0xAB1A25, 0xC2003E, 0x8F0071,
  50.                 //  br violet, violet    pink      lilac      lilac    l purple  purple
  51.                     0xE5001B, 0xE81700, 0xB84700, 0xAB7700, 0xABAB00, 0xD97925,
  52.                 //  red/or    d amber   amber     straw      red      pal sun
  53.                     0xAB5500, 0xDD2200, 0xF2000E, 0x0007F9,
  54.                 //  sunset    vivid su  d sunset  magenta
  55.                      0x5F00A1, 0x2F00D0, 0x00FF9E, 0x27FF6A
  56.                 //  d blue    bri blue    aqua     turquoise
  57.                     };
  58.  
  59. SYSTEM_MODE(AUTOMATIC); // this means I will be able to connect manually
  60. SYSTEM_THREAD(ENABLED); // this means my code runs in a separate thread form particle stuff
  61. STARTUP(WiFi.selectAntenna(ANT_AUTO)); // continually switches at high speed between antennas
  62.  
  63. void setup() {
  64.    
  65.     /* Attempt connecting to network */
  66.     waitFor(Particle.connected, TIMEOUT); //it will follow otherwise
  67.    
  68.     /* Declare and initialize indicator LED */
  69.     pinMode(STATUS_LED, OUTPUT);
  70.     pinResetFast(STATUS_LED);
  71.    
  72.     /* Setup AUX */
  73.     fluidity = 1;
  74.     palette = 0; // 0 - Black 1 - Normal - 2 Generated
  75.    
  76.     /* Declare particle variables and functions */    
  77.     Particle.variable( "signal", &signalStrength, INT ),
  78.     //curl "https://api.particle.io/v1/devices/1b002c001247353236343033/signal?access_token=e9ff4e5ec3b85e66f7e50e1c0e2fa606d0722c02"
  79.    
  80.     Particle.subscribe( "ping", myHandler, MY_DEVICES );
  81.    
  82.     Particle.function( "set_bright", setBrightness );
  83.     Particle.function( "set_speed", setSpeed );
  84.  
  85.     /* Setup Colours */
  86.     currentPalette = CRGBPalette16(CRGB::Black);
  87.     paletteFromHex();
  88.     currentBlending = LINEARBLEND;
  89.  
  90.     /* Setup LEDs */
  91.     delay( 3000 ); // power-up safety delay
  92.     FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  93.     FastLED.setBrightness( BRIGHTNESS );
  94.  
  95. }
  96.  
  97. void loop() {
  98.    
  99.     /* Update aux */
  100.     static uint8_t startIndex = 0;
  101.     startIndex++;
  102.     signalStrength = WiFi.RSSI();
  103.    
  104.     /* Call colour functions*/
  105.     FillLEDsFromPaletteColors(startIndex); // writes palette to LEDs
  106.    
  107.    
  108.     uint8_t maxChanges = 6;
  109.     nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges);
  110.    
  111.     //EVERY_N_HOURS(1) { lessFluidity(); }
  112.     EVERY_N_MINUTES(3) { lessFluidity(); }
  113.    
  114.     /* Update LED and wait */
  115.     FastLED.show();
  116.     FastLED.delay(1000 / UPDATES_PER_SECOND );
  117.    
  118.     pinResetFast(STATUS_LED);
  119. }
  120.  
  121. /*******************************************************************************
  122.  * Function Name  : myHandler
  123.  * Description    : Cloud Function - receives interaction
  124.  * Input          : char event name, char event data
  125.  * Output         : update fluidity
  126.  * Return         : 1 on success and -1 otherwise
  127.  *******************************************************************************/
  128.  
  129. void myHandler(const char *event, const char *data) {
  130.    
  131.     pinSetFast(STATUS_LED);
  132.     if (fluidity < 3) fluidity++;
  133.     newPalette();
  134.    
  135. }
  136.  
  137. /*******************************************************************************
  138.  * Function Name  : lessFluidity
  139.  * Description    : decreases fluidity value
  140.  * Input          : NA
  141.  * Output         : update fluidity
  142.  * Return         : NA
  143.  *******************************************************************************/
  144.  
  145. void lessFluidity() {
  146.    
  147.     if (fluidity > 1) fluidity --;
  148.    
  149. }
  150.  
  151. /*******************************************************************************
  152.  * Function Name  : paletteFromHex
  153.  * Description    : creates palette from hex values
  154.  * Input          : which palette to update
  155.  * Output         : update targetPalette
  156.  * Return         : NA
  157.  *******************************************************************************/
  158.  
  159. void paletteFromHex(){
  160.     targetPalette = CRGBPalette16(
  161.     0x5500AB, 0x84007C, 0xB5004B, 0xE5001B,
  162.     0xE81700, 0xB84700, 0xAB7700, 0xABAB00,
  163.     0xAB5500, 0xDD2200, 0xF2000E, 0xC2003E,
  164.     0x8F0071, 0x5F00A1, 0x2F00D0, 0x0007F9
  165.     );
  166.  
  167. //  violet, pink, lilac, red / magenta,
  168. //  red / orange, dark amber, amber, straw ( could be nicer )
  169. //  sunset orange, vivid sunset, dark sunset, magenta
  170. //  light purple, purple, dark blue, bright blue
  171.  
  172. }
  173.  
  174.  
  175. /*******************************************************************************
  176.  * Function Name  : newPalette
  177.  * Description    : creates palette from list of colours
  178.  * Input          : NA
  179.  * Output         : update targetPalette
  180.  * Return         : NA
  181.  *******************************************************************************/
  182.  
  183.  
  184. void newPalette(){
  185.    
  186.     int r1 = random(0, block1 + 1);
  187.     int r2 = random(block1, block2 + 1);
  188.     int r3 = random(block2, block3 + 1);
  189.     int r4 = random(block3, block4 + 1);
  190.    
  191.     CRGB c1 = colours[r1];
  192.     CRGB c2 = colours[r2];
  193.     CRGB c3 = colours[r3];
  194.     CRGB c4 = colours[r4];
  195.    
  196.     targetPalette = CRGBPalette16(c1, c2, c3, c4);
  197.  
  198. }
  199.  
  200. /*******************************************************************************
  201.  * Function Name  : FillLEDsFromPaletteColors
  202.  * Description    : update colour value for LED pixels
  203.  * Input          : int colorIndex
  204.  * Output         : NA
  205.  * Return         : NA
  206.  *******************************************************************************/
  207.  
  208. void FillLEDsFromPaletteColors(uint8_t colorIndex) {
  209.     uint8_t brightness = 255;
  210.     for( int i = 0; i < NUM_LEDS; i++) {
  211.         leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
  212.         colorIndex += 3;
  213.     }
  214. }
  215.  
  216.  
  217. /*******************************************************************************
  218.  * Function Name  : setBrightness
  219.  * Description    : Cloud Function - overrides LED brightness
  220.  * Input          : int brightness value
  221.  * Output         : NA
  222.  * Return         : 1 if successful, -1 otherwise
  223.  *******************************************************************************/
  224.  
  225. int setBrightness(String val) {
  226.   int newVal;
  227.     if (newVal = val.toInt()){
  228.         FastLED.setBrightness( newVal );
  229.         return 1;
  230.     }
  231.     return -1;
  232. }
  233.  
  234. /*******************************************************************************
  235.  * Function Name  : setSpeed
  236.  * Description    : Cloud Function - overrides animation speed
  237.  * Input          : int speed value
  238.  * Output         : NA
  239.  * Return         : 1 if successful, -1 otherwise
  240.  *******************************************************************************/
  241.  
  242. int setSpeed(String val) {
  243.   int newVal;
  244.     if (newVal = val.toInt()){
  245.         UPDATES_PER_SECOND = newVal;
  246.         return 1;
  247.     }
  248.     return -1;
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement