Hi all, I am a relative Picaxe noob, and this is my first real post on the forum....
This may be such a common routine that it hasn't merited prior, specific discussion, but I could not find any information on the forum while searching for the terms "ring buffer, circular buffer, FIFO, LILO, etc." As part of my weather station project, I want to know the "most recent X values" of a particular sensor reading, with "now" being the first value. Specifically, I want to know the following:
1. Max wind gust in the last minute, and last 10 minutes,
2. Average wind direction for the same time periods
3. Last 24 (one-hour) barometer readings
Below is a simple "Ring Buffer" routine that I came up with that will handle these needs. I am running it on a 20x2 so I can start my EEPROM pointers at zero. For non-X2 chips, you would need to consult the EEPROM command doc for usable locations, or use one of the other types of available memory. I welcome comments/criticisms, and any suggestions for more efficient code or methods.
Thanks to all for the wealth of information on this site...., Greg
This may be such a common routine that it hasn't merited prior, specific discussion, but I could not find any information on the forum while searching for the terms "ring buffer, circular buffer, FIFO, LILO, etc." As part of my weather station project, I want to know the "most recent X values" of a particular sensor reading, with "now" being the first value. Specifically, I want to know the following:
1. Max wind gust in the last minute, and last 10 minutes,
2. Average wind direction for the same time periods
3. Last 24 (one-hour) barometer readings
Below is a simple "Ring Buffer" routine that I came up with that will handle these needs. I am running it on a 20x2 so I can start my EEPROM pointers at zero. For non-X2 chips, you would need to consult the EEPROM command doc for usable locations, or use one of the other types of available memory. I welcome comments/criticisms, and any suggestions for more efficient code or methods.
Thanks to all for the wealth of information on this site...., Greg
Code:
#picaxe 20x2
#terminal 9600
#no_data
#no_table
;Simple Ring Buffer for 10 sequential readings of a byte value
;New data enters one end, old data out the other end
;Simply modifiy counter ranges for other data (array) sizes
;Should work with word variables with slight tweaking
; Greg Derda, "doppler", 4/30/2018
symbol data_pointer = b0 ;primary data-array location pointer (e.g., 0 - 9)
symbol temp_pointer = b1 ;temporary data-array location (e.g., 10 - 19)
symbol in_data = b2 ;incomming data
symbol temp_data = b3 ;temporary data
symbol out_data = b4 ;output data
main:
inc in_data ;Debug, for generating test incomming data
;Read and copy existing data-array to temporary-array before writing new in_data
for data_pointer = 0 to 9 ;change to indicate data-array range
read data_pointer, temp_data
temp_pointer = data_pointer + 10 ;offset to first location in temp data array
write temp_pointer,temp_data
write 0, in_data ;now write new data to first location of data array
next data_pointer
;Now write temp-data back to data-array, starting at second element
for temp_pointer = 10 to 19 ;change to indicate temporary-data range
data_pointer = temp_pointer - 9 ;start at element 1, not overwriting elelment 0
read temp_pointer, temp_data
write data_pointer, temp_data
next temp_pointer
;Debug Output, to watch the pointer and data movement
sertxd("ptr / data",13,10)
for data_pointer = 0 to 9
read data_pointer, out_data
sertxd(#data_pointer," / ",#out_data,13,10)
next data_pointer
sertxd (13,10)
pause 1000 ;Debug, to slow the output down a bit
; End Debug Output
goto main
Last edited: