RTCDevice.h 1.28 KB
#ifndef __RTC_DEVICE_H__
#define __RTC_DEVICE_H__

#include "mbed.h"

class RTCDevice {
public:
    const static char firstReg_Address = 0x02;
    const static char device_address = 0xA2;
    const static int length = 7;
    
    RTCDevice(Serial *serial, I2C *i2c) : serial(serial), i2c(i2c) {}
    
    /*
     * Permet de lire le RTC et d'obtenir la date et l'heure.
     */
    void read(char data[]) {
        char cmd[1]={firstReg_Address};
        i2c->write(address_found, cmd, 1);
        wait(0.2);
        i2c->read(address_found, data, length);
    }
    
    /*
     * Permet de trouver le RTC et son adresse. Assigne une adresse trouvée à 
     * la variable 'adress_found'.
     */
    bool find() {
        int count = 0;
        bool res = false;
        
        serial->printf("Searching for I2C devices...\n");
        if (!i2c->write(device_address, NULL, 0)) //0 est renvoyé = capteur trouvé 
        { 
            serial->printf(" - I2C device found at address 0x%02X\n", device_address);
            address_found = device_address;
            res = true;
            count++;
        }
        serial->printf("%d devices found\n\n", count);
        return res;
    }

private:
    unsigned char address_found;
    Serial *serial;
    I2C *i2c;
};

#endif /* #ifndef __RTC_DEVICE_H__*/