Skip to content

Commit

Permalink
Fix translation and serial echo, mior refactoring
Browse files Browse the repository at this point in the history
dakalamin committed Oct 24, 2024
1 parent 870e232 commit 4baf6f4
Showing 5 changed files with 85 additions and 81 deletions.
12 changes: 2 additions & 10 deletions src/buffer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "nchar.h"
#include "config.h"
#include "patterns.h"

@@ -63,16 +64,7 @@ class Buffer {
add(element);

#if SERIAL_ECHO
byte ncharBytes;
for (ncharBytes = 4; (ncharBytes > 1) && !(nchar & 0xFF000000); ncharBytes--, nchar <<= 8) { }

char* output = new char[ncharBytes];

for (byte i = 0; i < ncharBytes; i++, nchar <<= 8)
output[i] = (nchar > 0) ? nchar >> 24 : 0;

Serial.print(output);
delete [] output;
printNchar(nchar);
#endif
}

94 changes: 38 additions & 56 deletions src/config.h
Original file line number Diff line number Diff line change
@@ -2,6 +2,9 @@

#include <Arduino.h>

//! PLEASE DO NOT EDIT THIS FILE
//! PUT YOUR SETTINGS IN `src/custom_config.h` FILE


/*
Normalized UTF-8 char (nUTF-8) - left padded with 0s to reach 32 bits
@@ -27,67 +30,46 @@ typedef uint8_t braille_t;
#define LATCH_PIN 12 // ST_CP pin of 74HC595 shift register
#define CLOCK_PIN 13 // SH_CP pin of 74HC595 shift register

#define BAUD_RATE 9600
#define MIN_DELAY_MS 2

#ifndef PLATFORMIO
#include "custom_config.h"
#endif
#define BUTTON_CLICK_MS 100
#define BUTTON_CYCLE_MS 1000

#define AUTOCOUNT_BIT (1 << 7)
// if braille cells must be automatically counted
#define AUTOCOUNT_CELLS false
// number of braille cells if ther are not automatically counted
#define BRAILLE_CELLS 4

#ifndef BAUD_RATE
#define BAUD_RATE 9600
#endif
#define ANIMATION_ON_START true
#define ANIMATION_MS_PER_CELL 500

// if symbols must be printed to Serial
// as they are being sent to braille display
#define SERIAL_ECHO true

/*
LF is End Of Message
If 0, concatinates messages before and after LF symbol
e.g.: message is "x1[LF]0"
if 0 ⟶ "x10" ⟶ ⠭⠼⠁⠚
if 1 ⟶ "x1"|"0" ⟶ ⠭⠼⠁|⠼⠚
*/
#define LF_IS_EOM true
/*
Serial Not Available is End Of Message
If 0, concatinates messages before and after Serial not available
e.g.: message is "x1[Serial NA]0"
if 0 ⟶ "x10" ⟶ ⠭⠼⠁⠚
if 1 ⟶ "x1"|"0" ⟶ ⠭⠼⠁|⠼⠚
*/
#define SERIAL_NA_IS_EOM false

#define MIN_DELAY_MS 2

#ifndef BUTTON_CLICK_MS
#define BUTTON_CLICK_MS 100
#endif
#ifndef BUTTON_CYCLE_MS
#define BUTTON_CYCLE_MS 1000
#endif


#define AUTOCOUNT_BIT (1 << 7)
#ifndef AUTOCOUNT_CELLS
#define AUTOCOUNT_CELLS true
#endif
#ifndef BRAILLE_CELLS
#define BRAILLE_CELLS 4
#endif

#ifndef ANIMATION_ON_START
#define ANIMATION_ON_START true
#endif
#ifndef ANIMATION_MS_PER_CELL
#define ANIMATION_MS_PER_CELL 500
#endif

#ifndef SERIAL_ECHO
#define SERIAL_ECHO true
#endif

#ifndef LF_IS_EOM
/*
LF is End Of Message
If 0, concatinates messages before and after LF symbol
e.g.: message is "x1[LF]0"
if 0 ⟶ "x10" ⟶ ⠭⠼⠁⠚
if 1 ⟶ "x1"|"0" ⟶ ⠭⠼⠁|⠼⠚
*/
#define LF_IS_EOM true
#endif
#ifndef SERIAL_NA_IS_EOM
/*
Serial Not Available is End Of Message
If 0, concatinates messages before and after Serial not available
e.g.: message is "x1[Serial NA]0"
if 0 ⟶ "x10" ⟶ ⠭⠼⠁⠚
if 1 ⟶ "x1"|"0" ⟶ ⠭⠼⠁|⠼⠚
*/
#define SERIAL_NA_IS_EOM false
#endif
#include "custom_config.h"


byte brailleCells;
8 changes: 7 additions & 1 deletion src/custom_config.h
Original file line number Diff line number Diff line change
@@ -2,7 +2,13 @@

