2018 revision to classic DataBus AVC code.

Dependencies:   LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell

Updater.h

Committer:
shimniok
Date:
2018-12-21
Revision:
25:b8176ebb96c6
Parent:
15:35c40765f7c3
Child:
32:eb673f6f5734

File content as of revision 25:b8176ebb96c6:

#ifndef __UPDATER_H
#define __UPDATER_H

#include "mbed.h"
#include "L3G4200D.h"

/** Periodically reads sensor data
 * This class reads and updates sensor data. Intended to be called at a fixed
 * interval.
 * @code
 * Updater *u = Updater::instance(); // beware of lifetime of this pointer
 * Thread updaterThread(osPriorityRealtime, 512, 0, "updater");
 * EventQueue *updaterQueue = mbed_highprio_event_queue();
 * Event<void()> event(updaterQueue, callback(u, &Updater::update));
 * event.period(20);
 * event.post(); // if lifetime of u not correct, this will hard fault
 * updaterThread.start(callback(updaterQueue, &EventQueue::dispatch_forever));  
 *
 */
class Updater: private mbed::NonCopyable<Updater> {
public:
    /// Return singleton instance
    static Updater *instance();
    
    /// Attach a callback handler run each time updater() is run
    void attach(Callback<void()> cb);
    
    /// Update all sensors
    void update(); 
    
    /** Get gyro values
     * @return g array of x, y, and z gyro values
     * @return dt time since data last updated
     */
    void gyro(int g[3], float& dt);
    
    /** Get encoder count
     * @return encoder count since last call
     */
    int encoder();

private:
    /// Basic constructor (singleton)
    Updater();
    
    Callback<void()> _callback; // notification callback    
    Timer *t; // timer used to measure dt
    int _gyro[3]; // gyro raw
    int _ecount; // encoder count
    float _dt;
    int _interval;
    int thisTime;
    int lastTime;
};

#endif