Ideally, all electronic devices would be able to process data as soon as it is available with no delays. Unfortunately, there are many weak points in the data processing chain, which can result in situations where a processing point cannot physically process the data fast enough. In these scenarios, the incoming data can be temporarily stored to prevent losing incoming data. This memory system is called a buffer, which plays a key part in modern electronics. In this how-to, we’ll see how to use the Digi-Key IoT Studio buffer element!
Not all buffers are the same, and how they function can vary depending on the task. For example, some buffers are circular, meaning that when they are full, they start back at the beginning, while others have specific methods for data going in and out. Before we look at the buffer element used in the Digi-Key IoT Studio, we need to know about a few terminologies when talking about buffers.
While some buffers can have any element retrieved, it is rarely done. Instead, buffers are used sequentially to preserve the order in which data was sent. The buffer element used in the Digi-Key IoT Studio is known as a First In Last Out buffer, which means that data that was pushed in first will be the last data to be popped out. To better understand this concept, let’s take a simple example:
The use of the buffer in this example will demonstrate how the buffer can be used to store data. The Adafruit Huzzah 32 will be programmed to use a new element — the counter — that will be incremented once every two seconds. Each time the counter is incremented, the value of the counter is sent to the app via Bluetooth. The interval element, which triggers once every two seconds, is used to increment the counter, as well as retrieve its current value. The counter can be made to have a roll over value, which causes it to reset to 0 once that value has been reached. In this example, we leave the counters defaults which have the counter count between the numbers 0 and 9.
The app side of the how-to is essential because it uses an interval to probe a Bluetooth element once every second for values that have been sent to it. When valid data is returned by the Bluetooth element, the data is then pushed onto the buffer. At the same time, the buffer sends the data that was just pushed to it to a label element, so we can see what the last number to be sent was.
Pushing a value onto the buffer also causes another label element to display the current contents of the buffer (this allows us to visually see what resides in the buffer). A button element is then connected to the buffer and is set so that when the button is pushed, it causes a value to be popped off the buffer. This popped value is then sent to another label element, so we can see what the value is.