/*
Configure the project HERE
If you use PlatformIO, consider configuring it in `platformio.ini` first
If you use PlatformIO, you can also use
`build_flags` section of `platformio.ini` file
Configurations priority:
3. `platformio.ini` - only if you use PlatformIO
2. `src/config.h` - not supposed to be edited
1. `src/custom_config.h` - this file
*/

// #define AUTO_PIN // uint
30 changes: 26 additions & 4 deletions src/nchar.h
Original file line number Diff line number Diff line change
@@ -9,19 +9,25 @@ enum NcharSpecial : nchar32_t {
INVALID_NCHAR = UINT8_MAX,
EMPTY_NCHAR = INVALID_NCHAR - 1,
};
enum NcharAttribute : byte {
COMMON = 1 << 0,
IS_RUSSIAN = 1 << 1,
IS_UPPER_CASE = 1 << 2,
IS_NUMERIC = 1 << 3,
};

nchar32_t readUTF8Char() {
nchar32_t input = Serial.read();
byte octetsToRead;

if (input >> 7 == 0b0)
return input; // 0xxxxxxx -> 1 octet
return input; // 0xxxxxxx -> symbol is already read
else if (input >> 5 == 0b110)
octetsToRead = 1; // 110xxxxx -> 2 octets (1 octet to read)
octetsToRead = 1; // 110xxxxx -> 1 octet to read (2 octets total)
else if (input >> 4 == 0b1110)
octetsToRead = 2; // 1110xxxx -> 3 octets (2 octets to read)
octetsToRead = 2; // 1110xxxx -> 2 octets to read (3 octets total)
else if (input >> 3 == 0b11110)
octetsToRead = 3; // 11110xxx -> 4 octets (3 octets to read)
octetsToRead = 3; // 11110xxx -> 3 octets to read (4 octets total)
else
return INVALID_NCHAR; // anything else -> invalid

@@ -53,3 +59,19 @@ bool getNextNchar(nchar32_t& nchar) {
nchar = readUTF8Char();
return true;
}

void printNchar(nchar32_t nchar) {
if (nchar == INVALID_NCHAR)
return printNchar(0xEFBFBD); // � (U+FFFD)

byte ncharBytes = 1;
for (; (nchar >> (8 * ncharBytes)) & UINT8_MAX; ncharBytes++) { }

char* output = new char[ncharBytes + 1];
output[ncharBytes] = 0; // last byte must be 0
for (; ncharBytes > 0; ncharBytes--, nchar >>= 8)
output[ncharBytes - 1] = nchar & UINT8_MAX;

Serial.print(output);
delete [] output;
}
22 changes: 12 additions & 10 deletions src/translation.h
Original file line number Diff line number Diff line change
@@ -6,13 +6,6 @@
#include "nchar.h"


enum NcharAttribute : byte {
COMMON = 1 << 0,
IS_RUSSIAN = 1 << 1,
IS_UPPER_CASE = 1 << 2,
IS_NUMERIC = 1 << 3,
};

enum MessageAttribute : byte {
NEW_MESSAGE = 0,
QUOTATION_PARITY = 1 << 0,
@@ -26,9 +19,9 @@ enum RussianChar16 : uint16_t {
RUS_UPPER_LAST = 0xD0AF, // Я
RUS_UPPER_YO = 0xD081, // Ё

RUS_LOWER_FIRST = RUS_ALPHABET_LEN + RUS_UPPER_FIRST, // а
RUS_LOWER_LAST = RUS_ALPHABET_LEN + RUS_UPPER_LAST, // я
RUS_LOWER_YO = RUS_ALPHABET_LEN + RUS_UPPER_YO, // ё
RUS_LOWER_FIRST = 0xD0B0, // а
RUS_LOWER_LAST = 0xD18F, // я
RUS_LOWER_YO = 0xD191, // ё
};

byte firstEmptyIndex = 0;
@@ -77,6 +70,15 @@ byte ncharToBraille(nchar32_t nchar, byte& attribute) {
else if (RUS_LOWER_FIRST <= nchar && nchar <= RUS_LOWER_LAST) { // RUSSIAN LOWER CASE
attribute = IS_RUSSIAN;
index = nchar - RUS_LOWER_FIRST;
// from 'р' to 'я' values are shifted by +192
// ...
// 'о' = [110]10000 [10]111110 -> 'о' - 'а' = 14
// 'п' = [110]10000 [10]111111 -> 'п' - 'а' = 15
// 'р' = [110]10001 [10]000000 -> 'р' - 'а' = 208 -> 208 - 192 = 16
// 'с' = [110]10001 [10]000001 -> 'с' - 'а' = 209 -> 208 - 192 = 17
// ...
if (index > RUS_ALPHABET_LEN)
index -= 192;
array = RUSSIAN_TO_BRAILLE;
}
else if (nchar == RUS_UPPER_YO) { // ----------------------------- RUSSIAN Ё

0 comments on commit 4baf6f4

Please sign in to comment.