<commit_before>int gPowerState = 1;
float gHue = 1;
float gSaturation = 1;
float gBrightness = 1;

int redPin = A0;    // RED pin of the LED to PWM pin **A0**
int greenPin = D0;  // GREEN pin of the LED to PWM pin **D0**
int bluePin = D1;   // BLUE pin of the LED to PWM pin **D1**

void setup() {
   pinMode(redPin, OUTPUT);
   pinMode(greenPin, OUTPUT);
   pinMode(bluePin, OUTPUT);

   //Exposed internal functions as cloud API
   Particle.function("powerState", powerState);
   Particle.function("hue", hue);
   Particle.function("saturation", saturation);
   Particle.function("brightness", brightness);
}

void loop() {
    /* Do nothing */
}

/* Cloud methods */

int powerState(String value) {
    int result = 1;
    if (value.length() > 0) {
        gPowerState = (value.toInt() == 1);
        result = gPowerState;
    }
    updateLED();
    return result;
}

int hue(String value) {
    int result = -1;
    if (value.length() > 0) {
        float hueFloat = value.toFloat();
        gHue = hueFloat / 360.0;
        result = value.toInt();
    }
    updateLED();
    return result;
}

int saturation(String value) {
    int result = -1;
    if (value.length() > 0) {
        float saturationFloat = value.toFloat();
        gSaturation = saturationFloat / 100.0;
        result = value.toInt();
    }
    updateLED();
    return result;
}

int brightness(String value) {
    int result = -1;

    if (value.length() > 0) {
        float brightnessFloat = value.toFloat();
        gBrightness = value.toFloat() / 100.0;
        result = value.toInt();
    }

    updateLED();

    return result;
}

/* LED color manipulation methods */

void hslToRgb(float h, float s, float l){
    float tempR, tempG, tempB;
    int r, g, b;

    if(l == 0){
        ledControl(0,0,0);
    }
    if(s == 0){
        r = g = b = l; // achromatic
    }else{
        float q = l < 0.5 ? l * (1.0 + s) : l + s - l * s;
        float p = 2.0 * l - q;
        tempR = hue2rgb(p, q, h + 1.0/3.0);
        tempG = hue2rgb(p, q, h);
        tempB = hue2rgb(p, q, h - 1.0/3.0);
    }
    
    r = round(tempR * 255);
    g = round(tempG * 255);
    b = round(tempB * 255);

    ledControl(r,g,b);
}

float hue2rgb(float p, float q, float t){
    if(t < 0.0){ 
        t += 1.0;
    }
    if(t > 1.0){
        t -= 1.0;
    }
    if(t < 1.0/6.0){
        return p + (q - p) * 6.0 * t;
    }
    if(t < 1.0/2.0) {
        return q;
    }
    if(t < 2.0/3.0) {
        return p + (q - p) * (2.0/3.0 - t) * 6.0;
    }
    return p;
}

void ledControl(int redValue, int greenValue, int blueValue)
{
   // write the mixed color
   analogWrite(redPin, redValue);
   analogWrite(greenPin, greenValue);
   analogWrite(bluePin, blueValue);
}

// Only update if turned on
void updateLED() {
    if (gPowerState) {
        hslToRgb(gHue, gSaturation, gBrightness);
    } else {
        ledControl(0,0,0);
    }<commit_msg>Update hue-light-simulator.ino<commit_after>#include "math.h"

int gPowerState = 1;
float gHue = 1;
float gSaturation = 1;
float gBrightness = 1;

int redPin = A0;    // RED pin of the LED to PWM pin **A0**
int greenPin = D0;  // GREEN pin of the LED to PWM pin **D0**
int bluePin = D1;   // BLUE pin of the LED to PWM pin **D1**

void setup() {
   pinMode(redPin, OUTPUT);
   pinMode(greenPin, OUTPUT);
   pinMode(bluePin, OUTPUT);

   //Exposed internal functions as cloud API
   Particle.function("powerState", powerState);
   Particle.function("hue", hue);
   Particle.function("saturation", saturation);
   Particle.function("brightness", brightness);
}

void loop() {
    /* Do nothing */
}

/* Cloud methods */

int powerState(String value) {
    int result = 1;
    if (value.length() > 0) {
        gPowerState = (value.toInt() == 1);
        result = gPowerState;
    }
    updateLED();
    return result;
}

int hue(String value) {
    delay(200);
    int result = -1;
    if (value.length() > 0) {
        float hueFloat = value.toFloat();
        gHue = hueFloat;// / 360.0;
        result = value.toInt();
    }
    updateLED();
    return result;
}

int saturation(String value) {
    delay(200);
    int result = -1;
    if (value.length() > 0) {
        float saturationFloat = value.toFloat();
        gSaturation = saturationFloat / 100.0;
        result = value.toInt();
    }
    updateLED();
    return result;
}

int brightness(String value) {
    delay(200);
    int result = -1;

    if (value.length() > 0) {
        float brightnessFloat = value.toFloat();
        gBrightness = value.toFloat() / 100.0;
        result = value.toInt();
    }

    updateLED();

    return result;
}

/* LED color manipulation methods */

void hsvToRgb(float h, float s, float v) {
	float r, g, b;
	int i;
	float f, p, q, t;

	if(s == 0) {
		// Achromatic (grey)
		r = g = b = v;
		ledControl(round(r * 255), round(g * 255), round(b * 255));
	}

	h /= 60; // sector 0 to 5
	
	i = floor(h);
	f = h - i; // factorial part of h
	p = v * (1 - s);
	q = v * (1 - s * f);
	t = v * (1 - s * (1 - f));
 
	switch(i) {
		case 0:
			r = v;
			g = t;
			b = p;
			break;
 
		case 1:
			r = q;
			g = v;
			b = p;
			break;
 
		case 2:
			r = p;
			g = v;
			b = t;
			break;
 
		case 3:
			r = p;
			g = q;
			b = v;
			break;
 
		case 4:
			r = t;
			g = p;
			b = v;
			break;
 
		default: // case 5:
			r = v;
			g = p;
			b = q;
	}
 
	ledControl(round(r * 255), round(g * 255), round(b * 255));
}

void ledControl(int redValue, int greenValue, int blueValue)
{
   // write the mixed color
   analogWrite(redPin, redValue);
   analogWrite(greenPin, greenValue);
   analogWrite(bluePin, blueValue);
}

// Only update if turned on
void updateLED() {
    if (gPowerState) {
        hsvToRgb(gHue, gSaturation, gBrightness);
    } else {
        ledControl(0,0,0);
    }
{'fraction_non_alphanumeric': 0.15272474835126693, 'fraction_numerical': 0.02950364456785838, 'mean_word_length': 2.1371801850843766, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5153998', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2158, 'n_words': 609}