heartbeat

Simple server monitor system using encrypted messages over udp
git clone https://noulin.net/git/heartbeat.git
Log | Files | Refs | README

plasma.c (7086B)


      1 #! /usr/bin/env sheepy
      2 /* or direct path to sheepy: #! /usr/local/bin/sheepy */
      3 
      4 /* Libsheepy documentation: https://spartatek.se/libsheepy/ */
      5 #include "libsheepyObject.h"
      6 #include "termbox.h"
      7 #include <stdarg.h>
      8 
      9 #include "math.h"
     10 
     11 int argc; char **argv;
     12 
     13 /* enable/disable logging */
     14 /* #undef pLog */
     15 /* #define pLog(...) */
     16 
     17 u32 plasma[2000][2000];
     18 u32 palette[256];
     19 int paletteShift;
     20 bool quit = no;
     21 
     22 void print_tb(const char *str, int x, int y, uint16_t fg, uint16_t bg)
     23 {
     24   while (*str) {
     25     uint32_t uni;
     26     str += tb_utf8_char_to_unicode(&uni, str);
     27     tb_change_cell(x, y, uni, fg, bg);
     28     x++;
     29   }
     30 }
     31 
     32 void printf_tb(int x, int y, uint16_t fg, uint16_t bg, const char *fmt, ...)
     33 {
     34   char buf[4096];
     35   va_list vl;
     36   va_start(vl, fmt);
     37   vsnprintf(buf, sizeof(buf), fmt, vl);
     38   va_end(vl);
     39   print_tb(buf, x, y, fg, bg);
     40 }
     41 
     42 u32 HSVtoRGB(float H, float S,float V){
     43 	if(H>360 || H<0 || S>100 || S<0 || V>100 || V<0){
     44 		logE("The givem HSV values are not in valid range");
     45 		ret 0;
     46 	}
     47 	float s = S/100;
     48 	float v = V/100;
     49 	float C = s*v;
     50 	float X = C*(1-fabsf(fmodf(H/60.0, 2)-1));
     51 	float m = v-C;
     52 	float r,g,b;
     53 	if(H >= 0 && H < 60){
     54 		r = C,g = X,b = 0;
     55 	}
     56 	else if(H >= 60 && H < 120){
     57 		r = X,g = C,b = 0;
     58 	}
     59 	else if(H >= 120 && H < 180){
     60 		r = 0,g = C,b = X;
     61 	}
     62 	else if(H >= 180 && H < 240){
     63 		r = 0,g = X,b = C;
     64 	}
     65 	else if(H >= 240 && H < 300){
     66 		r = X,g = 0,b = C;
     67 	}
     68 	else{
     69 		r = C,g = 0,b = X;
     70 	}
     71 
     72 	int R = (r+m)*255;
     73 	int G = (g+m)*255;
     74 	int B = (b+m)*255;
     75 
     76 	ret (R << 16) + (G << 8) + B;
     77 }
     78 
     79 void showStuff(void *context) {
     80   forever {
     81 
     82     // 40 images/s
     83     paletteShift = (getMonotonicTime()/25000000) % 1024;
     84 
     85     tb_clear();
     86     range(y, tb_height()*2) {
     87       range(x, tb_width()) {
     88         if (y & 1) {
     89           // odd line
     90           tb_change_cell(/*x*/ x, /*y*/ y/2, 0x2584, palette[(plasma[y][x] + paletteShift) % 256],
     91                                                      palette[(plasma[y-1][x] + paletteShift) % 256]);
     92         }
     93       }
     94     }
     95 
     96     tb_present();
     97 
     98     msSleep(25);
     99     if (quit) break;
    100   }
    101 }
    102 
    103 int main(int ARGC, char** ARGV) {
    104 
    105   argc = ARGC; argv = ARGV;
    106 
    107   initLibsheepy(ARGV[0]);
    108   setLogMode(LOG_FUNC);
    109   pError0(openProgLogFile());
    110   //setLogSymbols(LOG_UTF8);
    111   //disableLibsheepyErrorLogs;
    112 
    113   int r = tb_init();
    114   if (r) {
    115     logE("tb_init() failed with error code %d\n", r);
    116     XFailure;
    117   }
    118 
    119   tb_select_output_mode(TB_OUTPUT_TRUECOLOR);
    120 
    121   //tb_set_cursor(0,0);
    122 
    123   void draw_all(void) {
    124     tb_clear();
    125     //tb_change_cell(/*x*/ 2, /*y*/ 2, '@', TB_GREEN, TB_WHITE | TB_BOLD);
    126     tb_present();
    127   }
    128 
    129   draw_all();
    130 
    131   /* range(j, ybmXT_549309366Height) { */
    132   /*   range(i, ybmXT_549309366Width) { */
    133   /*     if (j & 1) { */
    134   /*       // odd line */
    135   /*       tb_change_cell(#<{(|x|)}># i, #<{(|y|)}># j/2, 0x2584, ybmXT_549309366Pixels[j * ybmXT_549309366Width + i], ybmXT_549309366Pixels[(j-1) * ybmXT_549309366Width + i]); */
    136   /*     } */
    137   /*     else { */
    138   /*       // even line */
    139   /*       tb_change_cell(#<{(|x|)}># i, #<{(|y|)}># j/2, 0x2584, TB_DEFAULT, ybmXT_549309366Pixels[j * ybmXT_549309366Width + i]); */
    140   /*     } */
    141   /*   } */
    142   /* } */
    143 
    144   arange(i, palette) {
    145     //palette[i] = HSVtoRGB((f32)i*360.0/256.0,100,100);
    146     /* palette[i] =   ((u32)(128.0 + 128 * sin(3.1415 * (float)i / 32.0)) << 16) */
    147     /*              + ((u32)(128.0 + 128 * sin(3.1415 * (float)i / 64.0)) << 8) */
    148     /*              + (u32)(128.0 + 128 * sin(3.1415 * (float)i / 128.0)); */
    149     palette[i] =   ((u32)(128.0 + 128 * sin(3.1415 * (float)i / 16.0)) << 16)
    150                  + ((u32)(128.0 + 128 * sin(3.1415 * (float)i / 128.0)) << 8)
    151                  + 0;
    152   }
    153 
    154   void genPlasma() {
    155     u32 w = tb_width();
    156     u32 h = tb_height()*2;
    157     range(y, tb_height()*2) {
    158       range(x, tb_width()) {
    159         if (y & 1) {
    160           // odd line
    161           //tb_change_cell(/*x*/ x, /*y*/ y/2, 0x2584, pixels[imageIndex][y * widths[imageIndex] + i], pixels[imageIndex][(j-1) * widths[imageIndex] + i]);
    162           //u32 color = 128.0 + (128.0 * sin(x / 8.0));
    163           /* u32 color     = 128.0 + (128.0 * sin(sqrt((x - w / 2.0) * (x - w / 2.0) + (y - h / 2.0) * (y - h / 2.0)) / 8.0)); */
    164           /* u32 colorDown = 128.0 + (128.0 * sin(sqrt((x - w / 2.0) * (x - w / 2.0) + ((y-1) - h / 2.0) * ((y-1) - h / 2.0)) / 8.0)); */
    165           /* u32 color     = (128.0 + (128.0 * sin(x / 16.0)) + 128.0 + (128.0 * sin(y / 16.0))) / 2; */
    166           /* u32 colorDown = (128.0 + (128.0 * sin(x / 16.0)) + 128.0 + (128.0 * sin((y-1) / 16.0))) / 2; */
    167           /* u32 color     = ( 128.0 + (128.0 * sin(x / 16.0)) */
    168           /*                 + 128.0 + (128.0 * sin(y / 8.0)) */
    169           /*                 + 128.0 + (128.0 * sin((x + y) / 16.0)) */
    170           /*                 + 128.0 + (128.0 * sin(sqrt(x * x + y * y) / 8.0)) */
    171           /*                 ) / 4; */
    172           /* u32 colorDown = ( 128.0 + (128.0 * sin(x / 16.0)) */
    173           /*                 + 128.0 + (128.0 * sin((y-1) / 8.0)) */
    174           /*                 + 128.0 + (128.0 * sin((x + (y-1)) / 16.0)) */
    175           /*                 + 128.0 + (128.0 * sin(sqrt(x * x + (y-1) * (y-1)) / 8.0)) */
    176           /*                 ) / 4; */
    177           u32 color     = ( 128.0 + (128.0 * sin(x / 16.0))
    178                           + 128.0 + (128.0 * sin(y / 32.0))
    179                           + 128.0 + (128.0 * sin(sqrt(((x - w / 2.0)* (x - w / 2.0) + (y - h / 2.0) * (y - h / 2.0))) / 8.0))
    180                           + 128.0 + (128.0 * sin(sqrt((x * x + y * y)) / 8.0))
    181                           ) / 4;
    182           u32 colorDown = ( 128.0 + (128.0 * sin(x / 16.0))
    183                           + 128.0 + (128.0 * sin((y-1) / 32.0))
    184                           + 128.0 + (128.0 * sin(sqrt(((x - w / 2.0)* (x - w / 2.0) + ((y-1) - h / 2.0) * ((y-1) - h / 2.0))) / 8.0))
    185                           + 128.0 + (128.0 * sin(sqrt((x * x + (y-1) * (y-1))) / 8.0))
    186                           ) / 4;
    187           /* u32 rgb = (color << 16) + (color << 8) + color; */
    188           /* u32 rgbDown = (colorDown << 16) + (colorDown << 8) + colorDown; */
    189           /* u32 rgb = palette[color]; */
    190           /* u32 rgbDown = palette[colorDown]; */
    191           plasma[y][x] = color;
    192           plasma[y-1][x] = colorDown;
    193           //tb_change_cell(/*x*/ x, /*y*/ y/2, 0x2584, rgb, rgbDown);
    194         }
    195       }
    196     }
    197   }
    198   genPlasma();
    199 
    200   tpoolAdd(showStuff, null /*context*/);
    201 
    202   struct tb_event ev;
    203   while (tb_poll_event(&ev)) {
    204     switch (ev.type) {
    205       case TB_EVENT_KEY:
    206         switch (ev.key) {
    207           /* case TB_KEY_ESC: */
    208           /*   ctx.quit = yes; */
    209           /*   goto done; */
    210           /*   break; */
    211           case TB_KEY_SPACE:
    212             //showImage();
    213             break;
    214         }
    215         if (ev.ch == 'q') {
    216           quit = yes;
    217           goto done;
    218         }
    219 
    220         //tb_stringf(2, 30, TB_WHITE, TB_DEFAULT, "key code: %5d, char: %5d, mod: %5d", ev.key, ev.ch, ev.mod);
    221         break;
    222       case TB_EVENT_RESIZE:
    223         draw_all();
    224         break;
    225     }
    226     tb_present();
    227   }
    228 
    229   done:
    230   tpoolWait;
    231   tb_shutdown();
    232   XSuccess;
    233 }
    234 // vim: set expandtab ts=2 sw=2: