Skip to content

Instantly share code, notes, and snippets.

@LamberKeep
Last active April 25, 2023 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LamberKeep/b6a3815866483f80630433d375e898cd to your computer and use it in GitHub Desktop.
Save LamberKeep/b6a3815866483f80630433d375e898cd to your computer and use it in GitHub Desktop.
Hexadecimal number to braille pattern conventer. Binary clock. Now on C!
/**
* @file braille_binary_clock.c
*
* Hexadecimal number to braille pattern conventer. Binary clock.
*
* Created: 2023-04-26
* Updated: *never*
*
* @author Alexey Savin (LamberKeep)
*/
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
#include <uchar.h>
#include <time.h>
#include <unistd.h>
/**
* Translates 16 or less base to UTF-8 braille pattern character.
*
* Caution! Don't use a base greater than 16, as well as dozens more than 15.
*
* List of all braille pattern characters:
* https://en.wikibooks.org/wiki/Unicode/Character_reference/2000-2FFF
*
* @param input the input value that will be converted to a character.
* @param base the number system of this value.
* @return The Unicode character code.
*/
unsigned short hexToBraille(unsigned char input, unsigned char base) {
unsigned char intFirst = (unsigned char) input / base;
unsigned char intSecond = input % base;
unsigned char inputFirst = intFirst;
unsigned char inputSecond = intSecond;
// FIRST
if (intFirst % 2 != 0) {
intFirst -= 1;
}
if (intFirst == 0 || intFirst == 6) {
// pass
} else if (intFirst < 6) {
if (intFirst < 3) {
intFirst += 2;
} else {
intFirst -= 2;
}
} else {
if (intFirst % 4 == 0) {
intFirst -= 2;
}
if (inputFirst > 11) {
intFirst -= 2;
}
intFirst -= 5;
}
if (inputSecond > 7) {
intFirst += 8;
}
// SECOND
if (intSecond >= 8) {
intSecond -= 8;
}
if (intSecond >= 4) {
if (intSecond % 2 == 0) {
intSecond -= 3;
} else {
intSecond += 4;
}
} else if (intSecond % 2 != 0) {
intSecond += 7;
}
if (inputFirst % 2 != 0) {
intSecond += 4;
}
return (((unsigned short) 0x0280 + intSecond) << 4) + intFirst;
}
int main() {
setlocale(LC_CTYPE, "");
while (1) {
time_t now = time(NULL);
struct tm tm = *localtime(&now);
wprintf(
L"%lc %lc %lc\n",
hexToBraille((unsigned char) tm.tm_hour, (unsigned char) 10),
hexToBraille((unsigned char) tm.tm_min, (unsigned char) 10),
hexToBraille((unsigned char) tm.tm_sec, (unsigned char) 10)
);
sleep(1);
}
return 0;
}
"""
@file braille_binary_clock.py
Hexadecimal number to braille pattern conventer. Binary clock.
Created: 2022-11-17
Updated: 2023-04-26
@author Alexey Savin (LamberKeep)
"""
def hexToBraille(value: int, base = 16):
"""
Translates 16 or less base to UTF-8 braille pattern character.
Caution! Don't use a base greater than 16, as well as dozens more than 15.
List of all braille pattern characters:
https://en.wikibooks.org/wiki/Unicode/Character_reference/2000-2FFF
@param input the input value that will be converted to a character.
@param base the number system of this value.
@return The Unicode character code.
"""
intFirst = value // base
intSecond = value % base
# FIRST
if intFirst % 2 != 0:
intFirst -= 1
if intFirst == 0 or intFirst == 6:
pass
elif intFirst < 6:
if intFirst < 3:
intFirst += 2
else:
intFirst -= 2
else: # intFirst > 6
if intFirst % 4 == 0:
intFirst -= 2
if value // base > 11:
intFirst -= 2
intFirst -= 5
if value % base > 7:
intFirst += 8
# SECOND
if intSecond >= 8:
intSecond -= 8
if intSecond >= 4:
if intSecond % 2 == 0:
intSecond -= 3
else:
intSecond += 4
elif intSecond % 2 != 0:
intSecond += 7
if value // base % 2 != 0:
intSecond += 4
return 0x2800 + intSecond * 16 + intFirst
def binaryClock(loop = True, print_numbers = False, base = 10, sep=" "):
"""
Print binary clock as braille pattern characters.
@param loop repeat forever. Defaults to True.
@param print_numbers print time as numbers. Defaults to False.
@param base output number base. Defaults to 10.
@param sep string separator. Default: " ".
"""
import time
while True:
t = time.localtime(time.time())
if print_numbers:
print(t[3], end=sep)
print(t[4], end=sep)
print(t[5])
print(chr(hexToBraille(t[3], base)), end=sep)
print(chr(hexToBraille(t[4], base)), end=sep)
print(chr(hexToBraille(t[5], base)))
if not loop:
break
time.sleep(1)
def testCharacters(path: str):
"""
Debug function.
Compares the output of a function against a file of all symbols.
@param path the path to the file.
"""
count = 0
passed = 0
print("input\toutput\tresult")
for line in open(path).readlines():
items = line.split("\t")
correct_item = int(items[0], 16)
testing_item = hexToBraille(int(items[2] + items[3], 16))
print(f"{correct_item}\t{testing_item}", end=" ")
count += 1
if correct_item == testing_item:
print("\tpass")
passed += 1
else:
print("\tfail")
print(f"\npassed: {passed}/{count} ({round(passed/count*100, 1)}%)")
def getAllCharacters(base = 16):
"""
Debug function.
Prints all 255 braille pattern character, taking into account the number system.
@param base output number base. Defaults to 16.
"""
for i in range(0, 254):
print(f"{i}\t{chr(hexToBraille(i, base))}")
if __name__ == "__main__":
# Starts binary clock.
binaryClock(False, sep=" ")
# Print one character.
# print(chr(hexToBraille(0xff))) # Returns ⣿
# print(chr(hexToBraille(12, 10))) # Returns ⡠ (in base 10)
# Get all characters from range 0-255.
# getAllCharacters(16)
# getAllCharacters(10) # won't work after 159 (look hexToBraille function description)
# Test all characters from characters.txt (debug).
# testCharacters("./characters.txt")
U+2800 ⠀ 0 0
U+2801 ⠁ 8 0
U+2802 ⠂ 4 0
U+2803 ⠃ C 0
U+2804 ⠄ 2 0
U+2805 ⠅ A 0
U+2806 ⠆ 6 0
U+2807 ⠇ E 0
U+2808 ⠈ 0 8
U+2809 ⠉ 8 8
U+280A ⠊ 4 8
U+280B ⠋ C 8
U+280C ⠌ 2 8
U+280D ⠍ A 8
U+280E ⠎ 6 8
U+280F ⠏ E 8
U+2810 ⠐ 0 4
U+2811 ⠑ 8 4
U+2812 ⠒ 4 4
U+2813 ⠓ C 4
U+2814 ⠔ 2 4
U+2815 ⠕ A 4
U+2816 ⠖ 6 4
U+2817 ⠗ E 4
U+2818 ⠘ 0 C
U+2819 ⠙ 8 C
U+281A ⠚ 4 C
U+281B ⠛ C C
U+281C ⠜ 2 C
U+281D ⠝ A C
U+281E ⠞ 6 C
U+281F ⠟ E C
U+2820 ⠠ 0 2
U+2821 ⠡ 8 2
U+2822 ⠢ 4 2
U+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