#include <Keyboard.h>
#include "GyverButton.h"
int bit_array[25]; // For storing the data bit. bit_array[0] = data bit 1 (LSB), bit_array[23] = data bit 24 (MSB).
const int CLOCK_PIN = 4; // штангенциркуль
const int DATA_PIN = 5; // штангенциркуль
const int BTN_PIN = 9; // кнопка ввод/режимы
const int LED_SELECT_PIN = 6; // светодиод режима "значение + выделить"
const int LED_INPUT_PIN = 7; // светодиод режима "ввод значения"
const int LED_TAB_PIN = 8; // светодиод режима "значение + табулятор"
const int BEEPER_PIN = 3; // пищалка
unsigned long time_now; // For storing the time when the clock signal is changed from HIGH to LOW (falling edge trigger of data output).
//float value = 0.0;
byte state = 0; // состояние формата вывода: перенос, маркировать, табуляция
byte zeros = 2;
GButton butt1(BTN_PIN);
void setup() {
pinMode(CLOCK_PIN, INPUT);
pinMode(DATA_PIN, INPUT);
pinMode(LED_SELECT_PIN, OUTPUT);
pinMode(LED_INPUT_PIN, OUTPUT);
pinMode(LED_TAB_PIN, OUTPUT);
pinMode(BEEPER_PIN, OUTPUT);
//Serial.begin(9600);
butt1.setDebounce(1); // настройка антидребезга (по умолчанию 80 мс)
butt1.setTimeout(1000); // настройка таймаута долгий клик
butt1.setClickTimeout(1); // настройка таймаута быстрый клик
set_mode();
}
// #### основной код ####
void loop() {
butt1.tick(); // обязательная функция отработки. Должна постоянно опрашиваться
while (digitalRead(CLOCK_PIN) == LOW); // If clock is LOW wait until it turns to HIGH
time_now = micros();
while (digitalRead(CLOCK_PIN) == HIGH); // Wait for the end of the HIGH pulse
if ((micros() - time_now) > 500) // If the HIGH pulse was longer than 500 micros we are at the start of a new bit sequence
{
value = decode(); //decode the bit sequence
}
if (butt1.isClick())
{
tone(BEEPER_PIN, 1000, 100);
send_value();
}
if (butt1.isHolded())
{
if (state == 2)
{
state = 0;
}
else
{
state++;
}
tone(BEEPER_PIN, 500, 100);
set_mode();
}
}
// ##### вспомогательные функции #### //
float decode(void) {
int bit_array[25]; // For storing the data bit. bit_array[0] = data bit 1 (LSB), bit_array[23] = data bit 24 (MSB).
byte sign = 1;
byte i = 0;
float value = 0.0;
float result = 0.0;
bit_array[i] = digitalRead(DATA_PIN); // Store the 1st bit (start bit) which is always 1.
while (digitalRead(CLOCK_PIN) == HIGH);
for (i = 1; i <= 24; i++) {
while (digitalRead(CLOCK_PIN) == LOW); // Wait until clock returns to HIGH
bit_array[i] = digitalRead(DATA_PIN);
while (digitalRead(CLOCK_PIN) == HIGH); // Wait until clock returns to LOW
}
for (i = 0; i <= 24; i++) { // Show the content of the bit array. This is for verification only.
//Serial.print(bit_array[i]);
//Serial.print(" ");
}
for (i = 1; i <= 20; i++)// Turning the value in the bit array from binary to decimal.
{
value = value + (pow(2, i-1) * bit_array[i]);
}
if (bit_array[21] == 1) sign = -1; // Bit 21 is the sign bit. 0 -> +, 1 => -
if (bit_array[24] == 1) // Bit 24 tells the measureing unit (1 -> in, 0 -> mm)
{
result = (value*sign) / 2000.00;
zeros = 4;
//Serial.print(result, 3); // Print result with 3 decimals
//Serial.println(" in");
}
else
{
result = (value*sign) / 100.00;
zeros = 2;
//Serial.print(result, 2); // Print result with 2 decimals
//Serial.println(" mm");
}
return result;
}
void set_mode()
{
switch (state)
{
case 0: set_select();
break;
case 1: set_input();
break;
case 2: set_tab();
break;
}
}
void send_value()
{
switch (state)
{
case 0: send_select();
break;
case 1: send_input();
break;
case 2: send_tab();
break;
}
}
// ### отправка данных в клавиатуру ###
void send_select()
{
//Serial.print("send mode 0 - value and select ctrl+A "); //
//Serial.println(value);
Keyboard.print(result, zeros); // send a 'result' to the computer via Keyboard HID
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('A');
Keyboard.releaseAll();
}
void send_input()
{
//Serial.print("send mode 1 - value and input "); //
//Serial.println(value);
Keyboard.print(result, zeros); // send a 'result' to the computer via Keyboard HID
Keyboard.press(KEY_RETURN);
Keyboard.releaseAll();
}
void send_tab()
{
//Serial.print("send mode 2 - value and tabulator "); //
//Serial.println(value);
Keyboard.print(result, zeros); // send a 'result' to the computer via Keyboard HID
Keyboard.press(KEY_TAB);
Keyboard.releaseAll();
}
// ### установка светодиодов режима ###
void set_select(void)
{
//Serial.println("set mode 0 - value and select ctrl+A"); //
digitalWrite(LED_SELECT_PIN, HIGH);
digitalWrite(LED_INPUT_PIN, LOW);
digitalWrite(LED_TAB_PIN, LOW);
}
void set_input(void)
{
//Serial.println("set mode 1 - value and input"); //
digitalWrite(LED_SELECT_PIN, LOW);
digitalWrite(LED_INPUT_PIN, HIGH);
digitalWrite(LED_TAB_PIN, LOW);
}
void set_tab(void)
{
//Serial.println("set mode 2 - value and tabulator "); //
digitalWrite(LED_SELECT_PIN, LOW);
digitalWrite(LED_INPUT_PIN, LOW);
digitalWrite(LED_TAB_PIN, HIGH);
}