#include <FastLED.h>

#define LED_PIN     3
#define NUM_LEDS    241
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB

CRGB leds[NUM_LEDS];
const uint8_t led_count[] = {60, 48, 40, 32, 24, 16, 12, 8, 1};

#define NUM_RINGS (sizeof(led_count) / sizeof(led_count[0]))
#define FIRE_WIDTH 60
#define FIRE_HEIGHT NUM_RINGS
static uint8_t heat[FIRE_WIDTH][FIRE_HEIGHT];


void setup() {
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
}


void loop() {
  Fire2012(random8() / 2 + 128);

  CRGB *led = leds;
  uint8_t ring = 0;
  // map the modified Fire2012() onto the rings
  do {
    uint8_t count = led_count[ring];
    uint16_t td = FIRE_WIDTH * 255 / count;
    uint16_t t = 0;
    for (uint8_t i = 0; i < count ; i++) {
      uint8_t h = heat[t >> 8][FIRE_HEIGHT - 1 - ring];
      if (ring >= NUM_RINGS - 2)
        h = qadd8(h, 128);
      *led++ = HeatColor(h);
      t += td;
    }
  } while (++ring < NUM_RINGS);

  FastLED.show();
}

void Fire2012(uint8_t activity) {
  for (uint8_t h = 0; h < FIRE_WIDTH; h++) {
    // Step 1.  Cool down every cell a little
    for( uint8_t i = 0; i < FIRE_HEIGHT; i++) {
      heat[h][i] = qsub8( heat[h][i],  random8(33));
    }

    // Step 2.  Heat from each cell drifts 'up' and diffuses a little
    for( uint8_t k = FIRE_HEIGHT - 1; k >= 1; k--) {
      uint8_t hleft = (h + FIRE_WIDTH - 1) % FIRE_WIDTH;
      uint8_t hright = (h+1) % FIRE_WIDTH;
      heat[h][k] = (heat[h][k]
                  + heat[hleft][k - 1]
                  + heat[h][k - 1]
                  + heat[hright][k - 1] ) / 4;
    }

    if( random8() < activity ) {
      heat[h][0] = qadd8( heat[h][0], random8(42));
    }

  }
}