// fire width 19 height 7
// testing for segment clock
// Stolen from fire_201 byte by ldir
// Perlin fire
// https://pastebin.com/jSSVSRi6

//Perlin noise fire procedure 
//16x16 rgb led matrix demo
//Yaroslaw Turbin, 22.06.2020 
//https://vk.com/ldirko
//https://www.reddit.com/user/ldirko/
//https://www.reddit.com/r/FastLED/comments/hgu16i/my_fire_effect_implementation_based_on_perlin/
 
//idea in make perlin noise with time offset X and Z coord 
//this automatic scroll fire pattern    
//and distort fire noise.
//then substract Y based coodrd value to shift 
//fire color (not brightness) in palette. 
//this fadeout color from bottom matrix to up.
//this need some palette tweak for good looking fire color
  
#include "FastLED.h"
 
// LEDs pin
#define DATA_PIN 3
 
// LED brightness
#define BRIGHTNESS 255

// Matrix size
#define NUM_ROWS 7
#define NUM_COLS 19
#define NUM_LEDS NUM_ROWS * NUM_COLS

// Define the arrays of leds
CRGB leds[NUM_LEDS+1];

DEFINE_GRADIENT_PALETTE( firepal ) {    // define fire palette
   0,     0,  0,  0,   //black
  32,   255,  0,  0,  // red
  190,  255, 255, 0, //yellow
  255,  255, 255, 255  // white
};
 
CRGBPalette16 myPal = firepal;
 
void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
}
 
 
void loop() {
  int  a = millis();
  for (int i = 0; i < NUM_COLS; i++) {
    for (int j = 0; j < NUM_ROWS; j++) {
      // leds[XY1(i,j)] = ColorFromPalette (myPal, qsub8 (inoise8 (i * 80 , j * 80+ a , a /3), 
      // abs8(j - (NUM_ROWS-1)) * 255 / (NUM_ROWS+2)), BRIGHTNESS);  
      int index = XY(i,j);
      if ( index == NUM_LEDS ) continue;  
    leds[index] = CHSV ((sin8(i*8+a/7)+cos8(j*8+a/4))/2,255,255);
    }                                                              
  }
  FastLED.show();
}
 
byte looktable [] = {
  133, 0, 1, 133, 17, 133, 18, 19, 133, 35, 133, 36, 37, 133, 53, 133, 54, 55, 133,
  11, 133, 133, 2, 133, 29, 133, 133, 20, 133, 47, 133, 133, 38, 133, 65, 133, 133, 56,
  10, 133, 133, 3, 16, 28, 133, 133, 21, 34, 46, 133, 133, 39, 52, 64, 133, 133, 57,
  133, 12, 13, 133, 133, 133, 30, 31, 133, 133, 133, 48, 49, 133, 133, 133, 66, 67, 133,
  9, 133, 133, 4, 15, 27, 133, 133, 22, 33, 45, 133, 133, 40, 51, 63, 133, 133, 58,
  8, 133, 133, 5, 133, 26, 133, 133, 23, 133, 44, 133, 133, 41, 133, 62, 133, 133, 59,
  133, 7, 6, 133, 14, 133, 25, 24, 133, 32, 133, 43, 42, 133, 50, 133, 61, 60, 133
};

uint8_t XY (uint8_t x, uint8_t y) { 
  byte index = looktable [y * NUM_COLS + x];
  return (index);
}