]> mj.ucw.cz Git - home-hw.git/blob - nucleo-test/Src/test.c
Morseovkový dekodér
[home-hw.git] / nucleo-test / Src / test.c
1 #include "main.h"
2
3 #include <stdarg.h>
4 #include <stdint.h>
5 #include <string.h>
6
7 typedef unsigned int uint;
8 typedef uint8_t byte;
9 typedef uint16_t u16;
10 typedef int16_t s16;
11
12 void debug_putc(int c)
13 {
14   if (c == '\n')
15     debug_putc('\r');
16   while (!LL_USART_IsActiveFlag_TXE(USART2))
17     ;
18   LL_USART_TransmitData8(USART2, c);
19 }
20
21 void debug_puts(const char *s)
22 {
23   while (*s)
24     debug_putc(*s++);
25 }
26
27 enum printf_flags {
28   PF_ZERO_PAD = 1,
29   PF_SIGNED = 2,
30   PF_NEGATIVE = 4,
31   PF_UPPERCASE = 8,
32   PF_LEFT = 16,
33 };
34
35 static void printf_string(const char *s, uint width, uint flags)
36 {
37   uint len = strlen(s);
38   uint pad = (len < width) ? width - len : 0;
39   char pad_char = (flags & PF_ZERO_PAD) ? '0' : ' ';
40
41   if (flags & PF_LEFT)
42     debug_puts(s);
43   while (pad--)
44     debug_putc(pad_char);
45   if (!(flags & PF_LEFT))
46     debug_puts(s);
47 }
48
49 static void printf_number(uint i, uint width, uint flags, uint base)
50 {
51   char buf[16];
52   char *w = buf + sizeof(buf);
53
54   if (flags & PF_SIGNED)
55     {
56       if ((int) i < 0)
57         {
58           i = - (int) i;
59           flags |= PF_NEGATIVE;
60         }
61     }
62
63   *--w = 0;
64   do
65     {
66       uint digit = i % base;
67       if (digit < 10)
68         *--w = '0' + digit;
69       else
70         *--w = ((flags & PF_UPPERCASE) ? 'A' : 'a') + digit - 10;
71       i /= base;
72     }
73   while (i);
74
75   if (flags & PF_NEGATIVE)
76     *--w = '-';
77
78   printf_string(w, width, flags);
79 }
80
81 void debug_printf(const char *fmt, ...)
82 {
83   va_list args;
84   va_start(args, fmt);
85
86   while (*fmt)
87     {
88       int c = *fmt++;
89       if (c != '%')
90         {
91           debug_putc(c);
92           continue;
93         }
94
95       uint width = 0;
96       uint flags = 0;
97
98       if (*fmt == '-')
99         {
100           fmt++;
101           flags |= PF_LEFT;
102         }
103
104       if (*fmt == '0')
105         {
106           fmt++;
107           flags |= PF_ZERO_PAD;
108         }
109
110       while (*fmt >= '0' && *fmt <= '9')
111         width = 10*width + *fmt++ - '0';
112
113       c = *fmt++;
114       switch (c)
115         {
116         case 'd':
117           printf_number(va_arg(args, int), width, flags | PF_SIGNED, 10);
118           break;
119         case 'u':
120           printf_number(va_arg(args, int), width, flags, 10);
121           break;
122         case 'X':
123           flags |= PF_UPPERCASE;
124           // fall-thru
125         case 'x':
126           printf_number(va_arg(args, int), width, flags, 16);
127           break;
128         case 's':
129           printf_string(va_arg(args, char *), width, flags);
130           break;
131         default:
132           debug_putc(c);
133           continue;
134         }
135     }
136
137   va_end(args);
138 }
139
140 void decide_set(int n){
141     switch (n){
142       case 1:
143         LL_GPIO_SetOutputPin(MEGGY1_GPIO_Port, MEGGY1_Pin);
144         break;
145       case 2:
146         LL_GPIO_SetOutputPin(MEGGY2_GPIO_Port, MEGGY2_Pin);
147         break;
148       case 3:
149         LL_GPIO_SetOutputPin(MEGGY3_GPIO_Port, MEGGY3_Pin);
150         break;
151       case 0:
152         LL_GPIO_SetOutputPin(MEGGY4_GPIO_Port, MEGGY4_Pin);
153         break;
154     }
155 }
156
157 void decide_reset(int n){
158     switch (n){
159       case 1:
160         LL_GPIO_ResetOutputPin(MEGGY1_GPIO_Port, MEGGY1_Pin);
161         break;
162       case 2:
163         LL_GPIO_ResetOutputPin(MEGGY2_GPIO_Port, MEGGY2_Pin);
164         break;
165       case 3:
166         LL_GPIO_ResetOutputPin(MEGGY3_GPIO_Port, MEGGY3_Pin);
167         break;
168       case 0:
169         LL_GPIO_ResetOutputPin(MEGGY4_GPIO_Port, MEGGY4_Pin);
170         break;
171     }
172 }
173
174 byte recognize(int n){
175     if (n<150) return 0;
176     else return 1;
177 }
178
179 void run_test(void)
180 {
181   uint cnt = 0;
182   uint cnt2 = 0;
183   static const char mtree[]="**ETIANMSURWDKGOHVF*L*PJBXCYZQ**";
184   int letter=1;
185
186   for (;;)
187     {
188       LL_GPIO_SetOutputPin(LD2_GPIO_Port, LD2_Pin);
189       cnt=0;
190       while (!LL_GPIO_IsInputPinSet(BLUE_BUTTON_GPIO_Port, BLUE_BUTTON_Pin)){
191           cnt++;
192           LL_mDelay(1);
193       }
194       LL_GPIO_ResetOutputPin(LD2_GPIO_Port, LD2_Pin);
195       // debug_printf(">>> %d\n", cnt);
196       letter=(letter<<1)+recognize(cnt);
197       cnt=0;
198       while (LL_GPIO_IsInputPinSet(BLUE_BUTTON_GPIO_Port, BLUE_BUTTON_Pin)){
199           cnt++;
200           LL_mDelay(1);
201           if (cnt==150) {
202               debug_putc(mtree[letter]);
203               letter=1;
204           }
205           if (cnt==1500) debug_putc(' ');
206           if (cnt==4000) debug_putc('\n');
207       }
208
209       cnt2++;
210       for (int i=0;i<4;i++){
211           if (cnt2&(1<<i)) decide_set((i+1)%4);
212           else decide_reset((i+1)%4);
213       }
214     }
215 }