-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartB.c
218 lines (167 loc) · 3.97 KB
/
PartB.c
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// #include<string.h>
// #include<stdio.h>
// #include<stdlib.h>
#include<xmc_gpio.h>
#include<xmc_common.h>
#define TICKS_PER_SECOND 1000
#define TICKS_DOT 100
#define TICKS_DASH 300
#define TICKS_LETTER_SPACE 200
#define TICKS_WORD_SPACE 700
#define TICKS_SYMBOL_SPACE 100
#define LED1 P1_1
#define LED2 P1_0
#define GPIO_BUTTON1 XMC_GPIO_PORT1, 14
#define GPIO_BUTTON2 XMC_GPIO_PORT1, 15
void delay(uint32_t ticks_in_ms)
{
delay_ticks=ticks_in_ms;
while(delay_ticks>0);
}
// Words are seperated by a space equivalent to 7 dots
// Letters are seperated by a space of 3 dots
// 100ms space between dots and dashes
static const char *alpha[] = {
".-", //A
"-...", //B
"-.-.", //C
"-..", //D
".", //E
"..-.", //F
"--.", //G
"....", //H
"..", //I
".---", //J
"-.-", //K
".-..", //L
"--", //M
"-.", //N
"---", //O
".--.", //P
"--.-", //Q
".-.", //R
"...", //S
"-", //T
"..-", //U
"...-", //V
".--", //W
"-..-", //X
"-.--", //Y
"--..", //Z
};
static const char *num[] = {
"-----", //0
".----", //1
"..---", //2
"...--", //3
"....-", //4
".....", //5
"-....", //6
"--...", //7
"---..", //8
"----.", //9
};
char textString[] = "I CAN MORSE";
char* char2morse(char str)
{
// char* morse = (char*) malloc((strlength*6) * sizeof(char));
// int morselen=0;
// char letterBreak = '#';
// char wordBreak = '!';
char temp;
char space_char[] ="**";
temp = tolower(str);
if (temp == " ")
{
return space_char;
}
else if (isalpha(temp))
{
// printf("%s",alpha[(int)str[i]]);
// printf("%c %d %s\n",temp,(int) temp % 97,alpha[(int) temp % 97]);
return alpha[(int) temp % 97];
}
else if (isdigit(temp))
{
// printf("%s",alpha[(int)str[i]]);
// printf("%c %d %s\n",temp,(int) temp %48,num[(int) temp %48]);
return num[(int) temp %48];
}
return space_char;
}
void runMorse()
{
for(int i=0;i<strlen(textString);i++)
{
// printf(char2morse(textString[i]));
memcpy(morse_str,char2morse(textString[i]),5);
morse_str_len = strlen(morse_str);
// printf(morse_str);
if (morse_str[0] != '*')
{
for(int j=0;j<morse_str_len;j++)
{
if (morse_str[j] == '.')
{
XMC_GPIO_SetOutputHigh(LED1);
delay(TICKS_DOT);
XMC_GPIO_SetOutputLow(LED1);
delay(TICKS_SYMBOL_SPACE);
}
else if (morse_str[j] == '-')
{
XMC_GPIO_SetOutputHigh(LED1);
delay(TICKS_DASH);
XMC_GPIO_SetOutputLow(LED1);
delay(TICKS_SYMBOL_SPACE);
}
if (j+1 == morse_str_len)
{
XMC_GPIO_SetOutputLow(LED1);
delay(TICKS_LETTER_SPACE);
break;
}
}
}
else if (morse_str[0] == '*')
{
XMC_GPIO_SetOutputLow(LED1);
delay(TICKS_WORD_SPACE);
}
}
}
const XMC_GPIO_CONFIG_t in_config = \
{.mode=XMC_GPIO_MODE_INPUT_TRISTATE,\
.output_level=XMC_GPIO_OUTPUT_LEVEL_LOW,\
.output_strength=XMC_GPIO_OUTPUT_STRENGTH_STRONG_SHARP_EDGE
};
uint32_t delay_ticks = 0;
void SysTick_Handler(void)
{
static bool button1_status,button2_status;
static uint32_t buttonPress_ticks = 0;
button1_status = 0;
button2_status = 0;
if (delay_ticks>0){
delay_ticks--;
}
}
int main(void) {
XMC_GPIO_CONFIG_t config;
config.mode = XMC_GPIO_MODE_OUTPUT_PUSH_PULL;
config.output_level = XMC_GPIO_OUTPUT_LEVEL_LOW;
config.output_strength = XMC_GPIO_OUTPUT_STRENGTH_MEDIUM;
XMC_GPIO_Init(GPIO_BUTTON1, &in_config);
// XMC_GPIO_Init(GPIO_BUTTON2, &in_config);
XMC_GPIO_Init(LED1, &config);
/* System timer configuration */
SysTick_Config(SystemCoreClock / TICKS_PER_SECOND);
char morse_str[5];
int morse_str_len;
delay_ticks++;
while(1){
while(XMC_GPIO_GetInput(GPIO_BUTTON1) == 0)
{
}
}
}