|
| 1 | +/** |
| 2 | + * This example shows how to use the Lora Click wrapper of the LetMeCreate to |
| 3 | + * send or receive data. |
| 4 | + * |
| 5 | + * Depending on the mode selected, it either continuously sends a message every |
| 6 | + * second, or it waits for data and prints what it receives. |
| 7 | + * The user has to interrupt the program to exit it by pressing Ctrl+C. |
| 8 | + * |
| 9 | + * To run the example in send mode: |
| 10 | + * $ ./letmecreate_lora_example s |
| 11 | + * |
| 12 | + * To run the example in receive mode: |
| 13 | + * $ ./letmecreate_lora_example r |
| 14 | + * |
| 15 | + * The Lora Click must be inserted in Mikrobus 1 before running this program. |
| 16 | + */ |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +#include <signal.h> |
| 21 | +#include <stdio.h> |
| 22 | +#include <string.h> |
| 23 | +#include <unistd.h> |
| 24 | +#include <letmecreate/letmecreate.h> |
| 25 | + |
| 26 | +static volatile bool running = true; |
| 27 | + |
| 28 | +static void exit_program(int __attribute__ ((unused))signo) |
| 29 | +{ |
| 30 | + running = false; |
| 31 | +} |
| 32 | + |
| 33 | +static void receive(void) |
| 34 | +{ |
| 35 | + char buffer[16]; |
| 36 | + |
| 37 | + if (lora_click_receive((uint8_t*)buffer, sizeof(buffer) - 1) < 0) |
| 38 | + return; |
| 39 | + |
| 40 | + buffer[15] = '\0'; |
| 41 | + printf("Received \"%s\"\n", buffer); |
| 42 | +} |
| 43 | + |
| 44 | +static void send(unsigned int n) |
| 45 | +{ |
| 46 | + char buffer[255]; |
| 47 | + sprintf(buffer, "Hello, World! %u", n); |
| 48 | + |
| 49 | + if (lora_click_send((uint8_t*)buffer, strlen(buffer)) < 0) |
| 50 | + return; |
| 51 | + |
| 52 | + printf("Sent \"%s\"\n", buffer); |
| 53 | +} |
| 54 | + |
| 55 | +int main(int argc, char** argv) |
| 56 | +{ |
| 57 | + uint32_t n = 0; |
| 58 | + char mode; |
| 59 | + |
| 60 | + /* Set signal handler to exit program when Ctrl+c is pressed */ |
| 61 | + struct sigaction action = { |
| 62 | + .sa_handler = exit_program, |
| 63 | + .sa_flags = 0 |
| 64 | + }; |
| 65 | + sigemptyset(&action.sa_mask); |
| 66 | + sigaction (SIGINT, &action, NULL); |
| 67 | + |
| 68 | + if (argc < 2) { |
| 69 | + fprintf(stderr, "%s r|s\n", argv[0]); |
| 70 | + return -1; |
| 71 | + } |
| 72 | + |
| 73 | + mode = argv[1][0]; |
| 74 | + if (mode != 's' && mode != 'r') { |
| 75 | + fprintf(stderr, "Invalid mode.\n"); |
| 76 | + return -1; |
| 77 | + } |
| 78 | + |
| 79 | + printf("Press Ctrl+c to exit program.\n"); |
| 80 | + |
| 81 | + uart_init(); |
| 82 | + uart_select_bus(MIKROBUS_1); |
| 83 | + uart_set_baudrate(UART_BD_57600); |
| 84 | + lora_click_init(MIKROBUS_1, lora_click_get_default_configuration()); |
| 85 | + |
| 86 | + while (running) { |
| 87 | + if (mode == 's') { |
| 88 | + send(n); |
| 89 | + sleep(1); |
| 90 | + } |
| 91 | + else if (mode == 'r') |
| 92 | + receive(); |
| 93 | + |
| 94 | + ++n; |
| 95 | + } |
| 96 | + |
| 97 | + uart_release(); |
| 98 | + |
| 99 | + return 0; |
| 100 | +} |
0 commit comments