Blame view

RIOT/sys/arduino/serialport.cpp 3.19 KB
a752c7ab   elopes   add first test an...
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
  /*
   * Copyright (C) 2015 Freie Universitรคt Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser General
   * Public License v2.1. See the file LICENSE in the top level directory for more
   * details.
   */
  
  /**
   * @ingroup     sys_arduino
   * @{
   *
   * @file
   * @brief       Implementation of the Arduino 'Serial' interface
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   *
   * @}
   */
  extern "C" {
  #include <stdint.h>
  #include <string.h>
  #include <stdio.h>
  
  #include "irq.h"
  }
  
  #include "serialport.hpp"
  
  void rx_cb(void *arg, uint8_t c)
  {
      ringbuffer_t *buf = (ringbuffer_t *)arg;
      ringbuffer_add_one(buf, (char)c);
  }
  
  SerialPort::SerialPort(uart_t dev)
  {
      this->dev = dev;
  }
  
  int SerialPort::available(void)
  {
      return (int)rx_buf.avail;
  }
  
  void SerialPort::begin(long baudrate)
  {
      /* this clears the contents of the ringbuffer... */
      ringbuffer_init(&rx_buf, rx_mem, SERIAL_RX_BUFSIZE);
      uart_init(dev, (uint32_t)baudrate, rx_cb, (void *)&rx_buf);
  }
  
  void SerialPort::end(void)
  {
      uart_poweroff(dev);
  }
  
  size_t SerialPort::print(int val)
  {
      return print(val, DEC);
  }
  
  size_t SerialPort::print(int val, SerialFormat format)
  {
      char buf[64];
      size_t len;
      switch (format) {
          case BIN:
              /* TODO */
              return 0;
          case OCT:
              len = sprintf(buf, "%o", (unsigned)val);
              break;
          case DEC:
              len = sprintf(buf, "%i", val);
              break;
          case HEX:
              len = sprintf(buf, "%x", (unsigned)val);
              break;
          default:
              return 0;
      }
      write(buf, len);
      return len;
  }
  
  size_t SerialPort::print(float val)
  {
      return print(val, 2);
  }
  
  size_t SerialPort::print(float val, int format)
  {
      char buf[64];
      size_t len = sprintf(buf, "%.*f", format, (double)val);
      write(buf, len);
      return len;
  }
  
  size_t SerialPort::print(char val)
  {
      return (size_t)write((int)val);
  }
  
  size_t SerialPort::print(const char *val)
  {
      return (size_t)write(val);
  }
  
  size_t SerialPort::println(int val)
  {
      size_t res = print(val);
      write("\r\n");
      return (res + 2);
  }
  
  size_t SerialPort::println(int val, SerialFormat format)
  {
      size_t res = print(val, format);
      write("\r\n");
      return (res + 2);
  }
  
  size_t SerialPort::println(float val)
  {
      size_t res = print(val);
      write("\r\n");
      return (res + 2);
  }
  
  size_t SerialPort::println(float val, int format)
  {
      size_t res = print(val, format);
      write("\r\n");
      return (res + 2);
  }
  
  size_t SerialPort::println(char val)
  {
      size_t res = print(val);
      write("\r\n");
      return (res + 2);
  }
  
  size_t SerialPort::println(const char *val)
  {
      size_t res = print(val);
      write("\r\n");
      return (res + 2);
  }
  
  int SerialPort::read(void)
  {
      int res = -1;
  
      irq_disable();
      if (rx_buf.avail > 0) {
          res = ringbuffer_get_one(&rx_buf);
      }
      irq_enable();
  
      return res;
  }
  
  int SerialPort::write(int val)
  {
      uart_write(dev, (uint8_t *)&val, 1);
      return 1;
  }
  
  int SerialPort::write(const char *str)
  {
      uart_write(dev, (uint8_t *)str, strlen(str));
      return strlen(str);
  }
  
  int SerialPort::write(char *buf, int len)
  {
      uart_write(dev, (uint8_t *)buf, len);
      return len;
  }