examples: added receive and send examples
[libsi24] / examples / send-with-ack.c
1 #include <stdarg.h>
2 #include <stdint.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include "libsi24.h"
8
9 int example_spi_write_read(unsigned char* data, size_t sz)
10 {
11         (void)data;
12         (void)sz;
13         return 0;
14 }
15
16 void example_ce(unsigned val)
17 {
18         (void)val;
19 }
20
21 void example_event_handler(si24_t* si, si24_event_t* e)
22 {
23         (void)si;
24
25         switch (e->type) {
26         case EV_RX_COMPLETE:
27                 break;
28         case EV_TX_COMPLETE:
29                 break;
30         case EV_TX_FULL:
31                 break;
32         case EV_RX_EMPTY:
33                 break;
34         case EV_ERR_TIMEOUT:
35                 break;
36         case EV_ERR_BUS:
37                 break;
38         case EV_ERR_MAX_RETRIES:
39                 break;
40         case EV_ERR_CRC:
41                 break;
42         case EV_ERR_CONFIG:
43                 break;
44         default:
45                 break;
46         }
47 }
48
49 int main(void)
50 {
51         const unsigned char buf[] = "SAMPLE TEST MESSAGE!";
52
53         si24_ioctl_t ctl = {
54                 .write_and_read = example_spi_write_read,
55                 .chip_enable = example_ce,
56         };
57
58         const si24_opts_t opts = {
59                 .mode = SEND_MODE,
60                 .enable_ack = 1,
61                 .non_blocking = 0,
62                 .enable_crc = 1,
63                 .enable_dynpd = 1,
64                 .crc = TWO_BYTE,
65                 .ioctl = &ctl,
66                 .speed = MBPS2,
67                 .txpwr = PLUS4DB,
68                 .payload = 5,
69                 .timeout = 5,
70                 .retries = 5,
71                 .mac_addr = { 0xAB, 0xCD, 0xEF, 0xFF, 0xFF }
72         };
73
74         struct si24_t* si = si24_init(&opts, example_event_handler);
75
76         int bytes_sent = si24_send(si, buf, sizeof(buf));
77         while (bytes_sent != sizeof(buf)) {
78                 if (bytes_sent == -1)
79                         break;
80                 bytes_sent += si24_send(si, buf + bytes_sent, sizeof(buf) - bytes_sent);
81         }
82 }