Plaquette
 
Loading...
Searching...
No Matches
pq_serial.h
1/*
2 * pq_serial.h
3 *
4 * (c) 2025 Sofian Audry :: info(@)sofianaudry(.)com
5 * (c) 2025 Thomas O Fredericks :: tof(@)t-o-f(.)info
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20#ifndef PQ_SERIAL_H_
21#define PQ_SERIAL_H_
22
23#include "PqCore.h"
24
25namespace pq {
26
27// Serial definitions.
28#define PLAQUETTE_DEFAULT_SERIAL Serial
29
30// Platform-dependent using SerialType = for.
31#if (defined(ARDUINO) && ARDUINO >= 100 )
32
33// Always match the concrete type of the global Serial object for this core.
34 using SerialType = decltype(Serial);
35
36#elif defined(ARDUINO_ARCH_AVR)
37 #if defined(USBCON)
38 using SerialType = Serial_; // Leonardo, Micro (USB CDC)
39 #else
40 using SerialType = HardwareSerial; // Uno, Mega, Nano
41 #endif
42#elif defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
43 using SerialType = HardwareSerial;
44#elif defined(ARDUINO_ARCH_SAMD)
45 using SerialType = Uart;
46#elif defined(EPOXY_DUINO)
47 using SerialType = StdioSerial;
48#else
49 using SerialType = HardwareSerial; // Fallback
50#endif
51
53static inline void beginSerial(SerialType& serial, unsigned long baudrate) {
54 // Wait for last transmitted data to be sent.
55 serial.flush();
56
57 // Start serial with baudrate.
58 if (baudrate > 0) {
59 serial.begin(baudrate);
60 }
61
62 // Wait until serial is ready.
63 while (!serial) {}
64}
65
66}
67
68#endif