arduino-audio-tools
NoArduino.h
Go to the documentation of this file.
1 #pragma once
13 #include <stdint.h>
14 #include <algorithm> // std::max
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <ctype.h>
18 #include <string.h>
19 #include <chrono>
20 #include "AudioConfig.h"
21 
22 #define IS_NOARDUINO
23 
24 #ifndef PSTR
25 # define PSTR(fmt) fmt
26 #endif
27 
28 #ifndef PI
29 # define PI 3.14159265359f
30 #endif
31 
32 #ifndef INPUT
33 # define INPUT 0x0
34 #endif
35 
36 #ifndef OUTPUT
37 # define OUTPUT 0x1
38 #endif
39 
40 #ifndef INPUT_PULLUP
41 # define INPUT_PULLUP 0x2
42 #endif
43 
44 #ifndef HIGH
45 #define HIGH 0x1
46 # endif
47 #ifndef LOW
48 # define LOW 0x0
49 #endif
50 
51 
52 using namespace std;
53 
54 enum PrintCharFmt {DEC, HEX};
55 
56 namespace audio_tools {
57 
58 class Print {
59 public:
60 #ifndef DOXYGEN
61  virtual size_t write(uint8_t ch){
62  // not implememnted: to be overritten
63  return 0;
64  }
65 
66  virtual size_t write(const char *str) {
67  return write((const uint8_t *)str, strlen(str));
68  }
69 
70  virtual size_t write(const char *buffer, size_t size) {
71  return write((const uint8_t *)buffer, size);
72  }
73 
74  virtual int print(const char* msg){
75  int result = strlen(msg);
76  return write(msg, result);
77  }
78 
79  virtual int println(const char* msg="") {
80  int result = print(msg);
81  write('\n');
82  return result+1;
83  }
84 
85  virtual int print(int number){
86  char buffer[80];
87  snprintf(buffer,80,"%d", number);
88  return print(buffer);
89  }
90 
91  virtual int print(char c, PrintCharFmt spec){
92  char result[5];
93  switch(spec){
94  case DEC:
95  snprintf(result, 3,"%c", c);
96  return print(result);
97  case HEX:
98  snprintf(result, 3,"%x", c);
99  return print(result);
100  }
101  return -1;
102  }
103 
104 #endif
105 
106  virtual size_t write(const uint8_t *data, size_t len){
107  if (data == nullptr) return 0;
108  for (size_t j=0;j<len;j++){
109  write(data[j]);
110  }
111  return len;
112  }
113 
114 
115  virtual int availableForWrite() { return 1024; }
116 
117 
118  virtual void flush() { /* Empty implementation for backward compatibility */ }
119 
120  protected:
121  int _timeout = 10;
122 
123 };
124 
125 class Stream : public Print {
126 public:
127  virtual int available(){return 0;}
128  virtual size_t readBytes(uint8_t* data, size_t len) {return 0;}
129 #ifndef DOXYGEN
130  virtual int read(){return -1;}
131  virtual int peek(){return -1;}
132  virtual void setTimeout(size_t t){}
133 #endif
134  operator bool(){
135  return true;
136  }
137 };
138 
139 class Client : public Stream {
140 public:
141  void stop() {};
142  virtual int read(uint8_t* buffer, size_t len) { return 0;};
143  virtual int read() { return 0; };
144  bool connected() { return false;};
145  bool connect(const char* ip, int port) {return false;}
146  virtual operator bool() { return false;}
147 };
148 
149 class HardwareSerial : public Stream {
150 public:
151  size_t write(uint8_t ch) override {
152  return putchar(ch);
153  }
154  virtual operator bool() {
155  return true;
156  }
157 };
158 
159 static HardwareSerial Serial;
160 
162 inline long map(long x, long in_min, long in_max, long out_min, long out_max) {
163  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
164 }
165 
166 // #ifndef DESKTOP_MILLIS_DEFINED
167 
168 // /// Waits for the indicated milliseconds
169 // extern void delay(uint32_t ms);
170 
171 // /// Returns the milliseconds since the start
172 // extern uint32_t millis();
173 
174 // /// Waits for the indicated milliseconds
175 // extern void delayMicroseconds(uint32_t ms);
176 
177 // /// Returns the milliseconds since the start
178 // extern uint32_t micros();
179 
180 // #else
181 
182 // extern "C" {
183 
184 // /// Waits for the indicated milliseconds
185 // extern void delay(uint32_t ms);
186 
187 // /// Returns the milliseconds since the start
188 // extern uint32_t millis();
189 
190 // /// Waits for the indicated milliseconds
191 // extern void delayMicroseconds(uint32_t ms);
192 
193 // /// Returns the milliseconds since the start
194 // extern uint32_t micros();
195 
196 // }
197 
198 //#endif
199 
200 
201 } // namespace
202 
203 #if defined(ESP32)
204 #include "driver/gpio.h"
206 int digitalRead(int pin) {
207  printf("digitalRead:%d\n", pin);
208  return gpio_get_level((gpio_num_t)pin);
209 }
210 
211 void pinMode(int pin, int mode) {
212  gpio_num_t gpio_pin=(gpio_num_t)pin;
213  printf("pinMode(%d,%d)\n", pin, mode);
214 
215  gpio_reset_pin(gpio_pin);
216  switch (mode){
217  case INPUT:
218  gpio_set_direction(gpio_pin, GPIO_MODE_INPUT);
219  break;
220  case OUTPUT:
221  gpio_set_direction(gpio_pin, GPIO_MODE_OUTPUT);
222  break;
223  case INPUT_PULLUP:
224  gpio_set_direction(gpio_pin, GPIO_MODE_INPUT);
225  gpio_set_pull_mode(gpio_pin, GPIO_PULLUP_ONLY);
226  break;
227  default:
228  gpio_set_direction(gpio_pin, GPIO_MODE_INPUT_OUTPUT);
229  break;
230  }
231 }
232 
233 #endif
234 
235 //using namespace audio_tools;
236 
237 
238 
239 
int digitalRead(int pin)
e.g. for AudioActions
Definition: NoArduino.h:206
Definition: NoArduino.h:139
Definition: NoArduino.h:149
Definition: NoArduino.h:58
Definition: NoArduino.h:125
void stop()
Public generic methods.
Definition: AudioRuntime.h:27
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823
long map(long x, long in_min, long in_max, long out_min, long out_max)
Maps input to output values.
Definition: NoArduino.h:162