iskunk

Attempt at CM-5 "random and pleasing" pattern (rev. 2)

Mar 20th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1. /* http://www.housedillon.com/?p=1272
  2.  * Written by iskunk (Daniel Richard G.)
  3.  * THIS FILE IS IN THE PUBLIC DOMAIN
  4.  *
  5.  * Program to emulate the CM-5's "random and pleasing" LED panel mode
  6.  * (revision 2)
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdint.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13.  
  14. #define NUM_ROWS 128
  15.  
  16. static uint16_t fn(uint16_t x)
  17. {
  18.     /* https://en.wikipedia.org/wiki/Linear_feedback_shift_register
  19.      * Primitive polynomial: x^16 + x^12 + x^3 + x^1 + 1
  20.      */
  21.     return ((x >> 0) ^ (x >> 4) ^ (x >> 13) ^ (x >> 15)) & 1;
  22. }
  23.  
  24. static void print_row(uint16_t x)
  25. {
  26.     uint16_t m;
  27.     int pos;
  28.     char v[17];
  29.  
  30.     for (m = 1 << 15, pos = 0; m != 0; m >>= 1, pos++)
  31.         v[pos] = (x & m) ? 'O' : '-';
  32.  
  33.     v[16] = '\0';
  34.  
  35.     puts(v);
  36. }
  37.  
  38. int main(void)
  39. {
  40.     uint16_t gen = 1;
  41.     uint16_t rows[NUM_ROWS];
  42.     int i;
  43.  
  44.     memset(rows, 0, sizeof(rows));
  45.  
  46.     for (;;)
  47.     {
  48.         /* ANSI sequence to clear terminal */
  49.         fputs("\033[2J\033[1;1H", stdout);
  50.  
  51.         for (i = 0; i < NUM_ROWS; i++)
  52.         {
  53.             uint16_t gen_bit = fn(gen);
  54.             uint16_t row_bit = ((gen >> 0) & (gen >> 1)) & 1;
  55.  
  56.             gen = (gen_bit << 15) | (gen >> 1);
  57.  
  58.             if (i & 4)
  59.                 rows[i] = (rows[i] << 1) | row_bit;
  60.             else
  61.                 rows[i] = (row_bit << 15) | (rows[i] >> 1);
  62.  
  63.             print_row(rows[i]);
  64.         }
  65.  
  66.         fflush(stdout);
  67.         usleep(250000);
  68.     }
  69.  
  70.     return 0;
  71. }
  72.  
  73. /* EOF */
Advertisement
Add Comment
Please, Sign In to add comment