|
| 1 | +/* |
| 2 | + RingBufInterruptsExample.ino - Interrupt example, to demonstrate the power of a ring buffer with asynchronous io |
| 3 | + Created by D. Aaron Wisner (daw268@cornell.edu) |
| 4 | + January 17, 2015. |
| 5 | + Released into the public domain. |
| 6 | +
|
| 7 | + Trigger an interrupt by toggling EVENT_PIN, this will push an Event struct into the buffer |
| 8 | + Print the contents of the buffer by sending anything over Serial Monitor |
| 9 | +*/ |
| 10 | +#include <RingBuf.h> |
| 11 | + |
| 12 | +// MUST BE AN INTTERUPT COMPATIBLE PIN |
| 13 | +#define EVENT_PIN 3 |
| 14 | + |
| 15 | +struct Event |
| 16 | +{ |
| 17 | + int index; |
| 18 | + unsigned char pinState; |
| 19 | + unsigned long timestamp; |
| 20 | +}; |
| 21 | + |
| 22 | +// Declare as volatile, since modofied in ISR |
| 23 | +volatile unsigned int index = 0; |
| 24 | + |
| 25 | +// Create a RinBuf object designed to hold a 20 Event structs |
| 26 | +RingBuf *buf = RingBuf_new(sizeof(struct Event), 20); |
| 27 | + |
| 28 | + |
| 29 | +void setup() { |
| 30 | + Serial.begin(9600); |
| 31 | + |
| 32 | + // Check if null pointer |
| 33 | + if (!buf) |
| 34 | + { |
| 35 | + Serial.println("Not enough memory"); |
| 36 | + while (1); |
| 37 | + } |
| 38 | + |
| 39 | + // Attach the interrupt service routine |
| 40 | + attachInterrupt(digitalPinToInterrupt(EVENT_PIN), isr_event, CHANGE); |
| 41 | + |
| 42 | + |
| 43 | + Serial.println("Toggle the pin state of pin EVENT_PIN, to fire an interrupt"); |
| 44 | + Serial.println("Send anything over serial to dump the contents of the buffer"); |
| 45 | + Serial.println(); |
| 46 | +} |
| 47 | + |
| 48 | +void loop() { |
| 49 | + // Print the number of elements |
| 50 | + Serial.print("There are: "); |
| 51 | + Serial.print(buf->numElements(buf)); |
| 52 | + Serial.println(" elements in the ring buffer"); |
| 53 | + |
| 54 | + // Check if empty |
| 55 | + if (buf->isEmpty(buf)) |
| 56 | + { |
| 57 | + Serial.println("The ring buffer is currently empty"); |
| 58 | + } |
| 59 | + else if (buf->isFull(buf)) |
| 60 | + { |
| 61 | + Serial.println("The ring buffer is currently full"); |
| 62 | + } |
| 63 | + |
| 64 | + // If any message sent through serial monitor, dump buffer |
| 65 | + if (Serial.available() > 0) |
| 66 | + { |
| 67 | + // Print contents of buffer |
| 68 | + print_buf_contents(); |
| 69 | + |
| 70 | + //empty serial buffer |
| 71 | + while (Serial.read() > 0); |
| 72 | + } |
| 73 | + |
| 74 | + Serial.println(); |
| 75 | + |
| 76 | + delay(2000); |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +// This interrupt serve routine function is called whenever an interrupt fires on EVENT_PIN |
| 81 | +// An Event Struct will be appened to the buffer |
| 82 | +void isr_event() |
| 83 | +{ |
| 84 | + struct Event e; |
| 85 | + e.index = index++; |
| 86 | + e.pinState = digitalRead(EVENT_PIN); |
| 87 | + e.timestamp = millis(); |
| 88 | + // Add it to the buffer |
| 89 | + buf->add(buf, &e); |
| 90 | +} |
| 91 | + |
| 92 | +// Print the buffer's contents then empty it |
| 93 | +void print_buf_contents() |
| 94 | +{ |
| 95 | + struct Event e; |
| 96 | + |
| 97 | + Serial.println("\n______Dumping contents of ring buffer_______"); |
| 98 | + |
| 99 | + // Keep looping until pull() returns NULL |
| 100 | + while (buf->pull(buf, &e)) |
| 101 | + { |
| 102 | + // |
| 103 | + Serial.print("Event index: "); |
| 104 | + Serial.println(e.index); |
| 105 | + |
| 106 | + Serial.print("Pin state: "); |
| 107 | + Serial.println(e.pinState); |
| 108 | + |
| 109 | + Serial.print("Timestamp (ms): "); |
| 110 | + Serial.println(e.timestamp); |
| 111 | + |
| 112 | + Serial.println(); |
| 113 | + } |
| 114 | + |
| 115 | + Serial.println("______Done dumping contents_______"); |
| 116 | + |
| 117 | +} |
| 118 | + |
0 commit comments