<commit_before>/** \file
 * Draw fire patterns, derived from the pyramid Fire code.
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <inttypes.h>
#include <errno.h>
#include <unistd.h>
#include "ledscape.h"


// Borrowed by OctoWS2811 rainbow test
static unsigned int
h2rgb(
	unsigned int v1,
	unsigned int v2,
	unsigned int hue
)
{
	if (hue < 60)
		return v1 * 60 + (v2 - v1) * hue;
	if (hue < 180)
		return v2 * 60;
	if (hue < 240)
		return v1 * 60 + (v2 - v1) * (240 - hue);

	return v1 * 60;
}


// Convert HSL (Hue, Saturation, Lightness) to RGB (Red, Green, Blue)
//
//   hue:        0 to 359 - position on the color wheel, 0=red, 60=orange,
//                            120=yellow, 180=green, 240=blue, 300=violet
//
//   saturation: 0 to 100 - how bright or dull the color, 100=full, 0=gray
//
//   lightness:  0 to 100 - how light the color is, 100=white, 50=color, 0=black
//
static uint32_t
hsv2rgb(
	unsigned int hue,
	unsigned int saturation,
	unsigned int lightness
)
{
	unsigned int red, green, blue;
	unsigned int var1, var2;

	if (hue > 359)
		hue = hue % 360;
	if (saturation > 100)
		saturation = 100;
	if (lightness > 100)
		lightness = 100;

	// algorithm from: http://www.easyrgb.com/index.php?X=MATH&H=19#text19
	if (saturation == 0) {
		red = green = blue = lightness * 255 / 100;
	} else {
		if (lightness < 50) {
			var2 = lightness * (100 + saturation);
		} else {
			var2 = ((lightness + saturation) * 100) - (saturation * lightness);
		}
		var1 = lightness * 200 - var2;
		red = h2rgb(var1, var2, (hue < 240) ? hue + 120 : hue - 240) * 255 / 600000;
		green = h2rgb(var1, var2, hue) * 255 / 600000;
		blue = h2rgb(var1, var2, (hue >= 120) ? hue - 120 : hue + 240) * 255 / 600000;
	}
	return (blue << 16) | (green << 8) | red;
}


static const int w = 256;
static const int h = 16;

// This will contain the pixels used to calculate the fire effect
static uint8_t fire[256][16];

  // Flame colors
static uint32_t palette[255];
static float angle;
static uint32_t calc1[256], calc2[256], calc3[256], calc4[256], calc5[256];

static void draw(uint32_t * frame)
{
    memset(frame, 0, w*h*sizeof(*frame));

    angle = angle + 0.05;

    // Randomize the bottom row of the fire buffer
    for (int x = 0; x < w; x++)
    {
      fire[x][h-1] = random() % 190;
    }

    int counter = 0;
    // Do the fire calculations for every pixel, from top to bottom
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        // Add pixel values around current pixel

        fire[x][y] =
          ((fire[calc3[x]][calc2[y]]
          + fire[calc1[x]][calc2[y]]
          + fire[calc4[x]][calc2[y]]
          + fire[calc1[x]][calc5[y]]) << 5) / (128+(abs(x-w/2))/4); // 129;

        // Output everything to screen using our palette colors
        frame[counter] = palette[fire[x][y]];
        //frame[counter] = fire[x][y];

        // Extract the red value using right shift and bit mask 
        // equivalent of red(pgTemp.pixels[x+y*w])
        if ((frame[counter++] >> 8 & 0xFF) == 128) {
          // Only map 3D cube 'lit' pixels onto fire array needed for next frame
          fire[x][y] = 128;
        }
      }
    }
}


static int constrain(
	int x,
	int min,
	int max
)
{
	if (x < min)
		return min;
	if (x > max)
		return max;
	return x;
}

static void
init_pallete(void)
{
    for (int x = 0; x < 255; x++) {
      //Hue goes from 0 to 85: red to yellow
      //Saturation is always the maximum: 255
      //Lightness is 0..255 for x=0..128, and 255 for x=128..255
      palette[x] = hsv2rgb(x/3.5, 100, constrain(x, 0, 70));
    }

    // Precalculate which pixel values to add during animation loop
    // this speeds up the effect by 10fps
    for (int x = 0; x < w; x++) {
      calc1[x] = x % w;
      calc3[x] = (x - 1 + w) % w;
      calc4[x] = (x + 1) % w;
    }

    for (int y = 0; y < h; y++) {
      calc2[y] = (y + 1) % h;
      calc5[y] = (y + 2) % h;
    }
}


int
main(void)
{
	const int num_pixels = 256;
	ledscape_t * const leds = ledscape_init(num_pixels);
	printf("init done\n");
	time_t last_time = time(NULL);
	unsigned last_i = 0;

	unsigned i = 0;
	init_pallete();

	while (1)
	{
		// Alternate frame buffers on each draw command
		const unsigned frame_num = i++ % 2;
		ledscape_frame_t * const frame
			= ledscape_frame(leds, frame_num);

		uint32_t * const p = (void*) frame;

		draw(p);
		ledscape_draw(leds, frame_num);
		usleep(20000);

		// wait for the previous frame to finish;
		//const uint32_t response = ledscape_wait(leds);
		const uint32_t response = 0;
		time_t now = time(NULL);
		if (now != last_time)
		{
			printf("%d fps. starting %d previous %"PRIx32"\n",
				i - last_i, i, response);
			last_i = i;
			last_time = now;
		}<commit_msg>slow it down, tweak pallete and avoid empty first line<commit_after>/** \file
 * Draw fire patterns, derived from the pyramid Fire code.
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <inttypes.h>
#include <errno.h>
#include <unistd.h>
#include "ledscape.h"


// Borrowed by OctoWS2811 rainbow test
static unsigned int
h2rgb(
	unsigned int v1,
	unsigned int v2,
	unsigned int hue
)
{
	if (hue < 60)
		return v1 * 60 + (v2 - v1) * hue;
	if (hue < 180)
		return v2 * 60;
	if (hue < 240)
		return v1 * 60 + (v2 - v1) * (240 - hue);

	return v1 * 60;
}


// Convert HSL (Hue, Saturation, Lightness) to RGB (Red, Green, Blue)
//
//   hue:        0 to 359 - position on the color wheel, 0=red, 60=orange,
//                            120=yellow, 180=green, 240=blue, 300=violet
//
//   saturation: 0 to 100 - how bright or dull the color, 100=full, 0=gray
//
//   lightness:  0 to 100 - how light the color is, 100=white, 50=color, 0=black
//
static uint32_t
hsv2rgb(
	unsigned int hue,
	unsigned int saturation,
	unsigned int lightness
)
{
	unsigned int red, green, blue;
	unsigned int var1, var2;

	if (hue > 359)
		hue = hue % 360;
	if (saturation > 100)
		saturation = 100;
	if (lightness > 100)
		lightness = 100;

	// algorithm from: http://www.easyrgb.com/index.php?X=MATH&H=19#text19
	if (saturation == 0) {
		red = green = blue = lightness * 255 / 100;
	} else {
		if (lightness < 50) {
			var2 = lightness * (100 + saturation);
		} else {
			var2 = ((lightness + saturation) * 100) - (saturation * lightness);
		}
		var1 = lightness * 200 - var2;
		red = h2rgb(var1, var2, (hue < 240) ? hue + 120 : hue - 240) * 255 / 600000;
		green = h2rgb(var1, var2, hue) * 255 / 600000;
		blue = h2rgb(var1, var2, (hue >= 120) ? hue - 120 : hue + 240) * 255 / 600000;
	}
	return (blue << 16) | (green << 8) | red;
}


static const int w = 256;
static const int h = 20;

// This will contain the pixels used to calculate the fire effect
static uint8_t fire[256][32];

  // Flame colors
static uint32_t palette[255];
static float angle;
static uint32_t calc1[256], calc2[256], calc3[256], calc4[256], calc5[256];

static void draw(uint32_t * frame)
{
    memset(frame, 0, w*16*sizeof(*frame));

    angle = angle + 0.05;

    // Randomize the bottom row of the fire buffer
    for (int x = 0; x < w; x++)
    {
      fire[x][h-1] = random() % 190;
    }

    int counter = 0;
    // Do the fire calculations for every pixel, from top to bottom
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        // Add pixel values around current pixel

        fire[x][y] =
          ((fire[calc3[x]][calc2[y]]
          + fire[calc1[x]][calc2[y]]
          + fire[calc4[x]][calc2[y]]
          + fire[calc1[x]][calc5[y]]) << 5) / (128+(abs(x-w/2))/4); // 129;

        // Output everything to screen using our palette colors
	const uint32_t c = palette[fire[x][y]];
        //frame[counter] = fire[x][y];

        // Extract the red value using right shift and bit mask 
        // equivalent of red(pgTemp.pixels[x+y*w])
	// Only map 3D cube 'lit' pixels onto fire array needed for next frame
        if (((c >> 0) & 0xFF) == 128)
          fire[x][y] = 128;

	if (y > 1 && y < 2 + 16)
		frame[counter++] = c;
      }
    }
}


static int constrain(
	int x,
	int min,
	int max
)
{
	if (x < min)
		return min;
	if (x > max)
		return max;
	return x;
}

static void
init_pallete(void)
{
    for (int x = 0; x < 255; x++) {
      //Hue goes from 0 to 85: red to yellow
      //Saturation is always the maximum: 255
      //Lightness is 0..255 for x=0..128, and 255 for x=128..255
      palette[x] = hsv2rgb(x/2, 100, constrain(x, 0, 40));
    }

    // Precalculate which pixel values to add during animation loop
    // this speeds up the effect by 10fps
    for (int x = 0; x < w; x++) {
      calc1[x] = x % w;
      calc3[x] = (x - 1 + w) % w;
      calc4[x] = (x + 1) % w;
    }

    for (int y = 0; y < h; y++) {
      calc2[y] = (y + 1) % h;
      calc5[y] = (y + 2) % h;
    }
}


int
main(void)
{
	const int num_pixels = 256;
	ledscape_t * const leds = ledscape_init(num_pixels);
	printf("init done\n");
	time_t last_time = time(NULL);
	unsigned last_i = 0;

	unsigned i = 0;
	init_pallete();

	while (1)
	{
		// Alternate frame buffers on each draw command
		const unsigned frame_num = i++ % 2;
		ledscape_frame_t * const frame
			= ledscape_frame(leds, frame_num);

		uint32_t * const p = (void*) frame;

		draw(p);
		ledscape_draw(leds, frame_num);
		usleep(30000);

		// wait for the previous frame to finish;
		//const uint32_t response = ledscape_wait(leds);
		const uint32_t response = 0;
		time_t now = time(NULL);
		if (now != last_time)
		{
			printf("%d fps. starting %d previous %"PRIx32"\n",
				i - last_i, i, response);
			last_i = i;
			last_time = now;
		}
{'fraction_non_alphanumeric': 0.14930483502801412, 'fraction_numerical': 0.06816766964100436, 'mean_word_length': 2.5489690721649483, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4448333', 'n_tokens_mistral': 4303, 'n_tokens_neox': 3625, 'n_words': 1171}