Commit 1b3a0906777e587b24575f0fb53ff846c6287054
1 parent
d40fe488
Ajout des fichiers mbed
Showing
19 changed files
with
993 additions
and
0 deletions
Show diff stats
Mbed_compiler/Capteur_pollution/CurrentTimeService.h
0 → 100644
@@ -0,0 +1,89 @@ | @@ -0,0 +1,89 @@ | ||
1 | +#ifndef __BLE_CURRENT_TIME_SERVICE_H__ | ||
2 | +#define __BLE_CURRENT_TIME_SERVICE_H__ | ||
3 | + | ||
4 | +#include "BLEDevice.h" | ||
5 | + | ||
6 | +/** | ||
7 | +* @class CurrentTimeService | ||
8 | +* @brief BLE Time Service. This service provides the current time and date. | ||
9 | +* Service: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.current_time.xml | ||
10 | +* Date Time: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.date_time.xml | ||
11 | +* Day of Week: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.day_of_week.xml | ||
12 | +*/ | ||
13 | +class CurrentTimeService { | ||
14 | +public: | ||
15 | + const static uint16_t UUID_CURRENT_TIME_SERVICE = 0x1805; | ||
16 | + const static uint16_t UUID_DATE_TIME_CHAR = 0x2A08; | ||
17 | + const static uint16_t UUID_DAY_OF_WEEK_CHAR = 0x2A09; | ||
18 | + | ||
19 | + typedef uint8_t DayOfWeekType_t; | ||
20 | + typedef struct DateTimeType_t{ | ||
21 | + uint16_t year; | ||
22 | + uint8_t month; | ||
23 | + uint8_t day; | ||
24 | + uint8_t hours; | ||
25 | + uint8_t minutes; | ||
26 | + uint8_t seconds; | ||
27 | + } DateTimeType_t; | ||
28 | + | ||
29 | + /** | ||
30 | + * @brief CurrentTimeService constructor. | ||
31 | + * @param ble Reference to BLE device. | ||
32 | + */ | ||
33 | + CurrentTimeService(BLEDevice& _ble) : | ||
34 | + ble(_ble), | ||
35 | + dateTimeCharacteristic(UUID_DATE_TIME_CHAR, &dateTime, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), | ||
36 | + dayOfWeekCharacteristic(UUID_DAY_OF_WEEK_CHAR, &dayOfWeek, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) | ||
37 | + { | ||
38 | + static bool serviceAdded = false; /* We should only ever need to add the information service once. */ | ||
39 | + if (serviceAdded) { | ||
40 | + return; | ||
41 | + } | ||
42 | + | ||
43 | + GattCharacteristic *charTable[] = { &dateTimeCharacteristic, | ||
44 | + &dayOfWeekCharacteristic }; | ||
45 | + | ||
46 | + GattService currentTimeService(UUID_CURRENT_TIME_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); | ||
47 | + | ||
48 | + ble.addService(currentTimeService); | ||
49 | + serviceAdded = true; | ||
50 | + } | ||
51 | + | ||
52 | + /** | ||
53 | + * @brief Update date time characteristic. | ||
54 | + * @param newDateTimeVal New date time measurement. | ||
55 | + */ | ||
56 | + void updateDateTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hours, uint8_t minutes, uint8_t seconds) | ||
57 | + { | ||
58 | + dateTime = (DateTimeType_t) { | ||
59 | + (uint16_t)year, | ||
60 | + (uint8_t)month, | ||
61 | + (uint8_t)day, | ||
62 | + (uint8_t)hours, | ||
63 | + (uint8_t)minutes, | ||
64 | + (uint8_t)seconds | ||
65 | + }; | ||
66 | + ble.updateCharacteristicValue(dateTimeCharacteristic.getValueHandle(), (uint8_t *) &dateTime, sizeof(DateTimeType_t)); | ||
67 | + } | ||
68 | + | ||
69 | + /** | ||
70 | + * @brief Update day of week characteristic. | ||
71 | + * @param newDayOfWeekVal New day of week measurement. | ||
72 | + */ | ||
73 | + void updateDayOfWeek(DayOfWeekType_t day) | ||
74 | + { | ||
75 | + dayOfWeek = (DayOfWeekType_t)day; | ||
76 | + ble.updateCharacteristicValue(dayOfWeekCharacteristic.getValueHandle(), (uint8_t *) &dayOfWeek, sizeof(DayOfWeekType_t)); | ||
77 | + } | ||
78 | + | ||
79 | +private: | ||
80 | + BLEDevice& ble; | ||
81 | + | ||
82 | + DateTimeType_t dateTime; | ||
83 | + DayOfWeekType_t dayOfWeek; | ||
84 | + | ||
85 | + ReadOnlyGattCharacteristic<DateTimeType_t> dateTimeCharacteristic; | ||
86 | + ReadOnlyGattCharacteristic<DayOfWeekType_t> dayOfWeekCharacteristic; | ||
87 | +}; | ||
88 | + | ||
89 | +#endif /* #ifndef __BLE_CURRENT_TIME_SERVICE_H__*/ | ||
0 | \ No newline at end of file | 90 | \ No newline at end of file |
Mbed_compiler/Capteur_pollution/EnvironmentalSensingService.h
0 → 100644
@@ -0,0 +1,94 @@ | @@ -0,0 +1,94 @@ | ||
1 | +/* mbed Microcontroller Library | ||
2 | + * Copyright (c) 2006-2013 ARM Limited | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +#ifndef __BLE_ENVIRONMENTAL_SENSING_SERVICE_H__ | ||
18 | +#define __BLE_ENVIRONMENTAL_SENSING_SERVICE_H__ | ||
19 | + | ||
20 | +#include "BLEDevice.h" | ||
21 | + | ||
22 | +/** | ||
23 | +* @class EnvironmentalSensingService | ||
24 | +* @brief BLE Environmental Service. This service provides temperature, humidity and pressure measurement. | ||
25 | +* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.environmental_sensing.xml | ||
26 | +* Temperature: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature.xml | ||
27 | +* Humidity: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.humidity.xml | ||
28 | +* Pressure: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.pressure.xml | ||
29 | +*/ | ||
30 | +class EnvironmentalSensingService { | ||
31 | +public: | ||
32 | + const static uint16_t UUID_ENVIRONMENTAL_SENSING_SERVICE = 0x181A; | ||
33 | + const static uint16_t UUID_HUMIDITY_CHAR = 0x2A6F; | ||
34 | + const static uint16_t UUID_PRESSURE_CHAR = 0x2A6D; | ||
35 | + | ||
36 | + typedef uint16_t HumidityType_t; | ||
37 | + typedef uint32_t PressureType_t; | ||
38 | + | ||
39 | + /** | ||
40 | + * @brief EnvironmentalService constructor. | ||
41 | + * @param ble Reference to BLE device. | ||
42 | + */ | ||
43 | + EnvironmentalSensingService(BLEDevice& _ble) : | ||
44 | + ble(_ble), | ||
45 | + humidityCharacteristic(UUID_HUMIDITY_CHAR, &humidity, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), | ||
46 | + pressureCharacteristic(UUID_PRESSURE_CHAR, &pressure, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) | ||
47 | + { | ||
48 | + static bool serviceAdded = false; /* We should only ever need to add the information service once. */ | ||
49 | + if (serviceAdded) { | ||
50 | + return; | ||
51 | + } | ||
52 | + | ||
53 | + GattCharacteristic *charTable[] = { &pressureCharacteristic, | ||
54 | + &humidityCharacteristic | ||
55 | + }; | ||
56 | + | ||
57 | + GattService environmentalSensingService(UUID_ENVIRONMENTAL_SENSING_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); | ||
58 | + | ||
59 | + ble.addService(environmentalSensingService); | ||
60 | + serviceAdded = true; | ||
61 | + } | ||
62 | + | ||
63 | + /** | ||
64 | + * @brief Update humidity characteristic. | ||
65 | + * @param newHumidityVal New humidity measurement. | ||
66 | + */ | ||
67 | + void updateHumidity(double newHumidityVal) | ||
68 | + { | ||
69 | + humidity = (HumidityType_t) (newHumidityVal * 100); | ||
70 | + ble.updateCharacteristicValue(humidityCharacteristic.getValueHandle(), (uint8_t *) &humidity, sizeof(HumidityType_t)); | ||
71 | + } | ||
72 | + | ||
73 | + /** | ||
74 | + * @brief Update pressure characteristic. | ||
75 | + * @param newPressureVal New pressure measurement. | ||
76 | + */ | ||
77 | + void updatePressure(double newPressureVal) | ||
78 | + { | ||
79 | + pressure = (PressureType_t) (newPressureVal * 10); | ||
80 | + ble.updateCharacteristicValue(pressureCharacteristic.getValueHandle(), (uint8_t *) &pressure, sizeof(PressureType_t)); | ||
81 | + } | ||
82 | + | ||
83 | + | ||
84 | +private: | ||
85 | + BLEDevice& ble; | ||
86 | + | ||
87 | + HumidityType_t humidity; | ||
88 | + PressureType_t pressure; | ||
89 | + | ||
90 | + ReadOnlyGattCharacteristic<HumidityType_t> humidityCharacteristic; | ||
91 | + ReadOnlyGattCharacteristic<PressureType_t> pressureCharacteristic; | ||
92 | +}; | ||
93 | + | ||
94 | +#endif /* #ifndef __BLE_ENVIRONMENTAL_SENSING_SERVICE_H__*/ | ||
0 | \ No newline at end of file | 95 | \ No newline at end of file |
Mbed_compiler/Capteur_pollution/LocationAndNavigationService.h
0 → 100644
@@ -0,0 +1,76 @@ | @@ -0,0 +1,76 @@ | ||
1 | +#ifndef __BLE_LOCATION_AND_NAVIGATION_SERVICE_H__ | ||
2 | +#define __BLE_LOCATION_AND_NAVIGATION_SERVICE_H__ | ||
3 | + | ||
4 | +#include "BLEDevice.h" | ||
5 | + | ||
6 | +/** | ||
7 | +* @class LocationAndNavigationService | ||
8 | +* @brief BLE Location and Navigation Service. This service provides location. | ||
9 | +* Service: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.location_and_navigation.xml | ||
10 | +* LN Feature: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.ln_feature.xml | ||
11 | +* Location and Speed: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.location_and_speed.xml | ||
12 | +*/ | ||
13 | +class LocationAndNavigationService { | ||
14 | +public: | ||
15 | + const static uint16_t UUID_LOCATION_AND_NAVIGATION_SERVICE = 0x1819; | ||
16 | + const static uint16_t UUID_LN_FEATURE_CHAR = 0x2A6A; | ||
17 | + const static uint16_t UUID_LOCATION_AND_SPEED_CHAR = 0x2A67; | ||
18 | + | ||
19 | + typedef struct LocationAndSpeedType_t{ | ||
20 | + uint16_t flags; | ||
21 | + int32_t latitude; | ||
22 | + int32_t longitude; | ||
23 | + int32_t altitude; | ||
24 | + } LocationAndSpeedType_t; | ||
25 | + typedef uint32_t LNFeatureType_t; | ||
26 | + | ||
27 | + /** | ||
28 | + * @brief LocationAndNavigationService constructor. | ||
29 | + * @param ble Reference to BLE device. | ||
30 | + */ | ||
31 | + LocationAndNavigationService(BLEDevice& _ble, LNFeatureType_t features) : | ||
32 | + ble(_ble), | ||
33 | + lnFeature(features), | ||
34 | + lnFeatureCharacteristic(UUID_LN_FEATURE_CHAR, &lnFeature), | ||
35 | + locationAndSpeedCharacteristic(UUID_LOCATION_AND_SPEED_CHAR, &locationAndSpeed, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) | ||
36 | + { | ||
37 | + static bool serviceAdded = false; /* We should only ever need to add the information service once. */ | ||
38 | + if (serviceAdded) { | ||
39 | + return; | ||
40 | + } | ||
41 | + | ||
42 | + GattCharacteristic *charTable[] = { &lnFeatureCharacteristic, | ||
43 | + &locationAndSpeedCharacteristic }; | ||
44 | + | ||
45 | + GattService locationAndNavigationService(UUID_LOCATION_AND_NAVIGATION_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); | ||
46 | + | ||
47 | + ble.addService(locationAndNavigationService); | ||
48 | + serviceAdded = true; | ||
49 | + } | ||
50 | + | ||
51 | + /** | ||
52 | + * @brief Update location and speed characteristic. | ||
53 | + * @param newLocationAndSpeedVal New location and speed measurement. | ||
54 | + */ | ||
55 | + void updateLocationAndSpeed(uint16_t flags, double latitude, double longitude, double altitude) | ||
56 | + { | ||
57 | + locationAndSpeed = (LocationAndSpeedType_t) { | ||
58 | + (uint16_t)flags, | ||
59 | + (int32_t)(latitude * 100000), | ||
60 | + (int32_t)(longitude * 100000), | ||
61 | + (int32_t)(altitude * 100) | ||
62 | + }; | ||
63 | + ble.updateCharacteristicValue(locationAndSpeedCharacteristic.getValueHandle(), (uint8_t *) &locationAndSpeed, sizeof(LocationAndSpeedType_t)); | ||
64 | + } | ||
65 | + | ||
66 | +private: | ||
67 | + BLEDevice& ble; | ||
68 | + | ||
69 | + LNFeatureType_t lnFeature; | ||
70 | + LocationAndSpeedType_t locationAndSpeed; | ||
71 | + | ||
72 | + ReadOnlyGattCharacteristic<LNFeatureType_t> lnFeatureCharacteristic; | ||
73 | + ReadOnlyGattCharacteristic<LocationAndSpeedType_t> locationAndSpeedCharacteristic; | ||
74 | +}; | ||
75 | + | ||
76 | +#endif /* #ifndef __BLE_LOCATION_AND_NAVIGATION_SERVICE_H__*/ | ||
0 | \ No newline at end of file | 77 | \ No newline at end of file |
No preview for this file type
No preview for this file type
@@ -0,0 +1,67 @@ | @@ -0,0 +1,67 @@ | ||
1 | +#ifndef __BLE_POLLUTION_SERVICE_H__ | ||
2 | +#define __BLE_POLLUTION_SERVICE_H__ | ||
3 | + | ||
4 | +#include "BLEDevice.h" | ||
5 | + | ||
6 | +/* Pollution Service */ | ||
7 | +class PollutionService { | ||
8 | +public: | ||
9 | + const static uint16_t UUID_POLLUTION_SERVICE = 0xA000; | ||
10 | + const static uint16_t UUID_POLLUTION_LEVEL_CHAR = 0xA001; | ||
11 | + const static uint16_t UUID_TEMPERATURE_CHAR = 0x2A6E; | ||
12 | + | ||
13 | + typedef int16_t TemperatureType_t; | ||
14 | + typedef uint8_t PollutionLevelType_t; | ||
15 | + | ||
16 | + PollutionService(BLEDevice &_ble, PollutionLevelType_t level) : | ||
17 | + ble(_ble), | ||
18 | + pollutionLevel(level), | ||
19 | + pollutionLevelCharacteristic(UUID_POLLUTION_LEVEL_CHAR, &pollutionLevel, sizeof(pollutionLevel), sizeof(pollutionLevel), | ||
20 | + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), | ||
21 | + temperatureCharacteristic(UUID_TEMPERATURE_CHAR, &temperature, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { | ||
22 | + | ||
23 | + static bool serviceAdded = false; /* We should only ever need to add the service once. */ | ||
24 | + if (serviceAdded) { | ||
25 | + return; | ||
26 | + } | ||
27 | + | ||
28 | + GattCharacteristic *charTable[] = {&pollutionLevelCharacteristic, | ||
29 | + &temperatureCharacteristic }; | ||
30 | + GattService pollutionService(UUID_POLLUTION_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); | ||
31 | + | ||
32 | + ble.addService(pollutionService); | ||
33 | + serviceAdded = true; | ||
34 | + } | ||
35 | + | ||
36 | + /** | ||
37 | + * Update the pollution level with a new value. Valid values range from | ||
38 | + * 0..100. Anything outside this range will be ignored. | ||
39 | + * @param newLevel New level. | ||
40 | + */ | ||
41 | + void updatePollutionLevel(uint8_t newLevel) { | ||
42 | + pollutionLevel = newLevel; | ||
43 | + ble.updateCharacteristicValue(pollutionLevelCharacteristic.getValueAttribute().getHandle(), &pollutionLevel, 1); | ||
44 | + } | ||
45 | + | ||
46 | + /** | ||
47 | + * @brief Update temperature characteristic. | ||
48 | + * @param newTemperatureVal New temperature measurement. | ||
49 | + */ | ||
50 | + void updateTemperature(double newTemperatureVal) | ||
51 | + { | ||
52 | + temperature = (TemperatureType_t) (newTemperatureVal * 100); | ||
53 | + ble.updateCharacteristicValue(temperatureCharacteristic.getValueHandle(), (uint8_t *) &temperature, sizeof(TemperatureType_t)); | ||
54 | + } | ||
55 | + | ||
56 | + | ||
57 | +private: | ||
58 | + BLEDevice &ble; | ||
59 | + | ||
60 | + TemperatureType_t temperature; | ||
61 | + PollutionLevelType_t pollutionLevel; | ||
62 | + | ||
63 | + GattCharacteristic pollutionLevelCharacteristic; | ||
64 | + ReadOnlyGattCharacteristic<TemperatureType_t> temperatureCharacteristic; | ||
65 | +}; | ||
66 | + | ||
67 | +#endif /* #ifndef __BLE_POLLUTION_SERVICE_H__*/ |
@@ -0,0 +1,34 @@ | @@ -0,0 +1,34 @@ | ||
1 | +# Programme pour la connexion Bluetooth de la carte du capteur de pollution | ||
2 | + | ||
3 | +## Informations pour la connexion avec l'application Android : | ||
4 | +* Particules de pollution et température : | ||
5 | +** Pollution Service : *0xA000* | ||
6 | +** Pollution Level Characteristic : *0xA001* | ||
7 | +** Temperature Characteristic : *0x2A6E* | ||
8 | +* Humidité et Pression atmosphérique : | ||
9 | +** Environmental Service : *0x181A* **(Service officiel)** | ||
10 | +** Humidity Characteristic : *0x2A6F* | ||
11 | +** Pressure Characteristic : *0x2A6D* | ||
12 | +* Position géographique : | ||
13 | +** LocationAndNavigationService : *0x1819* **(Service officiel)** | ||
14 | +** Location and Speed Characteristic : *0x2A67* | ||
15 | +* Temps et heure : | ||
16 | +** CurrentTimeService : *0x1805* **(Service officiel)** | ||
17 | +** DateTime Characteristic : *0x2A08* | ||
18 | +** Day of Week Characteristic : *0x2A09* | ||
19 | +* Appearance : Pollution Particles Sensor : *0x6000* | ||
20 | + | ||
21 | +## Informations diverses sur le programme : | ||
22 | +* Le nom de l'application ne peut pas contenir plus de 14 caractères, sinon il | ||
23 | +devient par défaut ST_BTL_DEV. | ||
24 | + | ||
25 | +##Pour rajouter/modifier des services : | ||
26 | +* **uuid16_list** contient la liste des services auxquels souscrire. | ||
27 | +* Les services sont déclarés dans le fichier **GattService.h** du dossier **public**. | ||
28 | +* Les variables des services sont déclarés dans le main (pollution, battery, ...). | ||
29 | +* Les classes associées se trouvent dans le dossier **services**. | ||
30 | +* Les informations concernant l'apparence de l'objet connecté sont déclarées dans le | ||
31 | +fichier **GapAdvertisingData.h** du dossier **public**. | ||
32 | +* Les caractéristiques sont déclarées dans le fichier **GattCharacteristic.h** | ||
33 | +du dossier **public**. | ||
34 | +* Les services ne peuvent contenir plus de 2 caractéristiques à la fois. | ||
0 | \ No newline at end of file | 35 | \ No newline at end of file |
@@ -0,0 +1,108 @@ | @@ -0,0 +1,108 @@ | ||
1 | +/* mbed Microcontroller Library | ||
2 | + * Copyright (c) 2006-2013 ARM Limited | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | + | ||
17 | +#include "mbed.h" | ||
18 | +#include "BLEDevice.h" | ||
19 | +#include "PollutionService.h" | ||
20 | +#include "EnvironmentalSensingService.h" | ||
21 | +#include "LocationAndNavigationService.h" | ||
22 | +#include "CurrentTimeService.h" | ||
23 | +#include "BatteryService.h" | ||
24 | +#include "DeviceInformationService.h" | ||
25 | +#include "Utils.h" | ||
26 | + | ||
27 | +BLEDevice ble; | ||
28 | +DigitalOut led1(LED1); | ||
29 | + | ||
30 | +const static char DEVICE_NAME[] = "Pollution"; | ||
31 | +static const uint16_t uuid16_list[] = {0xFFFF, | ||
32 | + PollutionService::UUID_POLLUTION_SERVICE, | ||
33 | + EnvironmentalSensingService::UUID_ENVIRONMENTAL_SENSING_SERVICE, | ||
34 | + LocationAndNavigationService::UUID_LOCATION_AND_NAVIGATION_SERVICE, | ||
35 | + CurrentTimeService::UUID_CURRENT_TIME_SERVICE, | ||
36 | + GattService::UUID_BATTERY_SERVICE, | ||
37 | + GattService::UUID_DEVICE_INFORMATION_SERVICE}; | ||
38 | +static volatile bool triggerSensorPolling = false; | ||
39 | + | ||
40 | + | ||
41 | +void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) | ||
42 | +{ | ||
43 | + DEBUG("Disconnected!\n\r"); | ||
44 | + DEBUG("Restarting the advertising process\n\r"); | ||
45 | + ble.startAdvertising(); // restart advertising | ||
46 | +} | ||
47 | + | ||
48 | +void connectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *reason) | ||
49 | +{ | ||
50 | + DEBUG("Connected\r\n"); | ||
51 | +} | ||
52 | + | ||
53 | +void periodicCallback(void) | ||
54 | +{ | ||
55 | + led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ | ||
56 | + triggerSensorPolling = true; | ||
57 | +} | ||
58 | + | ||
59 | +int main(void) | ||
60 | +{ | ||
61 | + led1 = 1; | ||
62 | + Ticker ticker; | ||
63 | + ticker.attach(periodicCallback, 1); | ||
64 | + | ||
65 | + DEBUG("Initialising \n\r"); | ||
66 | + ble.init(); | ||
67 | + ble.onDisconnection(disconnectionCallback); | ||
68 | + ble.onConnection(connectionCallback); | ||
69 | + | ||
70 | + /* Setup services. */ | ||
71 | + PollutionService pollutionService(ble, 76); | ||
72 | + EnvironmentalSensingService environmentalSensingService(ble); | ||
73 | + LocationAndNavigationService locationAndNavigationService(ble, 0x0C0400); | ||
74 | + CurrentTimeService currentTimeService(ble); | ||
75 | + BatteryService batteryService(ble, 61); | ||
76 | + DeviceInformationService deviceInfoService(ble, "ST", "Nucleo", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); | ||
77 | + | ||
78 | + /* Setup advertising. */ | ||
79 | + /* BREDR_NOT_SUPPORTED means classic bluetooth not supported; | ||
80 | + * LE_GENERAL_DISCOVERABLE means that this peripheral can be | ||
81 | + * discovered by any BLE scanner--i.e. any phone. */ | ||
82 | + ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); | ||
83 | + ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); | ||
84 | + ble.accumulateAdvertisingPayload(GapAdvertisingData::POLLUTION_PARTICLES_SENSOR); | ||
85 | + ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); | ||
86 | + ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); | ||
87 | + ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ | ||
88 | + ble.startAdvertising(); | ||
89 | + | ||
90 | + while (true) | ||
91 | + { | ||
92 | + if (triggerSensorPolling) | ||
93 | + { | ||
94 | + triggerSensorPolling = false; | ||
95 | + pollutionService.updatePollutionLevel(73); | ||
96 | + pollutionService.updateTemperature(25.76); | ||
97 | + environmentalSensingService.updateHumidity(15.2); | ||
98 | + environmentalSensingService.updatePressure(3.5); | ||
99 | + locationAndNavigationService.updateLocationAndSpeed(0x0C00, 4836.5375, 00740.9373, 200.2); | ||
100 | + currentTimeService.updateDateTime(2018, 02, 02, 16, 29, 45); | ||
101 | + currentTimeService.updateDayOfWeek(5); | ||
102 | + } | ||
103 | + else | ||
104 | + { | ||
105 | + ble.waitForEvent(); | ||
106 | + } | ||
107 | + } | ||
108 | +} |
@@ -0,0 +1,63 @@ | @@ -0,0 +1,63 @@ | ||
1 | +#include "mbed.h" | ||
2 | + | ||
3 | +#define LENGTH 112 | ||
4 | +#define BAUD 9600 | ||
5 | + | ||
6 | +I2C i2c(D14, D15, BAUD); | ||
7 | +DigitalOut led1(LED1); | ||
8 | +Serial pc(SERIAL_TX, SERIAL_RX); | ||
9 | + | ||
10 | +unsigned char adress_DSGPM;//= 0xD0, 0xD1, 0xD2 ou 0xD3 en fonction des jumpers branchés sur le GPS | ||
11 | +char data[LENGTH]; | ||
12 | + | ||
13 | +bool findGPSDevices(); | ||
14 | + | ||
15 | +int main() | ||
16 | +{ | ||
17 | + pc.printf("starting..."); | ||
18 | + | ||
19 | + if(findGPSDevices()){ | ||
20 | + char BaseReg_Address = 0; | ||
21 | + char cmd[1]={BaseReg_Address}; | ||
22 | + | ||
23 | + while(1) | ||
24 | + { | ||
25 | + led1 = 1; | ||
26 | + wait(0.2); | ||
27 | + i2c.write(adress_DSGPM, cmd, 1); | ||
28 | + i2c.read(adress_DSGPM, data, LENGTH); | ||
29 | + pc.printf("Time : %d%d:%d%d:%d%d\n", data[0], data[1], data[2], data[3], data[4], data[5]); | ||
30 | + pc.printf("Date : %d%d/%d%d/%d%d%d%d\n", data[6], data[7], data[8], data[9], data[10], data[11], data[12], data[13]); | ||
31 | + pc.printf("Latitude : %d%d degres\n", data[14], data[15]); | ||
32 | + pc.printf("Longitude : %d%d%d degres\n", data[23], data[24], data[25]); | ||
33 | + pc.printf("Altitude : %d%d%d%d%d metres\n\n", data[39], data[40], data[41], data[42], data[43]); | ||
34 | + led1 = 0; | ||
35 | + wait(0.5); | ||
36 | + } | ||
37 | + } | ||
38 | +} | ||
39 | + | ||
40 | +/* | ||
41 | + * Permet de trouver les GPS et leurs adresses. Assigne une adresse trouvée à | ||
42 | + * la variable 'adress_DSGPM'. | ||
43 | + */ | ||
44 | +bool findGPSDevices() { | ||
45 | + int count = 0; | ||
46 | + bool res = false; | ||
47 | + | ||
48 | + pc.printf("Searching for I2C devices...\n"); | ||
49 | + for (int address=0xD0; address<0xD7; address+=2) | ||
50 | + { | ||
51 | + if (!i2c.write(address, NULL, 0)) //0 est renvoyé = GPS trouvé | ||
52 | + { | ||
53 | + pc.printf(" - I2C device found at address 0x%02X\n", address); | ||
54 | + adress_DSGPM = address; | ||
55 | + res = true; | ||
56 | + count++; | ||
57 | + } | ||
58 | + | ||
59 | + } | ||
60 | + pc.printf("%d devices found\n\n", count); | ||
61 | + return res; | ||
62 | +} | ||
63 | + |
@@ -0,0 +1,92 @@ | @@ -0,0 +1,92 @@ | ||
1 | +#ifndef __GPS_DEVICE_H__ | ||
2 | +#define __GPS_DEVICE_H__ | ||
3 | + | ||
4 | +#include "mbed.h" | ||
5 | +#define GPGGA "GPGGA" | ||
6 | + | ||
7 | +class GPSDevice { | ||
8 | +public: | ||
9 | + const static int length = 80; | ||
10 | + const static int baud = 9600; | ||
11 | + | ||
12 | + GPSDevice(Serial *uart) : uart(uart) {} | ||
13 | + | ||
14 | + /* | ||
15 | + * Permet de savoir si on lit le début de la trame associée au GPS. | ||
16 | + */ | ||
17 | + bool canBeRead() { | ||
18 | + return uart->readable() && uart->getc() == '$'; | ||
19 | + } | ||
20 | + | ||
21 | + /* | ||
22 | + * Permet de déterminer le type de trame du GPS. | ||
23 | + */ | ||
24 | + void readType(char type[6]){ | ||
25 | + uart->scanf("%5s", &type); | ||
26 | + } | ||
27 | + | ||
28 | + /* | ||
29 | + * Permet de lire la trame du GPS de type GGA. | ||
30 | + */ | ||
31 | + void readGPGGA() { | ||
32 | + char msg[100]; | ||
33 | + | ||
34 | + while (uart->getc() != ','); //waits for first comma then copies info | ||
35 | + uart->scanf("%s", &msg); | ||
36 | + | ||
37 | + char *tok = strtok(msg, ","); //begins isolating fields of information delimited by a comma | ||
38 | + int comma = 0; | ||
39 | + while(tok != NULL){ | ||
40 | + comma++; | ||
41 | + switch (comma) { | ||
42 | + case 1: UTC = tok; break; //GMT time in seconds, saves pointer to string | ||
43 | + case 2: lat = tok; break; //latitude | ||
44 | + case 3: latl = tok; break; //lat letter, (N or S) | ||
45 | + case 4: lon = tok; break; //longitude | ||
46 | + case 5: lonl = tok; break; //lon letter (W or E) | ||
47 | + case 6: valid = tok; break; //valid | ||
48 | + } | ||
49 | + | ||
50 | + tok = strtok(NULL, ","); | ||
51 | + } | ||
52 | + } | ||
53 | + | ||
54 | + /* | ||
55 | + * Permet de vérifier que la trame lue est valide. | ||
56 | + */ | ||
57 | + bool isReadValid() { | ||
58 | + return atoi(valid) == 1 || atoi(valid) == 2 || atoi(valid) == 3; | ||
59 | + } | ||
60 | + | ||
61 | + char* getUTC() { | ||
62 | + return UTC; | ||
63 | + } | ||
64 | + | ||
65 | + char* getLatitude() { | ||
66 | + if(strcmp(latl, "N")){ | ||
67 | + return lat; | ||
68 | + } else { | ||
69 | + char *newLat = ""; | ||
70 | + strcpy(newLat, "-"); | ||
71 | + strncat(lat, newLat, sizeof(lat)/sizeof(lat[0])); | ||
72 | + return newLat; | ||
73 | + } | ||
74 | + } | ||
75 | + | ||
76 | + char* getLongitude() { | ||
77 | + if(strcmp(lonl, "E")){ | ||
78 | + return lon; | ||
79 | + } else { | ||
80 | + char *newLon = ""; | ||
81 | + strcpy(newLon, "-"); | ||
82 | + strncat(lon, newLon, sizeof(lon)/sizeof(lon[0])); | ||
83 | + return newLon; | ||
84 | + } | ||
85 | + } | ||
86 | + | ||
87 | +private: | ||
88 | + Serial *uart; | ||
89 | + char *UTC, *lat, *latl, *lon, *lonl, *valid; | ||
90 | +}; | ||
91 | + | ||
92 | +#endif /* #ifndef __GPS_DEVICE_H__*/ | ||
0 | \ No newline at end of file | 93 | \ No newline at end of file |
@@ -0,0 +1,39 @@ | @@ -0,0 +1,39 @@ | ||
1 | +#include "mbed.h" | ||
2 | +#include "GPSDevice.h" | ||
3 | + | ||
4 | +#define LENGTH 80 | ||
5 | +#define BAUD 9600 | ||
6 | + | ||
7 | +DigitalOut led1(LED1); | ||
8 | +Serial pc(USBTX, USBRX, BAUD); | ||
9 | +Serial uart(D8, D2, BAUD); | ||
10 | + | ||
11 | +void readGPGGA(); | ||
12 | + | ||
13 | +int main() | ||
14 | +{ | ||
15 | + pc.printf("starting..."); | ||
16 | + GPSDevice gpsDevice(&uart); | ||
17 | + | ||
18 | + while(1) { | ||
19 | + char type[6]; | ||
20 | + if (gpsDevice.canBeRead()){ | ||
21 | + gpsDevice.readType(type); | ||
22 | + | ||
23 | + if (strcmp(type, GPGGA) == 0) { | ||
24 | + gpsDevice.readGPGGA(); | ||
25 | + | ||
26 | + if(gpsDevice.isReadValid()) { | ||
27 | + pc.printf("Time : %s\n", gpsDevice.getUTC()); | ||
28 | + pc.printf("Latitude : %s N\n", gpsDevice.getLatitude()); | ||
29 | + pc.printf("Longitude : %s E\n", gpsDevice.getLongitude()); | ||
30 | + } | ||
31 | + } | ||
32 | + led1 = !led1; | ||
33 | + } | ||
34 | + } | ||
35 | +} | ||
36 | + | ||
37 | + | ||
38 | + | ||
39 | + |
@@ -0,0 +1,98 @@ | @@ -0,0 +1,98 @@ | ||
1 | +#ifndef __HUMIDITY_DEVICE_H__ | ||
2 | +#define __HUMIDITY_DEVICE_H__ | ||
3 | + | ||
4 | +#include "mbed.h" | ||
5 | + | ||
6 | +class HumidityDevice { | ||
7 | +public: | ||
8 | + const static char configReg_Address = 0x02; | ||
9 | + const static char device_address = 0x80; | ||
10 | + int length; | ||
11 | + | ||
12 | + HumidityDevice(Serial *serial, I2C *i2c, bool shouldReadTemperature) : serial(serial), i2c(i2c), | ||
13 | + shouldReadTemperature(shouldReadTemperature) { | ||
14 | + if(shouldReadTemperature){ | ||
15 | + length = 4; | ||
16 | + triggerMes_Address = 0x00; | ||
17 | + } else { | ||
18 | + length = 2; | ||
19 | + triggerMes_Address = 0x01; | ||
20 | + } | ||
21 | + | ||
22 | + powRes = pow((double)2, (double)16); | ||
23 | + } | ||
24 | + | ||
25 | + /* | ||
26 | + * Permet de lire le capteur d'humidité et d'obtenir ses valeurs. | ||
27 | + */ | ||
28 | + void read(char data[]) { | ||
29 | + char cmd[1]={triggerMes_Address}; | ||
30 | + i2c->write(address_found, cmd, 1); | ||
31 | + wait(0.2); | ||
32 | + i2c->read(address_found, data, length); | ||
33 | + | ||
34 | + if(shouldReadTemperature){ | ||
35 | + temp = (float((data[0] << 8)| data[1]) / powRes) * 165 - 40; | ||
36 | + hum = (float((data[2] << 8)| data[3]) / powRes) * 100; | ||
37 | + } else { | ||
38 | + hum = (float((data[0] << 8)| data[1]) / powRes) * 100; | ||
39 | + } | ||
40 | + } | ||
41 | + | ||
42 | + /* | ||
43 | + * Permet de trouver le capteur d'humidité et son adresse. Assigne une adresse trouvée à | ||
44 | + * la variable 'adress_found'. | ||
45 | + */ | ||
46 | + bool find() { | ||
47 | + int count = 0; | ||
48 | + bool res = false; | ||
49 | + | ||
50 | + serial->printf("Searching for I2C devices...\n"); | ||
51 | + if (!i2c->write(device_address, NULL, 0)) //0 est renvoyé = capteur trouvé | ||
52 | + { | ||
53 | + serial->printf(" - I2C device found at address 0x%02X\n", device_address); | ||
54 | + address_found = device_address; | ||
55 | + res = true; | ||
56 | + count++; | ||
57 | + } | ||
58 | + serial->printf("%d devices found\n\n", count); | ||
59 | + return res; | ||
60 | + } | ||
61 | + | ||
62 | + /* | ||
63 | + * Permet de configurer le capteur d'humidité. | ||
64 | + */ | ||
65 | + void setup() { | ||
66 | + serial->printf("setting up device...\n"); | ||
67 | + char cmd[3] = {configReg_Address, 0x00, 0x00}; | ||
68 | + if(shouldReadTemperature){ | ||
69 | + cmd[1] = 0x10; | ||
70 | + } | ||
71 | + i2c->write(address_found, cmd, 3); | ||
72 | + serial->printf("finished set up\n"); | ||
73 | + } | ||
74 | + | ||
75 | + float getTemperature(){ | ||
76 | + return temp; | ||
77 | + } | ||
78 | + | ||
79 | + float getHumidity() { | ||
80 | + return hum; | ||
81 | + } | ||
82 | + | ||
83 | + bool hasReadTemperature() { | ||
84 | + return shouldReadTemperature; | ||
85 | + } | ||
86 | + | ||
87 | + | ||
88 | +private: | ||
89 | + unsigned char address_found; | ||
90 | + char triggerMes_Address; | ||
91 | + Serial *serial; | ||
92 | + I2C *i2c; | ||
93 | + bool shouldReadTemperature; | ||
94 | + float hum, temp; | ||
95 | + float powRes; | ||
96 | +}; | ||
97 | + | ||
98 | +#endif /* #ifndef __HUMIDITY_DEVICE_H__*/ | ||
0 | \ No newline at end of file | 99 | \ No newline at end of file |
@@ -0,0 +1,35 @@ | @@ -0,0 +1,35 @@ | ||
1 | +#include "mbed.h" | ||
2 | +#include "HumidityDevice.h" | ||
3 | + | ||
4 | +#define ADDRESS 0x80 | ||
5 | + | ||
6 | +I2C i2c(D14, D15); | ||
7 | +DigitalOut led1(LED1); | ||
8 | +Serial pc(SERIAL_TX, SERIAL_RX); | ||
9 | + | ||
10 | +int main() | ||
11 | +{ | ||
12 | + pc.printf("starting..."); | ||
13 | + HumidityDevice humidityDevice(&pc, &i2c, true); | ||
14 | + | ||
15 | + if(humidityDevice.find()){ | ||
16 | + humidityDevice.setup(); | ||
17 | + | ||
18 | + while(1) | ||
19 | + { | ||
20 | + led1 = 1; | ||
21 | + | ||
22 | + char *data; | ||
23 | + humidityDevice.read(data); | ||
24 | + | ||
25 | + if(humidityDevice.hasReadTemperature()){ | ||
26 | + pc.printf("Temperature : %f\n", humidityDevice.getTemperature()); | ||
27 | + pc.printf("Humidite : %f\n", humidityDevice.getHumidity()); | ||
28 | + } else { | ||
29 | + pc.printf("Humidite : %f\n", humidityDevice.getHumidity()); | ||
30 | + } | ||
31 | + led1 = 0; | ||
32 | + wait(0.5); | ||
33 | + } | ||
34 | + } | ||
35 | +} |
Mbed_compiler/Read_Pollution_Sensor/PollutionDevice.h
0 → 100644
@@ -0,0 +1,79 @@ | @@ -0,0 +1,79 @@ | ||
1 | +#ifndef __POLLUTION_DEVICE_H__ | ||
2 | +#define __POLLUTION_DEVICE_H__ | ||
3 | + | ||
4 | +#include "mbed.h" | ||
5 | + | ||
6 | +class PollutionDevice { | ||
7 | +public: | ||
8 | + const static int length = 31; | ||
9 | + const static int baud = 9600; | ||
10 | + | ||
11 | + PollutionDevice(Serial *uart) : uart(uart), PM01Value(0), PM2_5Value(0), PM10Value(0) {} | ||
12 | + | ||
13 | + /* | ||
14 | + * Permet de savoir si on lit le début de la trame associée au GPS. | ||
15 | + */ | ||
16 | + bool canBeRead() { | ||
17 | + return uart->readable() && uart->getc() == 0x42; | ||
18 | + } | ||
19 | + | ||
20 | + /* | ||
21 | + * Permet de lire la trame du capteur de pollution. | ||
22 | + */ | ||
23 | + void read() { | ||
24 | + char buf[length]; | ||
25 | + | ||
26 | + for(int i = 0; i < length; i++){ | ||
27 | + buf[i]=uart->getc(); | ||
28 | + } // full packet now in buffer. | ||
29 | + | ||
30 | + if(buf[0] == 0x4d){ | ||
31 | + if(checkValue(buf, length)){ | ||
32 | + PM01Value=((buf[3]<<8) + buf[4]); //count PM1.0 value of the air detector module | ||
33 | + PM2_5Value=((buf[5]<<8) + buf[6]);//count PM2.5 value of the air detector module | ||
34 | + PM10Value=((buf[7]<<8) + buf[8]); //count PM10 value of the air detector module | ||
35 | + } | ||
36 | + } | ||
37 | + } | ||
38 | + | ||
39 | + int getPM01() | ||
40 | + { | ||
41 | + return PM01Value; | ||
42 | + } | ||
43 | + | ||
44 | + int getPM2_5() | ||
45 | + { | ||
46 | + return PM2_5Value; | ||
47 | + } | ||
48 | + | ||
49 | + int getPM10() | ||
50 | + { | ||
51 | + return PM10Value; | ||
52 | + } | ||
53 | + | ||
54 | +private: | ||
55 | + Serial *uart; | ||
56 | + int PM01Value; //define PM1.0 value of the air detector module | ||
57 | + int PM2_5Value; //define PM2.5 value of the air detector module | ||
58 | + int PM10Value; //define PM10 value of the air detector module | ||
59 | + | ||
60 | + bool checkValue(char *buffer, char length) | ||
61 | + { | ||
62 | + bool flag = false; | ||
63 | + int sum = 0; | ||
64 | + | ||
65 | + for(int i = 0; i < (length - 2); i++){ | ||
66 | + sum = sum + buffer[i]; | ||
67 | + } | ||
68 | + sum = sum + 0x42; //0x42 : Le premier caractère qui n'est pas dans le buffer | ||
69 | + | ||
70 | + if(sum == ((buffer[length - 2] << 8) + buffer[length - 1])) //check the serial data | ||
71 | + { | ||
72 | + sum = 0; | ||
73 | + flag = true; | ||
74 | + } | ||
75 | + return flag; | ||
76 | + } | ||
77 | +}; | ||
78 | + | ||
79 | +#endif /* #ifndef __POLLUTION_DEVICE_H__*/ | ||
0 | \ No newline at end of file | 80 | \ No newline at end of file |
@@ -0,0 +1,25 @@ | @@ -0,0 +1,25 @@ | ||
1 | +#include "mbed.h" | ||
2 | +#include "PollutionDevice.h" | ||
3 | + | ||
4 | +DigitalOut led1(LED1); | ||
5 | +Serial pc(USBTX, USBRX, PollutionDevice::baud); | ||
6 | +Serial uart(D8, D2, PollutionDevice::baud); | ||
7 | + | ||
8 | +int main() | ||
9 | +{ | ||
10 | + pc.printf("starting...\n\n"); | ||
11 | + PollutionDevice pollutionDevice(&uart); | ||
12 | + | ||
13 | + while(1) { | ||
14 | + if(pollutionDevice.canBeRead()){ | ||
15 | + pollutionDevice.read(); | ||
16 | + pc.printf("PM1.0: %d ug/m3\nPM2.5: %d ug/m3\nPM10: %d ug/m3\n\n", pollutionDevice.getPM01(), pollutionDevice.getPM2_5(), pollutionDevice.getPM10()); | ||
17 | + | ||
18 | + led1 = !led1; | ||
19 | + } | ||
20 | + } | ||
21 | +} | ||
22 | + | ||
23 | + | ||
24 | + | ||
25 | + |
No preview for this file type
@@ -0,0 +1,15 @@ | @@ -0,0 +1,15 @@ | ||
1 | +#include "mbed.h" | ||
2 | +#include "LPS22HB.h" | ||
3 | + | ||
4 | +// I2C Communication | ||
5 | +I2C i2c(D14, D15); // SDA, SCL | ||
6 | +LPS22HB baro(i2c, LPS22HB_G_CHIP_ADDR); | ||
7 | +Serial pc(SERIAL_TX, SERIAL_RX); | ||
8 | + | ||
9 | +int main() { | ||
10 | + while( true){ | ||
11 | + baro.get(); | ||
12 | + pc.printf("Pressure: 0x%6.1f, Temperature: 0x%+4.1f\r\n", baro.pressure(), baro.temperature()); | ||
13 | + wait(1.0); | ||
14 | + } | ||
15 | +} | ||
0 | \ No newline at end of file | 16 | \ No newline at end of file |
@@ -0,0 +1,50 @@ | @@ -0,0 +1,50 @@ | ||
1 | +#ifndef __RTC_DEVICE_H__ | ||
2 | +#define __RTC_DEVICE_H__ | ||
3 | + | ||
4 | +#include "mbed.h" | ||
5 | + | ||
6 | +class RTCDevice { | ||
7 | +public: | ||
8 | + const static char firstReg_Address = 0x02; | ||
9 | + const static char device_address = 0xA2; | ||
10 | + const static int length = 7; | ||
11 | + | ||
12 | + RTCDevice(Serial *serial, I2C *i2c) : serial(serial), i2c(i2c) {} | ||
13 | + | ||
14 | + /* | ||
15 | + * Permet de lire le RTC et d'obtenir la date et l'heure. | ||
16 | + */ | ||
17 | + void read(char data[]) { | ||
18 | + char cmd[1]={firstReg_Address}; | ||
19 | + i2c->write(address_found, cmd, 1); | ||
20 | + wait(0.2); | ||
21 | + i2c->read(address_found, data, length); | ||
22 | + } | ||
23 | + | ||
24 | + /* | ||
25 | + * Permet de trouver le RTC et son adresse. Assigne une adresse trouvée à | ||
26 | + * la variable 'adress_found'. | ||
27 | + */ | ||
28 | + bool find() { | ||
29 | + int count = 0; | ||
30 | + bool res = false; | ||
31 | + | ||
32 | + serial->printf("Searching for I2C devices...\n"); | ||
33 | + if (!i2c->write(device_address, NULL, 0)) //0 est renvoyé = capteur trouvé | ||
34 | + { | ||
35 | + serial->printf(" - I2C device found at address 0x%02X\n", device_address); | ||
36 | + address_found = device_address; | ||
37 | + res = true; | ||
38 | + count++; | ||
39 | + } | ||
40 | + serial->printf("%d devices found\n\n", count); | ||
41 | + return res; | ||
42 | + } | ||
43 | + | ||
44 | +private: | ||
45 | + unsigned char address_found; | ||
46 | + Serial *serial; | ||
47 | + I2C *i2c; | ||
48 | +}; | ||
49 | + | ||
50 | +#endif /* #ifndef __RTC_DEVICE_H__*/ | ||
0 | \ No newline at end of file | 51 | \ No newline at end of file |
@@ -0,0 +1,29 @@ | @@ -0,0 +1,29 @@ | ||
1 | +#include "mbed.h" | ||
2 | +#include "RTCDevice.h" | ||
3 | + | ||
4 | +I2C i2c(D14, D15); | ||
5 | +DigitalOut led1(LED1); | ||
6 | +Serial pc(SERIAL_TX, SERIAL_RX); | ||
7 | + | ||
8 | +char data[RTCDevice::length]; | ||
9 | + | ||
10 | +int main() | ||
11 | +{ | ||
12 | + pc.printf("starting..."); | ||
13 | + RTCDevice rtcDevice(&pc, &i2c); | ||
14 | + | ||
15 | + if(rtcDevice.find()){ | ||
16 | + while(1) | ||
17 | + { | ||
18 | + led1 = 1; | ||
19 | + | ||
20 | + rtcDevice.read(data); | ||
21 | + pc.printf("Time : %d%d:%d%d:%d%d\n", data[2] & 0x30 >> 4, data[2] & 0x0F, data[1] & 0x70 >> 4, data[1] & 0x0F, data[0] & 0x70 >> 4, data[0] & 0x0F); | ||
22 | + pc.printf("Date : %d%d/%d%d/20%d%d\n", data[3] & 0x30 >> 4, data[3] & 0x0F, data[5] & 0x10 >> 4, data[5] & 0x0F, data[6] & 0xF0 >> 4, data[6] & 0x0F); | ||
23 | + | ||
24 | + led1 = 0; | ||
25 | + wait(0.5); | ||
26 | + } | ||
27 | + } | ||
28 | +} | ||
29 | + |