Skip to content

Instantly share code, notes, and snippets.

@mulle-nat
Created May 10, 2025 12:07
Show Gist options
  • Save mulle-nat/563639200da0adbf1f14ed83ecc9c06c to your computer and use it in GitHub Desktop.
Save mulle-nat/563639200da0adbf1f14ed83ecc9c06c to your computer and use it in GitHub Desktop.
A braille spinner animation
#include <stdio.h>
#include <unistd.h>
unsigned int braille_unicode_from_4x2_bytemap( char map[4][2])
{
unsigned int pattern = 0;
// Map dots to their corresponding bit positions in Braille
// Standard Braille dot positions are:
// 1 4 dot 1 = 0x01, dot 4 = 0x08
// 2 5 dot 2 = 0x02, dot 5 = 0x10
// 3 6 dot 3 = 0x04, dot 6 = 0x20
// 7 8 dot 7 = 0x40, dot 8 = 0x80
if( map[ 0][ 0] > 0) pattern |= 0x01; // dot 1
if( map[ 1][ 0] > 0) pattern |= 0x02; // dot 2
if( map[ 2][ 0] > 0) pattern |= 0x04; // dot 3
if( map[ 0][ 1] > 0) pattern |= 0x08; // dot 4
if( map[ 1][ 1] > 0) pattern |= 0x10; // dot 5
if( map[ 2][ 1] > 0) pattern |= 0x20; // dot 6
if( map[ 3][ 0] > 0) pattern |= 0x40; // dot 7
if( map[ 3][ 1] > 0) pattern |= 0x80; // dot 8
// Convert to UTF-8
return( 0x2800 + pattern); // 0x2800 is the Unicode Braille Pattern base
}
static unsigned int x_values[ 8] =
{
0, 1, 1, 1, 1, 0, 0, 0
};
static unsigned int y_values[ 8] =
{
0, 0, 1, 2, 3, 3, 2, 1
};
static void get_x_y_from_i( unsigned int i, unsigned int *x, unsigned int *y)
{
i &= 0x7;
*x = x_values[ i];
*y = y_values[ i];
}
static void init_4x2_bytemap( char map[4][2], unsigned int i)
{
unsigned int cycle;
memset( map, 0, 8);
cycle = i / 16;
cycle = cycle & 0xF;
if( cycle >= 8)
cycle = 15 - cycle;
switch( cycle & 0x7)
{
case 7 : map[ 0][ 1] = 0x1;
case 6 : map[ 1][ 1] = 0x1;
case 5 : map[ 2][ 1] = 0x1;
case 4 : map[ 3][ 1] = 0x1;
case 3 : map[ 3][ 0] = 0x1;
case 2 : map[ 2][ 0] = 0x1;
case 1 : map[ 1][ 0] = 0x1;
case 0 : break;
}
}
static void animate_4x2_bytemap( char map[4][2], unsigned int i)
{
unsigned int x, y;
if( (i & 0x7) == 0)
{
init_4x2_bytemap( map, i);
}
else
{
get_x_y_from_i( i - 1, &x, &y);
map[ y][ x] ^= 0x1;
}
get_x_y_from_i( i, &x, &y);
map[ y][ x] ^= 0x1;
}
int main(int argc, char *argv[])
{
unsigned int i = 0;
char map[ 4][ 2] = { 0 };
char utf8[ 4];
unsigned int unicode;
// Hide cursor
printf("\033[?25l");
while( 1)
{
animate_4x2_bytemap( map, i);
unicode = braille_unicode_from_4x2_bytemap( map);
utf8[0] = 0xE0 | (unicode >> 12);
utf8[1] = 0x80 | ((unicode >> 6) & 0x3F);
utf8[2] = 0x80 | (unicode & 0x3F);
utf8[3] = '\0';
printf("\r%s %u", utf8, i);
fflush(stdout);
usleep(50000);
++i;
}
printf("\033[?25h");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment