Add example for arduino board.
[bertos.git] / boards / arduino / examples / aprs / main.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Francesco Sacchi <batt@develer.com>
34  * \author Luca Ottaviano <lottaviano@develer.com>
35  * \author Daniele Basile <asterix@develer.com>
36  *
37  * \brief Arduino APRS radio demo.
38  *
39  * This example shows how to read and decode APRS radio packets.
40  * It uses the following modules:
41  * afsk
42  * ax25
43  * ser
44  *
45  * You will see how to use a serial port to output messages, init the afsk demodulator and
46  * how to parse input messages using ax25 module.
47  */
48
49 #include <cpu/irq.h>
50 #include <cfg/debug.h>
51
52 #include <net/afsk.h>
53 #include <net/ax25.h>
54
55 #include <drv/ser.h>
56 #include <drv/timer.h>
57
58 #include <stdio.h>
59 #include <string.h>
60
61 static Afsk afsk;
62 static AX25Ctx ax25;
63 static Serial ser;
64
65 #define ADC_CH 0
66
67 /*
68  * Print on console the message that we have received.
69  */
70 static void message_callback(struct AX25Msg *msg)
71 {
72         kfile_printf(&ser.fd, "\n\nSRC[%.6s-%d], DST[%.6s-%d]\r\n", msg->src.call, msg->src.ssid, msg->dst.call, msg->dst.ssid);
73
74         for (int i = 0; i < msg->rpt_cnt; i++)
75                 kfile_printf(&ser.fd, "via: [%.6s-%d]\r\n", msg->rpt_lst[i].call, msg->rpt_lst[i].ssid);
76
77         kfile_printf(&ser.fd, "DATA: %.*s\r\n", msg->len, msg->info);
78 }
79
80 static void init(void)
81 {
82         IRQ_ENABLE;
83         kdbg_init();
84         timer_init();
85
86         /*
87          * Init afsk demodulator. We need to implement the macros defined in hw_afsk.h, which
88          * is the hardware abstraction layer.
89          * We do not need transmission for now, so we set transmission DAC channel to 0.
90          */
91         afsk_init(&afsk, ADC_CH, 0);
92         /*
93          * Here we initialize AX25 context, the channel (KFile) we are going to read messages
94          * from and the callback that will be called on incoming messages.
95          */
96         ax25_init(&ax25, &afsk.fd, message_callback);
97
98         /* Initialize serial port, we are going to use it to show APRS messages*/
99         ser_init(&ser, SER_UART0);
100         ser_setbaudrate(&ser, 115200L);
101 }
102
103 static AX25Call path[] = AX25_PATH(AX25_CALL("apzbrt", 0), AX25_CALL("nocall", 0), AX25_CALL("wide1", 1), AX25_CALL("wide2", 2));
104
105 #define APRS_MSG    ">Test BeRTOS APRS http://www.bertos.org"
106
107 int main(void)
108 {
109         init();
110         ticks_t start = timer_clock();
111
112         while (1)
113         {
114                 /*
115                  * This function will look for new messages from the AFSK channel.
116                  * It will call the message_callback() function when a new message is received.
117                  * If there's nothing to do, this function will call cpu_relax()
118                  */
119                 ax25_poll(&ax25);
120
121
122                 /* Send out message every 15sec */
123                 if (timer_clock() - start > ms_to_ticks(15000L))
124                 {
125                         start = timer_clock();
126                         ax25_sendVia(&ax25, path, countof(path), APRS_MSG, sizeof(APRS_MSG));
127                 }
128         }
129         return 0;
130 }