Difference between revisions of "Component: Circular Buffer (Storage)"
(XML import) |
|||
(One intermediate revision by the same user not shown) | |||
Line 7: | Line 7: | ||
|- | |- | ||
| width="20%" style="color: gray;" | Version | | width="20%" style="color: gray;" | Version | ||
− | | 1. | + | | 1.5 (Release) |
|- | |- | ||
| width="20%" style="color: gray;" | Category | | width="20%" style="color: gray;" | Category | ||
Line 21: | Line 21: | ||
==Examples== | ==Examples== | ||
− | |||
===Basic Example=== | ===Basic Example=== | ||
Circular Buffer Example File demonstrating the storage and retrieval of data. | Circular Buffer Example File demonstrating the storage and retrieval of data. | ||
− | |||
{{Fcfile|CircularBuffer.fcfx|CircularBuffer}} | {{Fcfile|CircularBuffer.fcfx|CircularBuffer}} | ||
− | |||
− | |||
System panel showing data in and data out of the FIFO circular buffer. | System panel showing data in and data out of the FIFO circular buffer. | ||
Line 47: | Line 43: | ||
Each key press on the keypad is fed into the Circular Buffer component. We then poll the buffer to see if the correct characters for the password have been entered. | Each key press on the keypad is fed into the Circular Buffer component. We then poll the buffer to see if the correct characters for the password have been entered. | ||
− | |||
{{Fcfile|KeypadDoorEntry.fcfx|Keypad Door Entry}} | {{Fcfile|KeypadDoorEntry.fcfx|Keypad Door Entry}} | ||
− | |||
− | |||
If the correct sequence is detected then we activate the solenoid component for 5 seconds. | If the correct sequence is detected then we activate the solenoid component for 5 seconds. | ||
Line 59: | Line 52: | ||
This example takes bytes received from the Serial UART and uses the circular buffer to store the bytes as they come in using an interrupt. The main routine then forwards the bytes through to a PC using a USB serial connection. This example would work equally well for creating a bridge between several UARTs or translating Serial to SPI or I2C etc. | This example takes bytes received from the Serial UART and uses the circular buffer to store the bytes as they come in using an interrupt. The main routine then forwards the bytes through to a PC using a USB serial connection. This example would work equally well for creating a bridge between several UARTs or translating Serial to SPI or I2C etc. | ||
− | |||
{{Fcfile|CommsBuffer.fcfx|UART to USB Serial Data Bridge}} | {{Fcfile|CommsBuffer.fcfx|UART to USB Serial Data Bridge}} | ||
− | |||
− | |||
===AT Command Responses=== | ===AT Command Responses=== | ||
This example sends out AT commands to an AT based communications module e.g. GSM or Bluetooth. The response from the module is stored in the circular buffer and we then scan the buffer for appropriate responses from the module. The outcome of the command is then printed to the LCD to let you know if the command was accepted, caused an error or simply timed out. | This example sends out AT commands to an AT based communications module e.g. GSM or Bluetooth. The response from the module is stored in the circular buffer and we then scan the buffer for appropriate responses from the module. The outcome of the command is then printed to the LCD to let you know if the command was accepted, caused an error or simply timed out. | ||
− | |||
{{Fcfile|CircAT.fcfx|Reliable AT Command Response}} | {{Fcfile|CircAT.fcfx|Reliable AT Command Response}} | ||
− | |||
− | |||
===Searching for none ASCII characters=== | ===Searching for none ASCII characters=== | ||
Line 77: | Line 64: | ||
Will look for the ASCII characters A,B,C followed by the byte values 255 (0xFF) and 128 (0x80). | Will look for the ASCII characters A,B,C followed by the byte values 255 (0xFF) and 128 (0x80). | ||
− | |||
==Downloadable macro reference== | ==Downloadable macro reference== | ||
Line 102: | Line 88: | ||
:[[Variable Types|BYTE]] ''ResetFind'' | :[[Variable Types|BYTE]] ''ResetFind'' | ||
::0=Continue the find from the last operation, 1=Start again | ::0=Continue the find from the last operation, 1=Start again | ||
+ | |||
+ | |||
+ | '''Return value''' | ||
+ | |||
+ | :[[Variable Types|BYTE]] | ||
+ | |||
+ | |||
+ | ===<span style="font-weight: normal;"><u><tt>GetIndexedByte</tt></u></span>=== | ||
+ | Gets the value of a single byte at a known location in the buffer. | ||
+ | |||
+ | Leaves the buffer contents and pointers untouched. | ||
+ | |||
+ | '''Parameters''' | ||
+ | |||
+ | :[[Variable Types|UINT]] ''address'' | ||
Line 138: | Line 139: | ||
===<span style="font-weight: normal;"><u><tt>GetNumberBytes</tt></u></span>=== | ===<span style="font-weight: normal;"><u><tt>GetNumberBytes</tt></u></span>=== | ||
Returns the number of valid data bytes currently inside the buffer. | Returns the number of valid data bytes currently inside the buffer. | ||
+ | |||
+ | '''Parameters''' | ||
+ | |||
+ | :''This macro has no parameters'' | ||
+ | |||
+ | |||
+ | '''Return value''' | ||
+ | |||
+ | :[[Variable Types|UINT]] | ||
+ | |||
+ | |||
+ | ===<span style="font-weight: normal;"><u><tt>PeekByte</tt></u></span>=== | ||
+ | Reads the next available byte from the circular buffer without advancing the current index, | ||
+ | |||
+ | Returns 255/512 if the buffer is empty. | ||
'''Parameters''' | '''Parameters''' |
Latest revision as of 10:26, 9 May 2018
Author | Matrix Ltd |
Version | 1.5 (Release) |
Category | Storage |
Contents
Circular Buffer component
Circular buffer component allowing easy and efficient first in first out (FIFO) style data byte storage. Useful when used with a comms component to capture data as it comes in ready for processing when we have time. Also features macros to allow you to check for specific incoming responses such as "OK" or "ERROR".
Examples
Basic Example
Circular Buffer Example File demonstrating the storage and retrieval of data.
CircularBuffer
System panel showing data in and data out of the FIFO circular buffer.
Console window showing data in the circular buffer on the first simulation run.
Console window showing data in the circular buffer on the second simulation run, note that the start location has been shifted and will eventually wrap hence the name, circular buffer.
Keypad Based Door Entry System
Each key press on the keypad is fed into the Circular Buffer component. We then poll the buffer to see if the correct characters for the password have been entered.
Keypad Door Entry
If the correct sequence is detected then we activate the solenoid component for 5 seconds.
Reliable Communications Data
This example takes bytes received from the Serial UART and uses the circular buffer to store the bytes as they come in using an interrupt. The main routine then forwards the bytes through to a PC using a USB serial connection. This example would work equally well for creating a bridge between several UARTs or translating Serial to SPI or I2C etc.
UART to USB Serial Data Bridge
AT Command Responses
This example sends out AT commands to an AT based communications module e.g. GSM or Bluetooth. The response from the module is stored in the circular buffer and we then scan the buffer for appropriate responses from the module. The outcome of the command is then printed to the LCD to let you know if the command was accepted, caused an error or simply timed out.
Reliable AT Command Response
Searching for none ASCII characters
The LookForValue and WaitForValue functions can be useful when searching for ASCII data in the circular buffer. You can also use the escape sequence \x to search for hexadecimal values in the buffer.
e.g. "ABC\xFF\x80"
Will look for the ASCII characters A,B,C followed by the byte values 255 (0xFF) and 128 (0x80).
Downloadable macro reference
LookForValue
Scans the buffer for an array of specific values.
Returns 0 if the value is not found.
Returns 1 if the value is found.
Parameters
- <- STRING Value
- Value to look for, can be a string or byte array,
- This parameter may be returned back to the caller
- BYTE NumChars
- The number of characters you wish to try and match
- BYTE RemoveContent
- 0=Leave data alone, 1=Remove data from buffer
- BYTE ResetFind
- 0=Continue the find from the last operation, 1=Start again
Return value
GetIndexedByte
Gets the value of a single byte at a known location in the buffer.
Leaves the buffer contents and pointers untouched.
Parameters
- UINT address
Return value
WaitForValue
Waits for an array of values to appear in the circular buffer.
Returns 0 to indicate a timeout.
Returns 1 to indicate the data has been found.
Parameters
- <- STRING Value
- This parameter may be returned back to the caller
- BYTE NumChars
- Number of characters to try and look for
- BYTE RemoveContent
- 0=Leave the buffer contents untouched, 1=Remove values as you go
- UINT Timeout
- Max amount of time to wait in milliseconds before returning 0=WaitForever
Return value
GetNumberBytes
Returns the number of valid data bytes currently inside the buffer.
Parameters
- This macro has no parameters
Return value
PeekByte
Reads the next available byte from the circular buffer without advancing the current index,
Returns 255/512 if the buffer is empty.
Parameters
- This macro has no parameters
Return value
PutByte
Add byte to the next free location inside the circular buffer.
If the data goes into the buffer correctly then return 1.
Otherwise the buffer is full and the return value will equal 0.
Parameters
- BYTE Data
Return value
GetByte
Reads the next available byte from the circular buffer,
Returns 255 if the buffer is empty.
Parameters
- This macro has no parameters
Return value
FlushBuffer
Clears the contents of the buffer and re-initialises the index locations.
Parameters
- This macro has no parameters
Return value
- This call does not return a value
Simulation macro reference
GetConsoleHandle
Gets the handle to the console allowing data displaying on the panel etc.
Parameters
- This macro has no parameters
Return value
Property reference
Buffer Size
This property is of type Unsigned integer and can be referenced with the variable name BufferSize.
Sets the number of byte elements inside the circular buffer.
Storage Type
This property is of type Fixed list of ints and can be referenced with the variable name StorageType.
When buffer is full this property decides what to do.
Store first x values - Will retain the information inside the buffer, new data will be discarded.
Store last x values - Will discard the oldest location in the buffer and overwrite with new data.
Memory Type
This property is of type Fixed list of ints and can be referenced with the variable name MemType.
No additional information
Return Type
This property is of type Fixed list of ints and can be referenced with the variable name RETURN.
Specifies the way the receive function indicates a timeout.
8 Bit mode - Timeout is represented by the value 255.
16 Bit mode - Timeout is represented by the value 512 allowing the value 255 to represent valid data