bruh
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
#include "includes.h"
|
||||
|
||||
static const char* control_type[] = {"Voltage", "PWM"};
|
||||
static const char* work_types[] = {"Glowing", "Blinking", "Breath"};
|
||||
|
||||
#define MASK_D 0xff000000
|
||||
#define MASK_R 0x00ff0000
|
||||
#define MASK_G 0x0000ff00
|
||||
#define MASK_B 0x000000ff
|
||||
#define SHIFT_D 24
|
||||
#define SHIFT_R 16
|
||||
#define SHIFT_G 8
|
||||
|
||||
#define MASK_F 0x0800
|
||||
#define MASK_T 0x0600
|
||||
#define MASK_E 0x01ff
|
||||
#define SHIFT_F 11
|
||||
#define SHIFT_T 9
|
||||
|
||||
rgbled* create_led() {
|
||||
rgbled* led = (rgbled*)malloc(sizeof(rgbled));
|
||||
set_Temp(led, 0);
|
||||
set_R(led, 0);
|
||||
set_D(led, 255);
|
||||
set_T(led, 0);
|
||||
set_F(led, 0);
|
||||
return led;
|
||||
}
|
||||
|
||||
rgbled_bad* create_bad_led() {
|
||||
rgbled_bad* led = (rgbled_bad*)malloc(sizeof(rgbled_bad));
|
||||
return led;
|
||||
}
|
||||
|
||||
void set_Temp(rgbled* led, int tempK) {
|
||||
if (tempK < 0 || tempK > 51200) return;
|
||||
tempK /= 100;
|
||||
int red, green, blue;
|
||||
if (tempK <= 66) {
|
||||
green = 99.4708025861 * log(tempK) - 161.1195681661;
|
||||
red = 255;
|
||||
if (tempK <= 19) {
|
||||
blue = 0;
|
||||
} else {
|
||||
blue = 138.5177312231 * log((tempK)-10) - 305.0447927307;
|
||||
}
|
||||
} else {
|
||||
red = 329.698727446 * powf((tempK-60), -0.1332047592);
|
||||
green = 288.1221695283 * powf((tempK - 60), -0.0755148492);
|
||||
blue = 255;
|
||||
}
|
||||
set_R(led, red);
|
||||
set_G(led, green);
|
||||
set_B(led, blue);
|
||||
led->efg = (led->efg & ~MASK_E) | ((tempK << 0) & MASK_E);
|
||||
}
|
||||
|
||||
void set_R(rgbled* led, int red) {
|
||||
if (red < 0 || red > 255) return;
|
||||
led->rgbd = (led->rgbd & ~MASK_R) | ((red << SHIFT_R) & MASK_R);
|
||||
led->efg = (led->efg & ~MASK_E) | ((0) & MASK_E);
|
||||
}
|
||||
|
||||
void set_G(rgbled* led, int green) {
|
||||
if (green < 0 || green > 255) return;
|
||||
led->rgbd = (led->rgbd & ~MASK_G) | ((green << SHIFT_G) & MASK_G);
|
||||
led->efg = (led->efg & ~MASK_E) | ((0) & MASK_E);
|
||||
}
|
||||
|
||||
void set_B(rgbled* led, int blue) {
|
||||
if (blue < 0 || blue > 255) return;
|
||||
led->rgbd = (led->rgbd & ~MASK_B) | ((blue << 0) & MASK_B);
|
||||
led->efg = (led->efg & ~MASK_E) | ((0) & MASK_E);
|
||||
}
|
||||
|
||||
void set_D(rgbled* led, int bright) {
|
||||
if (bright < 0 || bright > 255) return;
|
||||
led->rgbd = (led->rgbd & ~MASK_D) | ((bright << SHIFT_D) & MASK_D);
|
||||
}
|
||||
|
||||
void set_T(rgbled* led, int work) {
|
||||
if (work < 0 || work > 3) return;
|
||||
led->efg = (led->efg & ~MASK_T) | ((work << SHIFT_T) & MASK_T);
|
||||
}
|
||||
|
||||
void set_F(rgbled* led, int type) {
|
||||
if (type < 0 || type > 1) return;
|
||||
led->efg = (led->efg & ~MASK_F) | ((type << SHIFT_F) & MASK_F);
|
||||
}
|
||||
|
||||
int get_Temp(rgbled* led) {
|
||||
return (led->efg & MASK_E)*100;
|
||||
}
|
||||
|
||||
int get_R(rgbled* led) {
|
||||
return (led->rgbd & MASK_R) >> SHIFT_R;
|
||||
}
|
||||
|
||||
int get_G(rgbled* led) {
|
||||
return (led->rgbd & MASK_G) >> SHIFT_G;
|
||||
}
|
||||
|
||||
int get_B(rgbled* led) {
|
||||
return (led->rgbd & MASK_B);
|
||||
}
|
||||
|
||||
int get_D(rgbled* led) {
|
||||
return (led->rgbd & MASK_D) >> SHIFT_D;
|
||||
}
|
||||
|
||||
int get_F(rgbled* led) {
|
||||
return (led->efg & MASK_F) >> SHIFT_F;
|
||||
}
|
||||
|
||||
int get_T(rgbled* led) {
|
||||
return (led->efg & MASK_T) >> SHIFT_T;
|
||||
}
|
||||
|
||||
void hex_color(int num) {
|
||||
printf("%c%c", ((num/16) > 9) ? 'a'+(num / 16)-10 : '0'+(num/16), ((num%16) > 9) ? 'a'+(num%16)-10 : '0'+(num%16));
|
||||
}
|
||||
|
||||
void print_colorHEX(rgbled* led) {
|
||||
int r = get_R(led)*(get_D(led)/255);
|
||||
int g = get_G(led)*(get_D(led)/255);
|
||||
int b = get_B(led)*(get_D(led)/255);
|
||||
//printf("(%d, %d, %d) ", r, g, b);
|
||||
putchar('#');
|
||||
hex_color(r);
|
||||
hex_color(g);
|
||||
hex_color(b);
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "includes.h"
|
||||
|
||||
tdQueue* queue_create() {
|
||||
tdQueue* queue = (tdQueue*)malloc(sizeof(tdQueue));
|
||||
queue->data = NULL;
|
||||
queue->size = 0;
|
||||
queue->capacity = 0;
|
||||
return queue;
|
||||
}
|
||||
|
||||
void queue_decontruct(tdQueue* queue)
|
||||
{
|
||||
int i = 0;
|
||||
while ((i < queue->size) && (queue->data[i] != NULL)) {
|
||||
deconstruct_matrix2d(queue->data[i]);
|
||||
i++;
|
||||
}
|
||||
free(queue);
|
||||
}
|
||||
|
||||
bool queue_is_empty(tdQueue* queue)
|
||||
{
|
||||
return queue->size == 0;
|
||||
}
|
||||
|
||||
int queue_size(tdQueue* queue)
|
||||
{
|
||||
return queue->size;
|
||||
}
|
||||
|
||||
void queue_enqueue(tdQueue* queue, matrix2d* data)
|
||||
{
|
||||
if (queue->capacity == queue->size) {
|
||||
if (queue->capacity) {
|
||||
queue->capacity = ceil(queue->capacity*sqrt(2));
|
||||
queue->data = (matrix2d**)realloc(queue->data, sizeof(matrix2d*)*(queue->capacity));
|
||||
} else {
|
||||
queue->capacity = ceil(sqrt(2));
|
||||
queue->data = (matrix2d**)malloc(sizeof(matrix2d*)*(queue->capacity));
|
||||
}
|
||||
}
|
||||
queue->data[queue->size++] = data;
|
||||
}
|
||||
|
||||
matrix2d* queue_dequeue(tdQueue* queue)
|
||||
{
|
||||
if (queue->data == NULL) return NULL;
|
||||
matrix2d* data = queue->data[0];
|
||||
for (int i = 1; i < queue->size; i++) {
|
||||
queue->data[i-1] = queue->data[i];
|
||||
}
|
||||
queue->size -= 1;
|
||||
return data;
|
||||
}
|
||||
|
||||
matrix2d* queue_peek(tdQueue* queue)
|
||||
{
|
||||
if (queue->data == NULL) return NULL;
|
||||
matrix2d* data = queue->data[0];
|
||||
return data;
|
||||
}
|
||||
|
||||
void queue_clear(tdQueue* queue)
|
||||
{
|
||||
int i = 0;
|
||||
while (i < queue->capacity && queue->data[i] != NULL)
|
||||
deconstruct_matrix2d((matrix2d*) (queue->data)[i++]);
|
||||
queue->size = 0;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "includes.h"
|
||||
|
||||
int main(void) {
|
||||
tdQueue* queue = queue_create();
|
||||
printf("queue is empty? %i\n", queue_is_empty(queue));
|
||||
printf("ptr - %p %p\n", queue_peek(queue), queue_dequeue(queue));
|
||||
matrix2d* mat = create_matrix2d(2, 2);
|
||||
int temp = -1;
|
||||
rgbled* led = create_led();
|
||||
for (int y = 0; y < mat->y; y++)
|
||||
for (int x = 0; x < mat->x; x++) {
|
||||
led = create_led();
|
||||
while(scanf("%d", &temp) != 1 || temp < 0 || temp >= 511000) continue;
|
||||
set_Temp(led, temp);
|
||||
set_elem_matrix2d(mat, (void*)led, x, y);
|
||||
temp = -1;
|
||||
}
|
||||
queue_enqueue(queue, mat);
|
||||
printf("queue is empty? %i\n", queue_is_empty(queue));
|
||||
printf("Size queue? %i (%i)\n", queue_size(queue), queue->capacity);
|
||||
print_matrix2d(queue_peek(queue));
|
||||
for (int i = 0; i < 500000; i++) {
|
||||
mat = create_matrix2d(10, 10);
|
||||
fill_rand_matrix2d(mat);
|
||||
queue_enqueue(queue, mat);
|
||||
}
|
||||
printf("Size queue? %i (%i)\n", queue_size(queue), queue->capacity);
|
||||
mat = queue_dequeue(queue);
|
||||
print_matrix2d(mat);
|
||||
deconstruct_matrix2d(mat);
|
||||
printf("Size queue? %i (%i)\n", queue_size(queue), queue->capacity);
|
||||
tdQueue* queue_b = queue_create();
|
||||
for (int i = 0; i < 500000; i++) {
|
||||
mat = create_matrix2d(10, 10);
|
||||
fill_rand_matrix2d(mat);
|
||||
queue_enqueue(queue_b, mat);
|
||||
}
|
||||
printf("Size queue_b? %i (%i)\n", queue_size(queue_b), queue_b->capacity);
|
||||
queue_decontruct(queue);
|
||||
queue_decontruct(queue_b);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
#include "includes.h"
|
||||
|
||||
int getrand(int min, int max)
|
||||
{
|
||||
return (int)rand() / (RAND_MAX + 1.0) * (max - min) + min;
|
||||
}
|
||||
|
||||
matrix2d* create_matrix2d(unsigned int x, unsigned int y)
|
||||
{
|
||||
matrix2d* mat = (matrix2d*)malloc(sizeof(matrix2d));
|
||||
mat->ptr = (void***)malloc(sizeof(void**) * x);
|
||||
for (int i = 0; i < x; i++)
|
||||
mat->ptr[i] = (void**)malloc(sizeof(void*) * y);
|
||||
for (int i = 0; i < x; i++)
|
||||
for (int j = 0; j < y; j++)
|
||||
mat->ptr[i][j] = (void*)create_led();
|
||||
*((unsigned int*)&(mat->x)) = x;
|
||||
*((unsigned int*)&(mat->y)) = y;
|
||||
return mat;
|
||||
}
|
||||
|
||||
void deconstruct_matrix2d( matrix2d* mat)
|
||||
{
|
||||
for (int i = 0; i < mat->x; i++){
|
||||
for (int j = 0; j < mat->y; j++)
|
||||
free(mat->ptr[i][j]);
|
||||
free(mat->ptr[i]);
|
||||
}
|
||||
free(mat->ptr);
|
||||
free(mat);
|
||||
}
|
||||
|
||||
matrix2d* get_row_matrix2d(matrix2d* mat, int y)
|
||||
{
|
||||
if (y >= mat->y)
|
||||
return NULL;
|
||||
matrix2d* res = create_matrix2d( mat->x, 1);
|
||||
for (int x = 0; x < mat->x; x++)
|
||||
memcpy(res->ptr[x][0], mat->ptr[x][y], sizeof(rgbled));
|
||||
return res;
|
||||
}
|
||||
|
||||
matrix2d* get_column_matrix2d(matrix2d* mat, int x)
|
||||
{
|
||||
if (x >= mat->x)
|
||||
return NULL;
|
||||
matrix2d* res = create_matrix2d(1, mat->y);
|
||||
for (int y = 0; y < mat->y; y++)
|
||||
memcpy(res->ptr[0][y], mat->ptr[x][y], sizeof(rgbled));
|
||||
return res;
|
||||
}
|
||||
|
||||
void print_matrix2d(matrix2d* mat)
|
||||
{
|
||||
for (int y = 0; y < mat->y; y++) {
|
||||
for (int x = 0; x < mat->x; x++) {
|
||||
print_colorHEX((rgbled*)mat->ptr[x][y]);
|
||||
putchar(' ');
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
|
||||
void fill_rand_matrix2d(matrix2d* mat)
|
||||
{
|
||||
for (int y = 0; y < mat->y; y++)
|
||||
for (int x = 0; x < mat->x; x++) {
|
||||
set_R(mat->ptr[x][y], getrand(0, 255));
|
||||
set_G(mat->ptr[x][y], getrand(0, 255));
|
||||
set_B(mat->ptr[x][y], getrand(0, 255));
|
||||
}
|
||||
}
|
||||
|
||||
matrix2d* transpond_mtrx(matrix2d* mat)
|
||||
{
|
||||
matrix2d* res = create_matrix2d(mat->y, mat->x);
|
||||
for (int y = 0; y < mat->y; y++)
|
||||
for (int x = 0; x < mat->x; x++)
|
||||
memcpy(res->ptr[y][x], mat->ptr[x][y], sizeof(rgbled));
|
||||
return res;
|
||||
}
|
||||
|
||||
bool equal_mtrx(matrix2d* matA, matrix2d* matB)
|
||||
{
|
||||
if (matA->x != matB->x || matA->y != matB->y)
|
||||
return false;
|
||||
for (int x = 0; x < matA->x; x++)
|
||||
for (int y = 0; y < matA->y; y++) {
|
||||
rgbled* ledA = (rgbled*)matA->ptr[x][y];
|
||||
rgbled* ledB = (rgbled*)matB->ptr[x][y];
|
||||
if ((ledA->rgbd != ledB->rgbd) || (ledA->efg != ledB->efg))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool not_equal_mtrx(matrix2d* matA, matrix2d* matB)
|
||||
{
|
||||
if (matA->x != matB->x || matA->y != matB->y)
|
||||
return true;
|
||||
for (int x = 0; x < matA->x; x++)
|
||||
for (int y = 0; y < matA->y; y++) {
|
||||
rgbled* ledA = (rgbled*)matA->ptr[x][y];
|
||||
rgbled* ledB = (rgbled*)matB->ptr[x][y];
|
||||
if ((ledA->rgbd == ledB->rgbd) && (ledA->efg == ledB->efg))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void set_elem_matrix2d(matrix2d* mat, void* elem, unsigned int x, unsigned int y)
|
||||
{
|
||||
free(mat->ptr[x][y]);
|
||||
mat->ptr[x][y] = elem;
|
||||
}
|
||||
Reference in New Issue
Block a user