//real candle simulation 
//based on this post http://petemills.blogspot.com/2012/02/attiny-candle.html

#include <FastLED.h>

#define DATA_PIN    0                //set your leds datapin   change to 32 for m5 atom lite
#define LED_TYPE    WS2812B          //leds type
#define COLOR_ORDER GRB              //color order of leds

#define MAX_POWER_MILLIAMPS 700      //write here your power in milliamps. default i set 800 mA for safet

#define NUM_LEDS 1                   
byte brigtness = 255;

CRGB leds [NUM_LEDS];

const byte candleRaw [] PROGMEM =  {

0x10, 0x00, 0x00, 0x00, 0x77, 0x7B, 0x7D, 0x7B, 0x78, 0xBA, 0x95, 0x53, 0x5E, 0x3E, 0x3E, 0x43, 0x52, 0x35, 0x3E, 0x75, 0x95, 
0x10, 0x00, 0x10, 0x00, 0x4B, 0x65, 0x7B, 0x52, 0x63, 0x58, 0x6B, 0x55, 0x62, 0x77, 0x72, 0x87, 0x85, 0x8C, 0x5D, 0x7A, 0x2E, 
0x10, 0x00, 0x20, 0x00, 0x53, 0x5D, 0x50, 0x62, 0x55, 0x6F, 0x67, 0x58, 0x78, 0x4E, 0x55, 0x95, 0x6B, 0x6D, 0x7D, 0x73, 0x73, 
0x10, 0x00, 0x30, 0x00, 0x7D, 0x5B, 0x6B, 0x68, 0x6A, 0x6A, 0x60, 0x6B, 0x77, 0x77, 0x98, 0x7A, 0x87, 0x60, 0x5B, 0x6B, 0xC9, 
0x10, 0x00, 0x40, 0x00, 0x53, 0x4A, 0x53, 0x68, 0x45, 0x3B, 0x65, 0x67, 0x9C, 0x60, 0x67, 0x53, 0x73, 0x75, 0x63, 0x8A, 0x81, 
0x10, 0x00, 0x50, 0x00, 0x7F, 0x83, 0x88, 0x80, 0x63, 0x58, 0x58, 0x6B, 0x7A, 0x78, 0x7B, 0x83, 0x8A, 0x87, 0x8A, 0x85, 0x08, 
0x10, 0x00, 0x60, 0x00, 0x83, 0x88, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8C, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x88, 0x83, 0x7F, 0x0B, 
0x10, 0x00, 0x70, 0x00, 0x7D, 0x7B, 0x7A, 0x78, 0x77, 0x75, 0x70, 0x70, 0x72, 0x70, 0x70, 0x4D, 0x41, 0x6D, 0x68, 0x60, 0xB5, 
0x10, 0x00, 0x80, 0x00, 0x35, 0x35, 0x3D, 0x3B, 0x41, 0x45, 0x52, 0x5E, 0x41, 0x53, 0x5D, 0x60, 0x65, 0x6A, 0x50, 0x48, 0xA0, 
0x10, 0x00, 0x90, 0x00, 0x4B, 0x4E, 0x35, 0x35, 0x31, 0x33, 0x33, 0x36, 0x3B, 0x40, 0x50, 0x4E, 0x52, 0x5D, 0x60, 0x53, 0x15, 
0x10, 0x00, 0xA0, 0x00, 0x56, 0x4B, 0x35, 0x2D, 0x2E, 0x2E, 0x35, 0x38, 0x38, 0x39, 0x3B, 0x38, 0x31, 0x58, 0x40, 0x60, 0x77, 
0x10, 0x00, 0xB0, 0x00, 0x4D, 0x50, 0x5A, 0x5D, 0x43, 0x40, 0x53, 0x58, 0x5A, 0x55, 0x4E, 0x31, 0x31, 0x2B, 0x2E, 0x33, 0xD3, 
0x10, 0x00, 0xC0, 0x00, 0x31, 0x36, 0x35, 0x36, 0x38, 0x39, 0x39, 0x38, 0x40, 0x4A, 0x41, 0x3B, 0x50, 0x62, 0x40, 0x36, 0x4E, 
0x10, 0x00, 0xD0, 0x00, 0x29, 0x2D, 0x45, 0x5E, 0x5D, 0x52, 0x3E, 0x33, 0x3B, 0x43, 0x35, 0x45, 0x38, 0x35, 0x31, 0x33, 0x3E, 
0x10, 0x00, 0xE0, 0x00, 0x36, 0x36, 0x39, 0x36, 0x38, 0x3B, 0x41, 0x36, 0x36, 0x30, 0x39, 0x33, 0x2B, 0x29, 0x33, 0x5A, 0x98, 
0x10, 0x00, 0xF0, 0x00, 0x63, 0x56, 0x41, 0x3D, 0x50, 0x52, 0x55, 0x60, 0x65, 0x55, 0x3B, 0x30, 0x2E, 0x30, 0x3B, 0x4E, 0x66, 
0x10, 0x01, 0x00, 0x00, 0x36, 0x2E, 0x2B, 0x3B, 0x39, 0x3D, 0x4A, 0x50, 0x3D, 0x45, 0x58, 0x4E, 0x4B, 0x4E, 0x4A, 0x45, 0xC5, 
0x10, 0x01, 0x10, 0x00, 0x58, 0x4B, 0x55, 0x5D, 0x5B, 0x56, 0x58, 0x5E, 0x60, 0x77, 0x5E, 0x38, 0x5A, 0x52, 0x46, 0x4B, 0x79, 
0x10, 0x01, 0x20, 0x00, 0x50, 0x4A, 0x4A, 0x35, 0x4E, 0x41, 0x2E, 0x36, 0x36, 0x38, 0x52, 0x4B, 0x46, 0x3B, 0x33, 0x40, 0xC4, 
0x10, 0x01, 0x30, 0x00, 0x4E, 0x60, 0x5A, 0x50, 0x4D, 0x43, 0x4A, 0x50, 0x4B, 0x48, 0x40, 0x3D, 0x40, 0x46, 0x52, 0x5B, 0xFA, 
0x10, 0x01, 0x40, 0x00, 0x62, 0x63, 0x63, 0x5B, 0x52, 0x46, 0x5B, 0x43, 0x55, 0x45, 0x26, 0x35, 0x3B, 0x5B, 0x43, 0x4D, 0xDB, 
0x10, 0x01, 0x50, 0x00, 0x40, 0x56, 0x58, 0x5A, 0x5D, 0x50, 0x46, 0x45, 0x45, 0x41, 0x3B, 0x43, 0x72, 0x87, 0x90, 0x8A, 0x08, 
0x10, 0x01, 0x60, 0x00, 0x8F, 0x97, 0x9D, 0x95, 0x73, 0x65, 0x6B, 0x4D, 0x46, 0x45, 0x55, 0x55, 0x41, 0x56, 0x55, 0x55, 0x31, 
0x10, 0x01, 0x70, 0x00, 0x56, 0x5A, 0x5A, 0x5B, 0x5E, 0x56, 0x62, 0x55, 0x65, 0x58, 0x5A, 0x62, 0x68, 0x6D, 0x6D, 0x6B, 0x89, 
0x10, 0x01, 0x80, 0x00, 0x68, 0x6A, 0x6F, 0x65, 0x6D, 0x31, 0x6F, 0x55, 0x48, 0x50, 0x55, 0x67, 0x5A, 0x41, 0x55, 0x5E, 0xC5, 
0x10, 0x01, 0x90, 0x00, 0x60, 0x65, 0x68, 0x68, 0x63, 0x60, 0x6A, 0x60, 0x67, 0x6A, 0x7F, 0x83, 0x8C, 0x87, 0x88, 0x92, 0x3D, 
0x10, 0x01, 0xA0, 0x00, 0x8D, 0x8F, 0x88, 0x8C, 0x8C, 0x85, 0x82, 0x6A, 0x4E, 0x35, 0x23, 0x11, 0x19, 0x43, 0x3B, 0x41, 0x93, 
0x10, 0x01, 0xB0, 0x00, 0x67, 0x4A, 0x4A, 0x3B, 0x2E, 0x30, 0x45, 0x41, 0x4A, 0x58, 0x48, 0x70, 0x5B, 0x6D, 0x72, 0x62, 0x2F, 
0x10, 0x01, 0xC0, 0x00, 0x75, 0x67, 0x56, 0x5A, 0x5E, 0x55, 0x4D, 0x77, 0x53, 0x2D, 0x36, 0x41, 0x5D, 0x55, 0x40, 0x40, 0x03, 
0x10, 0x01, 0xD0, 0x00, 0x40, 0x40, 0x40, 0x3E, 0x41, 0x5E, 0x82, 0x92, 0x88, 0x88, 0x90, 0x94, 0x88, 0x85, 0x7B, 0x63, 0x4F, 
0x10, 0x01, 0xE0, 0x00, 0x55, 0x53, 0x56, 0x55, 0x50, 0x53, 0x55, 0x03, 0x34, 0x01, 0x3A, 0x7E, 0xFF, 0x01, 0x60, 0x3E, 0x36, 
0x10, 0x01, 0xF0, 0x00, 0x3E, 0x28, 0x01, 0x8E, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x16, 
0x00, 0x00, 0x00, 0x01, 0xFF
};

