RTCDevice.h
1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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__*/