Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* written by iskunk (Daniel Richard G.)
- http://www.housedillon.com/?p=1272 (check the Wayback Machine)
- emulates CM-5's 'random and pleasing' LED panel mode (rev. 3)
- FILE IS IN THE PUBLIC DOMAIN */
- #include <stdio.h>
- #include <string.h> /* defines memset */
- #include <stdint.h> /* defines fixed-size 2-byte integer uint16_t */
- #include <stdlib.h>
- #include <unistd.h> /* defines usleep function */
- #include <locale.h>
- #include <wchar.h>
- #define SLEEP 222222 /* was 250000 */
- #define NUM_ROWS 64 /* unique rows - was 32 */
- #define NUM_ROWS_DISPLAYED 64 /* displayed rows - was 106 on the CM-5 */
- #define CHAR '▒' /* ■ ▀ ▄ █ ▓ ▒ ░ - they all print out the same char */
- int main(void)
- {
- setlocale(LC_CTYPE, "");
- /* uint16_t is used as integer sizes can vary between machines */
- uint16_t gen, i, gen_bit, row_bit, rows[NUM_ROWS], m, pos;
- wchar_t output[18];
- memset(rows, 0, sizeof(rows)); /* zeroes out the rows array */
- //fputs("\033[0;31m", stdout); /* set forground color to red */
- gen = 1; /* non-random seed for LFSR - other numbers also work */
- for (;;) /* main loop - each iteration fills out the entire grid */
- {
- //fputs("\033[2J\033[1;1H", stdout); /* ANSI to clear terminal */
- system("clear||cls");
- for (i = 0; i < NUM_ROWS; i++) /* run for num unique rows */
- {
- /* see: wikipedia.org/wiki/Linear_feedback_shift_register
- a way to generate pseudo-random nums from a non-random seed
- operation is deterministic, has a finite number of states,
- will eventually repeat itself, can have a long cycle
- primitive polynomial: x^16 + x^12 + x^3 + x^1 + 1 */
- /* C bitwise operators used - all have L to R associativity:
- & = AND | = OR ^ = XOR
- >> = right-shift << = left-shift */
- gen_bit = ((gen >> 0)^(gen >> 4)^(gen >> 13)^(gen >> 15)) & 1;
- row_bit = ((gen >> 0) & (gen >> 1)) & 1;
- gen = (gen_bit << 15) | (gen >> 1);
- /* the 4 defines the row block height */
- if (i & 4)
- rows[i] = (row_bit << 15) | (rows[i] >> 1);
- else
- rows[i] = (rows[i] << 1) | row_bit;
- }
- /* run 64 times - number of displayed rows */
- for (i = 0; i < NUM_ROWS_DISPLAYED; i++)
- {
- /* why is m being setup this way? it's equal to 32768 */
- for (m = 1 << 15, pos = 0; m != 0; m >>= 1, pos++)
- output[pos] = ((rows[i & 31]) & m) ? L'█' : L' ';
- output[16] = 10;
- output[17] = 0;
- wprintf(output);
- }
- usleep(SLEEP); /* pause between each loop iteration */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement