FifoBuffer

Description

The FifoBuffer object implements a buffer based on the FIFO (First-In-First-Out) principle. It can be either used as a queue which grows whenever a producer writes to it and shrinks whenever a consumer reads from it. Alternatively a ring-buffer-like behaviour can be achieved by limiting the size of the FIFO buffer through the maximumSize property.

› Inherits:List

Properties

input

This property holds the input of the FIFO buffer. Whenever the input is written it is added to the FIFO, i.e. to the front of List.items. If the new FIFO size exceeds maximumSize the least-recently added item is removed.

› Type:Variant
› Signal:inputChanged()
› Attributes:Writable

maximumSize

This property holds the desired maximum size of the FIFO buffer. The FIFO buffer will never contain more items than specified by this property. Leave at 0 to implement a queue which is always to be drained manually through the read() method.

› Type:SignedInteger
› Default:0
› Signal:maximumSizeChanged()
› Attributes:Writable

output

This property holds the output of the FIFO buffer, i.e. the last item of List.items. This property is updated automatically.

› Type:Variant
› Signal:outputChanged()
› Attributes:Readonly

size

This property holds the current size of the FIFO buffer, i.e. the number of items in the List.items property. This property is provided for convenience only and equals to items.length.

› Type:SignedInteger
› Signal:sizeChanged()
› Attributes:Readonly

Methods

read()

This method reads the output of the FIFO buffer. This is the same as reading from the output property except for the additional removal of the read data from the FIFO buffer. If the FIFO buffer is empty, an invalid value (undefined) is returned.

› Returns:Variant

write(Variant data)

This method writes the given data into the FIFO buffer. This is the same as writing/updating the input property.

Example

import InCore.Foundation 2.5
import InCore.Modbus 2.5

Application {

    // calculate the 5 second average of the (fake) measurements taken every 50 ms
    property var measurementInterval: 50
    property var averagingInterval: 5000

    Measurement {
        ReduceList on data {
            source: FifoBuffer {
                id: fifoBuffer
                maximumSize: averagingInterval / measurementInterval
                property var timer : Timer {
                    interval: measurementInterval
                    onTriggered: fifoBuffer.write( Math.random() )
                }
            }
            accumulatorInitValue: 0
            eval: accumulator + item / source.items.length;
        }
        onDataChanged: console.log("Average of", fifoBuffer.items.length, "measurements:", data)
    }

    // calculate electric charge (ampere hours) from a charge rate retrieved through a fictional Modbus register

    ModbusRegister {
        id: chargeRateInAmpere
        dataType: ModbusRegister.Float
        address: 123
        count: 2
        Polling on data {
            interval: 1000
        }
    }

    Measurement {
        ReduceList on data {
            source: FifoBuffer {
                maximumSize: 3600
                input: chargeRateInAmpere.data
            }
            accumulatorInitValue: 0
            eval: accumulator + item
        }
        onDataChanged: console.log("Total charge:", data, "Ah")
    }
}