const uint8_t sintable[256] PROGMEM = {
 0,   0,   0,   0,   1,   1,   1,   2,   2,   3,   4,   5,   6,   6,   8,
 9,   10,  11,  12,  14,  15,  17,  18,  20,  22,  23,  25,  27,  29,  31,
 33,  35,  38,  40,  42,  45,  47,  49,  52,  54,  57,  60,  62,  65,  68,
 71,  73,  76,  79,  82,  85,  88,  91,  94,  97,  100, 103, 106, 109, 113,
 116, 119, 122, 125, 128, 131, 135, 138, 141, 144, 147, 150, 153, 156, 159,
 162, 165, 168, 171, 174, 177, 180, 183, 186, 189, 191, 194, 197, 199, 202,
 204, 207, 209, 212, 214, 216, 218, 221, 223, 225, 227, 229, 231, 232, 234,
 236, 238, 239, 241, 242, 243, 245, 246, 247, 248, 249, 250, 251, 252, 252,
 253, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 253,
 253, 252, 252, 251, 250, 249, 248, 247, 246, 245, 243, 242, 241, 239, 238,
 236, 234, 232, 231, 229, 227, 225, 223, 221, 218, 216, 214, 212, 209, 207,
 204, 202, 199, 197, 194, 191, 189, 186, 183, 180, 177, 174, 171, 168, 165,
 162, 159, 156, 153, 150, 147, 144, 141, 138, 135, 131, 128, 125, 122, 119,
 116, 113, 109, 106, 103, 100, 97,  94,  91,  88,  85,  82,  79,  76,  73,
 71,  68,  65,  62,  60,  57,  54,  52,  49,  47,  45,  42,  40,  38,  35,
 33,  31,  29,  27,  25,  23,  22,  20,  18,  17,  15,  14,  12,  11,  10,
 9,   8,   6,   6,   5,   4,   3,   2,   2,   1,   1,   1,   0,   0,   0, 0
};

