Mascobot / pandemic-ventilator-2.0

Open Source Pandemic Ventilator with Raspberry Pi and Arduino
238 stars 49 forks source link

SerialEvent call in loop #6

Open antoinepetty opened 4 years ago

antoinepetty commented 4 years ago

I think SerialEvent() is called by the Arduino firmware at the start of each loop anyway. I don't think you need to explicitly call this function anywhere.

antoinepetty commented 4 years ago

https://www.arduino.cc/reference/en/language/functions/communication/serial/serialevent/ https://www.arduino.cc/en/Tutorial/SerialEvent

bschwind commented 4 years ago

They have a serialEvent function defined in the source, is that different from the Arduino source for the function?

https://github.com/Mascobot/pandemic-ventilator-2.0/blob/244b9f619848143c57e61b739bd51ed6c3813b82/code/arduino/vent/vent.ino#L227-L239

antoinepetty commented 4 years ago

From my experience I haven't ever needed an explicit call for this function when it was defined in the source. Try running the example and you'll see what I mean: https://www.arduino.cc/en/Tutorial/SerialEvent

The documentation says it's called when new data is received, but this thread says it's called at the end of each loop: https://forum.arduino.cc/index.php?topic=449625.0

bschwind commented 4 years ago

Ah interesting, I didn't know it was somewhat of a "magic" function.

https://github.com/arduino/ArduinoCore-avr/blob/0f3d4da614d5a195e03d70741031092a002e2d33/cores/arduino/main.cpp#L47

https://github.com/arduino/ArduinoCore-avr/blob/0f3d4da614d5a195e03d70741031092a002e2d33/cores/arduino/HardwareSerial.cpp#L64-L67

antoinepetty commented 4 years ago

Thanks for those links, it's nice to see what happens under the hood!