Stap 4: afstandsbediening
Zoals gedefinieerd:
#define UNIFIEDFUNCTION "theFunction"
en dan in Setup:
Spark.function(UNIFIEDFUNCTION, setColorInModeFunction);
Deze functie aangeroepen vanuit het Internet via de NSURLRequest dan de takken in prachtig delen volgens de mode en de waarden tellen. Dit deel misschien geavanceerdere kunt krijgen, een controlesom kan worden ingevoerd, enzovoort.
int setColorInModeFunction(String args) { unsigned char allValuesMaxCount = 25; // 64 chars -24 separator = 40 digis; // 40/25 digits = 1.6 digits per value double allValues[allValuesMaxCount]; unsigned int count = parseArgsIntoDoubles(args, allValues); if(count > allValuesMaxCount) {// How to deal with a once-in-a-lifetime situation? Serial.println("Houston, we have a problem!"); } double mode = allValues[0]; if(THEFEATURE_FUNCTION == mode)// set overall brightness and rotation {// 1 == rpm, 2 = brightness double rotationExponent = allValues[1]; rotationDirectionIsClock = (0 < rotationExponent); float buttonRpmExp = (float)fabs(rotationExponent); rpm = (unsigned int) round(pow(2,buttonRpmExp));// the only case we get fractional exponents, e.g. smooth changes buttonRpmIndex = (unsigned char)round(buttonRpmExp);// to get the buttons as index right brightness = (float)constrain(allValues[2],0.0,1.0); int exp; double fraction = frexp(256.0*brightness, &exp); brightnessIndex = (unsigned char)constrain(exp,0,7); } else if (SINGLECOL_FUNCTION == mode && 3 == count)// set one indexed color { unsigned int aColorIndex = constrain((unsigned long)(floor(allValues[1])),0,11); theColorValues[aColorIndex] = constrain((unsigned long)(floor(allValues[2])),0,0xFFFFFF); } else // some more or all colors will be set { unsigned char startIndex = 0; unsigned char offset = 0; unsigned char colorsCount = 12; // 12: there will be 12 values for all the LEDs, there is no mode, value is positive if( 0 <= mode && 12 == count)// we got all values, no explicit mode { // nothing } else if(INDEXEDCOL_FUNCTION == mode) { // set all values for the loop startIndex = constrain((unsigned long)(floor(allValues[1])),0,11); colorsCount = constrain((unsigned long)(floor(allValues[2])),1,12); offset = 3; } if(13 > startIndex + colorsCount)// no more than 12 for(unsigned int i = 0; i < colorsCount ; i++) {// set the indexed color from offset to end unsigned char readIndex = offset + i; // for readability, start at offset; theColorValues[i + startIndex] = constrain((unsigned long)(floor(allValues[readIndex])),0,0xFFFFFF); }; } return count; }
De afbeeldingen tonen enkele speciale gevallen. Alle LED rood verkort de oproep, omdat de rode kleur in hex 0x0000FF, decimale slechts 256 is. Dit is alleen op verzoek. De volgende twee beelden tonen beide een heleboel LEDs in zwart, waardoor een reeks van '0'.
De laatstgenoemde wordt de interface kunt u een kleur met een waaronder en een schuifregelaar Helderheid instellen weergegeven. Vanuit deze interface kan de kleur worden ingesteld als één kleur.