const byte stembergGradient [] PROGMEM = {
0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
};

uint16_t masslen = sizeof(candleRaw);
uint16_t index = 0;

int main() {
 init();
 FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
 FastLED.setBrightness(brigtness);
  while (true) {
    // byte randomVal = pgm_read_byte (candleRaw+(index++)%masslen);
    // byte randomVal = getRandom();
    // byte randomVal = lcgRandom();  
    // byte randomVal = random_two_shifts();

    // leds [0] = CHSV (0, 255, randomVal);
    // FastLED.delay(60);    
    // if (random8(100)>10) leds [0].setHue(random8(15)); else leds [0] = 0;
    // FastLED.show();
    stembergOn();
    FastLED.show();
    delay(20);
    // delay(random8(30,100));
  }
}

void stembergOn (){
  static byte index= 0;
    byte massindex = pgm_read_byte(sintable+index/2);
    leds[0] = CHSV (0, 255, pgm_read_byte (stembergGradient+massindex));  // 8 6 5 2  4 
    if (index!=255) index++;
   

}

uint8_t getRandom(void) {
 const uint8_t randomvals[] PROGMEM = {
    234,191,103,250,144,74,39,34,215,128,9,122,144,74,137,105,123,218,158,175,205,
    118,149,13,98,7,173,179,194,97,115,110,213,80,220,142,102,102,36,152,90,135,
    105,176,173,49,6,197,48,140,176,122,4,53,83,216,212,202,170,180,214,53,161,
    225,129,185,106,22,12,190,97,158,170,92,160,194,134,169,98,246,128,195,24,
    198,165,156,77,126,113,136,58,156,196,136,41,246,164,84,138,171,184,42,214,
    203,128,89,39,198,85,140,148,149,36,215,78,170,234,131,124,152,239,154,214,
    130,194,49,3,69,248,120,179,101,163,131,124,184,148,213,118,213,81,177,149,
    58,213,33,201,63,10,195,215,190,7,86,245,128,9,8,40,102,51,125,94,92,5,159,
    75,253,158,40,4,6,178,241,92,124,73,248,1,157,61,50,86,136,113,22,16,171,209,
    230,144,240,14,188,2,167,22,88,57,50,86,171,73,114,175,34,226,245,57,180,111,
    220,186,170,242,141,229,49,158,30,82,161,49,124,65,139,24,95,14,133,65,238,
    116,180,190,49,130,30,30,59,93,173,139,19,187,2,163,102,26,255,23,239,196,19,
    6,162
  };

  static uint8_t random1, random2;
  random1++;
  if (random1 == 0) {
    random2 += 0x41;
  }
  return pgm_read_byte(randomvals + random1) ^ random2;
}


uint8_t lcgRandom (){   //linear congruential generator
  static byte randomNum;
  randomNum = (randomNum*5) + 129;
  return (randomNum); 
}


uint32_t random_two_shifts() {  //found this in https://cdn.hackaday.io/files/10721458687264/bh-badge-animate.c 
  static uint32_t r_z = 362436069, r_w = 521288629;

	r_z = 36969 * (r_z & 65535) + (r_z >> 16);
  r_w = 18000 * (r_w & 65535) + (r_w >> 16);

  return (r_z << 16) + r_w;
}
ATTINY8520PU