Components For Embedded Programs
Components For Embedded Programs
programs
● State machine
● Circular buffer
3 components ● Queue
State machine
When inputs appear intermittently rather than as periodic samples, it is often convenient to think of the
system as reacting to those inputs. The reaction of most systems can be characterized in terms of the
input received and the current state of the system.This leads naturally to a finite-state machine style of
describing the reactive system’s behavior.
The behavior we want to implement is a simple seat belt controller [Chi94]. The controller’s job is to turn
on a buzzer if a person sits in a seat and does not fasten the seat belt within a fixed amount of time. This
system has three inputs and one output. The inputs are a sensor for the seat to know when a person has
sat down, a seat belt sensor that tells when the belt is fastened, and a timer that goes off when the
required time interval has elapsed. The output is the buzzer. Appearing below is a state diagram that
describes the seat belt controller’s behavior.
Circular Buffer
● The circular buffer is a data structure that lets us handle streaming data in an efficient way.
● At each point in time, the algorithm needs a subset of the data stream that forms a window into the
stream.
● The window slides with time as we throw out old values no longer needed and add new values.
● Because the size of the window does not change, we can use a fixed-size buffer to hold the current
data. To avoid constantly copying data within the buffer, we will move the head of the buffer in time.
● Because the size of the window does not change, we can use a fixed-size buffer to hold the current
data. To avoid constantly copying data within the buffer, we will move the head of the buffer in time.
● When the pointer gets to the end of the buffer, it wraps around to the top.
FIR Filter using circular buffer
● The filter operates at a sample rate with inputs arriving and outputs generated at the sample rate.
● The inputs x[n] and y[n] are sequences indexed by n, which corresponds to the sequence of samples.
● Nodes in the graph can represent either arithmetic operators or delay operators. The + node adds its two inputs and produces
the output y[n]. The box labeled z-1 is a delay operator.
The delay elements running vertically hold the input samples with the most recent sample at the top and the oldest one at the bottom.
Unfortunately, the signal flow graph doesn’t explicitly label all of the values that we use as inputs to operations, so the figure also
shows the values (xi) we need to operate on in our FIR loop. When we compute the filter function, we want to match the bi’s and xi’s.
We will use our circular buffer for the x’s, which change over time. We will use a standard array for the b’s, which don’t change. In order
for the filter function to be able to use the same I value for both sets of data, we need to put the x data in the proper order. We can put
the b data in a standard array with b0 being the first element. When we add a new x value, it becomes x 0 and replaces the oldest data
value in the buffer. This means that the buffer head moves from higher to lower values, not lower to higher as we might expect.
Queues and producer/consumer
Queues are used whenever data may arrive and depart at somewhat unpredictable times or when variable amounts of data may arrive.
A queue is often referred to as an elastic buffer.
● When several of these systems operate in a chain, the variable-rate output of one stage becomes
the variable-rate input of another stage.
● In producer/consumer system. p1 and p2 are the two blocks that perform algorithmic processing.
The data is fed to them by queues that act as elastic buffers.
● The queues modify the flow of control in the system as well as store data. If, for example, p2 runs
ahead of p1, it will eventually run out of data in its q12 input queue. At that point, the queue will
return an empty signal to p2. At this point, p2 should stop working until more data is available.
● The queues in a producer/consumer may hold either uniform-sized data elements or variable-sized
data elements.
Models of programs
● Data flow graph