Think of a cyclocomputer as a hardwired combination of a calculator, a quartz-clock, and a dedicated CPU working with a buffer.
EDIT TO A MORE PLAUSIBLE ALGORITHM:
Each time the magnet closes the reed-switch, a request is sent to the clock to capture a time-stamp, a time-stamped event is sent to a buffer, and the wheel circumference is added to the current distance and to the odometer.
Each time the CPU updates the current speed, it takes the first and last timestamped events from the buffer, calculates the distance (wheel circumference multiplied by number-of-events-minus-one) and divides by the calculated elapsed time (last timestamp minus first timestamp in the buffer), displaying the speed and clearing the buffer.
Besides the persistently stored variables (current distance, maximum speed, etc.), the computer works with three temporary variables: LAST_TIME, TEMP_TIME and TEMP_DISTANCE, all of them set to zero. The present_time() function, related to the clock, is abstracted here as a resource readily available upon request.
Each time the reed-switch closes contact, the computer performs the following operations:
TRIP_DISTANCE = TRIP_DISTANCE + WHEEL_CIRCUMFERENCE
if LAST_TIME is zero:
LAST_TIME = present_time()
TEMP_TIME = TEMP_TIME + (present_time() - LAST_TIME)
LAST_TIME = present_time()
TEMP_DISTANCE = TEMP_DISTANCE + WHEEL_CIRCUMFERENCE
Each time the computer refreshes the screen, it performs the following operations:
CURRENT_SPEED = TEMP_DISTANCE / TEMP_TIME
TEMP_TIME = TEMP_DISTANCE = 0
AVERAGE_SPEED = TRIP_DISTANCE / TRIP_TIME
if CURRENT_SPEED > MAXIMUM_SPEED:
MAXIMUM_SPEED = CURRENT_SPEED
Finally, each time the auto-stop function is activated (when the bike is stopped):
LAST_TIME = 0
All these operations are not only trivial computationally speaking, but also almost realtime because thy are hardware-implemented in the integrated circuit.
It is worth considering two things:
- If the computer is working with auto-start / auto-stop function, if the wheel takes too long to complete a turn, the speed refresh function enters in pause mode;
- Most probably, these variables have a fixed-point/integer nature, that is, they have a maximum value. For the TEMP_TIME, which probably works on microssecond resolution, this might lead to variable overflow if too much time is elapsed between speed refreshes. That implies a minimum speed in order to work properly, depending on the memory size and number type of these variables in the integrated circuit.
Also, it is necessary that it counts the time between revolutions, not the number of revolutions during a fixed time interval, because time is a continuous (floating point) measure, and number of wheel turns is a discrete (integer) measure. If the second option would use, the speed would always be "rounded" to the nearest possible integer value, giving incorrect results except for very high speeds.