forked from kasshinokun/AC_II
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
149 lines (130 loc) · 3.92 KB
/
main.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// C++ code
//Algoritmo desenvolvido por Welbert em 02-10-2024
//Adaptado para Arduino por Gabriel em 05-10-2024
//Revisao 1_05_10_2024
#include <string.h>
#include <math.h>
typedef struct {
char key;
int value;
}dictionary_1;
typedef struct {
int key;
char value;
}dictionary_2;
dictionary_1 char_to_int[]={{'0',0},{'1',1},{'2',2},{'3',3},{'4',4},{'5',5},{'6',6},{'7',7},
{'8',8},{'9',9},{'A',10},{'a',10},{'B',11},{'b',11},{'C',12},
{'c',12},{'D',13},{'d',13},{'E',14},{'e',14},{'F',15},{'f',15}};
dictionary_2 int_to_char[]={{0,'0'},{1,'1'},{2,'2'},{3,'3'},{4,'4'},{5,'5'},{6,'6'},
{7,'7'},{8,'8'},{9,'9'},{10,'A'},{11,'B'},{12,'C'},{13,'D'},{14,'E'},{15,'F'}};
bool valide_string(String str){
bool resp=false;
char* s = convert_char_array(str);
if (str.length()==1){
// Displaying the values stored in the array of
// structures
for (int i = 0; i < (sizeof(char_to_int) / sizeof(*char_to_int)); i++) {
if (char_to_int[i].key==s[0]){
resp=true;
delete[] s;
break;
}
}
}
return resp;
}
//Converte o caracter para decimal baseando-se em heexadecimal
unsigned long hex2dec(String hex)
{
unsigned long result = 0;
for (int i=0; i<hex.length(); i++) {
if (hex[i]>=48 && hex[i]<=57)
{
result += (hex[i]-48)*pow(16,hex.length()-i-1);
} else if (hex[i]>=65 && hex[i]<=70) {
result += (hex[i]-55)*pow(16,hex.length( )-i-1);
} else if (hex[i]>=97 && hex[i]<=102) {
result += (hex[i]-87)*pow(16,hex.length()-i-1);
}
}
return result;//retorna o valor
}
//Checagem do tamanho da String
int check_string(String str) {
if (str.length() % 3 == 0 && str.length() % 96 == 0) {
return 1;
} else {
return 0;
}
}
char* convert_char_array(String str) {
if (check_string(str) == 1) {
// Aloca o array na heap para evitar problemas de escopo
char* arr= new char[str.length() + 1];
strcpy(arr, str.c_str());
return arr; // Retorna o ponteiro para o array
} else {
return nullptr; // Retorna nullptr se a condicao nao for atendida
}
}
void execute_operations(String str) {
char* arr= convert_char_array(str);
if (arr != nullptr) {
// Executa operacoes com o array de caracteres
Serial.println("Array de caracteres: ");
Serial.print(arr);
Serial.println ();
delete[] arr;
Serial.println();
}else{
Serial.println("Erro: A string nao atende os condicoes.");
}
}
//Funcoes de portas logicas (vazias por enquanto)
int porta_not(char a) {
if (valide_string(String()+a)==1){
return ~hex2dec(String()+a);
}
}
int porta_not_2(char a) {
if (valide_string(String()+a)==1){
int x=hex2dec(String()+a);
return (x>0|x!=0?~x+1:~x+2);
}
}
int porta_and(char a, char b) {
if (valide_string(String()+a)==1 && valide_string(String()+b)==1){
return (hex2dec(String()+a) & hex2dec(String()+b));
}
}
int porta_or(char a, char b) {
if (valide_string(String()+a)==1 && valide_string(String()+b)==1){
return (hex2dec(String()+a) | hex2dec(String()+b));
}
}
int porta_xor(char a, char b) {
if (valide_string(String()+a)==1 && valide_string(String()+b)==1){
return (hex2dec(String()+a) ^ hex2dec(String()+b));
}
}
int porta_nand(char a, char b) {
if (valide_string(String()+a)==1 && valide_string(String()+b)==1){
return ~(hex2dec(String()+a) & hex2dec(String()+b));
}
}
int porta_nor(char a, char b) {
if (valide_string(String()+a)==1 && valide_string(String()+b)==1){
return ~(hex2dec(String()+a) | hex2dec(String()+b));
}
}
int porta_xnor(char a, char b) {
if (valide_string(String()+a)==1 && valide_string(String()+b)==1){
return ~(hex2dec(String()+a) ^ hex2dec(String()+b));
}
}
void setup()
{
}
void loop()
{
}