OpenCores
URL https://opencores.org/ocsvn/neo430/neo430/trunk

Subversion Repositories neo430

[/] [neo430/] [trunk/] [neo430/] [sw/] [example/] [morse_translator/] [main.c] - Blame information for rev 198

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 198 zero_gravi
// #################################################################################################
2
// #  < Morse code translator >                                                                    #
3
// # ********************************************************************************************* #
4
// # Translates a text string into Morse code. The encode can only encode letters.                 #
5
// # Output via GPIO.output(0) (bootloader status LED).                                            #
6
// # ********************************************************************************************* #
7
// # BSD 3-Clause License                                                                          #
8
// #                                                                                               #
9
// # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
10
// #                                                                                               #
11
// # Redistribution and use in source and binary forms, with or without modification, are          #
12
// # permitted provided that the following conditions are met:                                     #
13
// #                                                                                               #
14
// # 1. Redistributions of source code must retain the above copyright notice, this list of        #
15
// #    conditions and the following disclaimer.                                                   #
16
// #                                                                                               #
17
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
18
// #    conditions and the following disclaimer in the documentation and/or other materials        #
19
// #    provided with the distribution.                                                            #
20
// #                                                                                               #
21
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
22
// #    endorse or promote products derived from this software without specific prior written      #
23
// #    permission.                                                                                #
24
// #                                                                                               #
25
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
26
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
27
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
28
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
29
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
30
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
31
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
32
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
33
// # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
34
// # ********************************************************************************************* #
35
// # The NEO430 Processor - https://github.com/stnolting/neo430                                    #
36
// #################################################################################################
37
 
38
 
39
// Libraries
40
#include <stdint.h>
41
#include <neo430.h>
42
 
43
// Configuration
44
#define MAX_STRING_LENGTH 128 // max length of plain morse text
45
#define LED_PIN 0 // GPIO output pin #0
46
#define TIME_PRSC 192
47
#define BAUD_RATE 19200
48
 
49
// Global vars
50
uint32_t time_base;
51
 
52
// Prototypes
53
void send_morse(const char *s);
54
 
55
// Morse alphabet (ASCII order)
56
const char morse_code[][7] = {
57
  "--..--", // ,
58
  "-....-", // -
59
  ".-.-.-", // .
60
  "-..-.",  // /
61
 
62
  "-----",  // 0
63
  ".----",  // 1
64
  "..---",  // 2
65
  "...--",  // 3
66
  "....-",  // 4
67
  ".....",  // 5
68
  "-....",  // 6
69
  "--...",  // 7
70
  "---..",  // 8
71
  "----.",  // 9
72
 
73
  "---...", // :
74
  "-.-.-.", // ;
75
  "",       // < (not implemented)
76
  "-...-",  // =
77
  "",       // > (not implemented)
78
  "..--..", // ?
79
  "",       // @ (not implemented)
80
 
81
  ".-",     // A
82
  "-...",   // B
83
  "-.-.",   // C
84
  "-..",    // D
85
  ".",      // E
86
  "..-.",   // F
87
  "--.",    // G
88
  "....",   // H
89
  "..",     // I
90
  ".---",   // J
91
  "-.-",    // K
92
  ".-..",   // L
93
  "--",     // M
94
  "-.",     // N
95
  "---",    // O
96
  ".--.",   // P
97
  "--.-",   // Q
98
  ".-.",    // R
99
  "...",    // S
100
  "-",      // T
101
  "..-",    // U
102
  "...-",   // V
103
  ".--",    // W
104
  "-..-",   // X
105
  "-.--",   // Y
106
  "--..",   // Z
107
};
108
 
109
/* ------------------------------------------------------------
110
 * INFO Main function
111
 * ------------------------------------------------------------ */
112
int main(void) {
113
 
114
  char buffer[MAX_STRING_LENGTH];
115
 
116
  // setup UART
117
  neo430_uart_setup(BAUD_RATE);
118
 
119
  // configure time base
120
  uint32_t clock = CLOCKSPEED_32bit;
121
  time_base = 0;
122
  while (clock >= TIME_PRSC) {
123
    clock = clock - TIME_PRSC;
124
    time_base++; // time base for a 'Dit'
125
  }
126
 
127
  neo430_uart_br_print("\n--- Morse code translator ---\n");
128
  neo430_uart_br_print("Enter a string to translate it to Morse code.\n");
129
  neo430_uart_br_print("Output via high-active LED at GPIO.out(0) (bootloader status LED).\n");
130
 
131
  // check if GPIO unit was synthesized, exit if no GPIO is available
132
  if (!(SYS_FEATURES & (1<<SYS_GPIO_EN))) {
133
    neo430_uart_br_print("Error! No GPIO unit synthesized!");
134
    return 1;
135
  }
136
 
137
  neo430_gpio_port_set(0); // LED off
138
 
139
  while (1) {
140
    // get string
141
    neo430_uart_br_print("\nEnter text: ");
142
    uint16_t length = neo430_uart_scan(buffer, MAX_STRING_LENGTH, 1);
143
    neo430_uart_br_print("\nSending: ");
144
 
145
    // encode each letter
146
    uint16_t i = 0;
147
    for (i=0; i<length; i++) {
148
      char c = buffer[i];
149
 
150
      // cast to capital letters
151
      if ((c >= 'a') && (c <= 'z'))
152
        c -= 32;
153
 
154
      // in valid alphabet?
155
      if ((c >= ',') && (c <= 'Z')) {
156
        uint8_t index = c - ',';
157
        neo430_uart_br_print(" ");
158
        send_morse(morse_code[index]);
159
 
160
        uint32_t time = time_base * 3; // inter-letter pause
161
        while(time--)
162
          asm volatile ("nop");
163
 
164
      }
165
      // user abort?
166
      if (neo430_uart_char_received() != 0) {
167
        neo430_uart_br_print("\nAborted.");
168
        break;
169
      }
170
    }
171
 
172
  }
173
 
174
  return 0;
175
}
176
 
177
 
178
/* ------------------------------------------------------------
179
 * INFO Send morse code via LED pin
180
 * PARAM *s pointer to source morse symbol string
181
 * ------------------------------------------------------------ */
182
void send_morse(const char *s){
183
 
184
  char c = 0;
185
  uint32_t time = 0;
186
 
187
  while ((c = *s++)) {
188
 
189
    neo430_gpio_pin_set(LED_PIN); // LED on
190
 
191
    if (c == '.')
192
      time = time_base;
193
    else if (c == '-')
194
      time = time_base * 3;
195
    else
196
      time = 0;
197
 
198
    neo430_uart_putc(c);
199
 
200
    // wait
201
    while(time--)
202
      asm volatile ("nop");
203
 
204
    neo430_gpio_pin_clr(LED_PIN); // LED off
205
 
206
    // inter-symbol pause
207
    time = time_base;
208
    while(time--)
209
      asm volatile ("nop");
210
  }
211
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.