Ultra lightweight serial support for Arduino, properly supporting incoming interrupts
#include <PicoSerial.h>
#define BAUD 250000
uint32_t setup() {
gotBAUD=PicoSerial.begin(BAUD,myIn); // myIn is your code which gets called when bytes arrive
PicoSerial.print("The BAUD I really got was:");
PicoSerial.println(gotBAUD); // If you ask for 115200 on an 8mhz CPU, you really get 111111 baud...
}
void loop() {
// do stuff - usually checking your read buffer here...
}
void myIn(int c) { // this is an ISR - it gets called when new data comes in. buffer and exit this as fast as you can; do not do anything else in here.
myBuffer[bufferIndex++]=c;
}
uint32_t begin(BAUD,rxCallBackFunction); // Sets baud rate, and lets you tell PicoSerial which of your functions you want to call when data is ready. Returns baudrate
int read() // Unbuffered read. returns -1 if no character is available or an available character. Do not use if you supplied an rxCallBackFunction prior
size_t write(uint8_t b) // Unbuffered write; param[in] b byte to write. return 1
boolean canWrite(); // true if we can do a nonblocking write next
This code was adapted from PetitSerial.h, which is a small Serial class in the spirit of Petit FatFs.
- Open a terminal (commandline, bash shell, whatever)
- Change into your Arduino folder
cd /Applications/Arduino.app/Contents/Java/libraries/
(or)
cd ~/Arduino/libraries/
- grab and install this code
git clone https://github.com/gitcnd/PicoSerial.git
- restart your arduino IDE
- Choose File => Examples => PicoSerial => PicoSerial_example
- Hit the "build" button and enjoy!
(Method 2) - see https://www.arduino.cc/en/Guide/Libraries
- Download the ZIP of this repo: https://github.com/gitcnd/PicoSerial/archive/master.zip
- In your IDE, select Sketch -> Include Library -> Add .ZIP Library
- Choose File => Examples => PicoSerial => PicoSerial_example
- Hit the "build" button and enjoy!