Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
WeatherStation.cpp@11:dbc310addbf6, 2019-03-19 (annotated)
- Committer:
- loicguibert
- Date:
- Tue Mar 19 20:48:48 2019 +0000
- Revision:
- 11:dbc310addbf6
- Parent:
- 6:5f56e551f84f
- Child:
- 12:856286ad4cdc
Point 5 done
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| loicguibert | 4:bfe306335065 | 1 | #include "WeatherStation.h" |
| loicguibert | 4:bfe306335065 | 2 | |
| loicguibert | 4:bfe306335065 | 3 | // initialization of static data members |
| loicguibert | 11:dbc310addbf6 | 4 | //const int WeatherStation::m_blinkInterval = 500; |
| loicguibert | 4:bfe306335065 | 5 | const int WeatherStation::m_measurementInterval = 10000; |
| loicguibert | 4:bfe306335065 | 6 | |
| loicguibert | 4:bfe306335065 | 7 | // constructor |
| loicguibert | 11:dbc310addbf6 | 8 | WeatherStation::WeatherStation(IDevKit& iDevKit, Logger& logger, BLE& ble) : |
| loicguibert | 11:dbc310addbf6 | 9 | GAPPeripheral(ble, iDevKit, "Spada-Guibert", GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED, m_eventQueue, logger), |
| loicguibert | 11:dbc310addbf6 | 10 | m_iDevKit(iDevKit), |
| loicguibert | 5:0d9e292a9d06 | 11 | m_logger(logger), |
| loicguibert | 11:dbc310addbf6 | 12 | //m_peripheral(ble, iDevKit, "Spada-Guibert", GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED,m_eventQueue, logger), |
| loicguibert | 11:dbc310addbf6 | 13 | m_lps25hb(m_iDevKit.getSDA(), m_iDevKit.getSCL(), m_logger), |
| loicguibert | 11:dbc310addbf6 | 14 | m_hdc1000(m_iDevKit.getSDA(), m_iDevKit.getSCL(), m_iDevKit.getDataRdy(), m_logger) { |
| loicguibert | 4:bfe306335065 | 15 | } |
| loicguibert | 4:bfe306335065 | 16 | |
| loicguibert | 4:bfe306335065 | 17 | void WeatherStation::start() { |
| loicguibert | 4:bfe306335065 | 18 | m_logger.log("WeatherStation is starting\r\n"); |
| loicguibert | 5:0d9e292a9d06 | 19 | |
| loicguibert | 5:0d9e292a9d06 | 20 | |
| loicguibert | 4:bfe306335065 | 21 | // make sure that the LPW25HB device is present |
| loicguibert | 4:bfe306335065 | 22 | // if not log an error and return |
| loicguibert | 5:0d9e292a9d06 | 23 | if (m_lps25hb.probe()) { |
| loicguibert | 5:0d9e292a9d06 | 24 | m_logger.log("LPS25HB device found\r\n"); |
| loicguibert | 5:0d9e292a9d06 | 25 | } else { |
| loicguibert | 5:0d9e292a9d06 | 26 | m_logger.log("Device LPS25HB not found!\r\n"); |
| loicguibert | 5:0d9e292a9d06 | 27 | return; |
| loicguibert | 5:0d9e292a9d06 | 28 | } |
| loicguibert | 5:0d9e292a9d06 | 29 | |
| loicguibert | 5:0d9e292a9d06 | 30 | |
| loicguibert | 4:bfe306335065 | 31 | // make sure that the HDC1000 device is present |
| loicguibert | 4:bfe306335065 | 32 | // if not log an error and return |
| loicguibert | 5:0d9e292a9d06 | 33 | if (m_hdc1000.probe()) { |
| loicguibert | 5:0d9e292a9d06 | 34 | m_logger.log("HDC1000 device found\r\n"); |
| loicguibert | 5:0d9e292a9d06 | 35 | } else { |
| loicguibert | 5:0d9e292a9d06 | 36 | m_logger.log("Device HDC1000 not found!\r\n"); |
| loicguibert | 5:0d9e292a9d06 | 37 | return; |
| loicguibert | 5:0d9e292a9d06 | 38 | } |
| loicguibert | 6:5f56e551f84f | 39 | |
| loicguibert | 6:5f56e551f84f | 40 | //Cleaning the console by the addition of a blank line |
| loicguibert | 6:5f56e551f84f | 41 | m_logger.log("\r\n"); |
| loicguibert | 5:0d9e292a9d06 | 42 | |
| loicguibert | 4:bfe306335065 | 43 | |
| loicguibert | 4:bfe306335065 | 44 | // to show we're running we'll blink every m_blinkInterval milliseconds |
| loicguibert | 4:bfe306335065 | 45 | // the syntax used here is the following: |
| loicguibert | 4:bfe306335065 | 46 | // 1. the first argument represents the interval in milliseconds at which the event is executed |
| loicguibert | 4:bfe306335065 | 47 | // 2. the second argument represents the address of instance of the object on which the method passed in argument 3 will be applied |
| loicguibert | 4:bfe306335065 | 48 | // 3. the third argument is the address of the method to be called (applied to the object passed as argument 2) |
| loicguibert | 11:dbc310addbf6 | 49 | //m_eventQueue.call_every(m_blinkInterval, &m_iDevKit, &IDevKit::toggleLed1); |
| loicguibert | 4:bfe306335065 | 50 | |
| loicguibert | 4:bfe306335065 | 51 | // schedule measurements every m_measurementInterval milliseconds |
| loicguibert | 4:bfe306335065 | 52 | // apply the same syntax as in the previous call |
| loicguibert | 4:bfe306335065 | 53 | // 1. the measurement interval is m_measurementInterval |
| loicguibert | 4:bfe306335065 | 54 | // 2. the object is the WeatherStation itself (recall how you refer to the current object in Java - the same applies in c++) |
| loicguibert | 4:bfe306335065 | 55 | // 3. the method to call is performMeasurements() |
| loicguibert | 4:bfe306335065 | 56 | // m_eventQueue.call_every(TO COMPLETED); |
| loicguibert | 4:bfe306335065 | 57 | |
| loicguibert | 4:bfe306335065 | 58 | m_eventQueue.call_every(m_measurementInterval, this, &WeatherStation::performMeasurements); |
| loicguibert | 11:dbc310addbf6 | 59 | |
| loicguibert | 11:dbc310addbf6 | 60 | // Starting the BLE peripheral |
| loicguibert | 11:dbc310addbf6 | 61 | // the syntax used here is the following: |
| loicguibert | 11:dbc310addbf6 | 62 | // 1. the first argument represents the address of instance of the object on which the method passed in argument 3 will be applied |
| loicguibert | 11:dbc310addbf6 | 63 | // 2. the second argument is the address of the method to be called (applied to the object passed as argument 2) |
| loicguibert | 11:dbc310addbf6 | 64 | m_eventQueue.call(this, &WeatherStation::advertise); |
| loicguibert | 4:bfe306335065 | 65 | |
| loicguibert | 4:bfe306335065 | 66 | // this will not return until shutdown |
| loicguibert | 4:bfe306335065 | 67 | m_eventQueue.dispatch_forever(); |
| loicguibert | 4:bfe306335065 | 68 | } |
| loicguibert | 11:dbc310addbf6 | 69 | |
| loicguibert | 11:dbc310addbf6 | 70 | void WeatherStation::advertise(void){ |
| loicguibert | 11:dbc310addbf6 | 71 | GAPPeripheral::advertise(); |
| loicguibert | 11:dbc310addbf6 | 72 | } |
| loicguibert | 4:bfe306335065 | 73 | |
| loicguibert | 4:bfe306335065 | 74 | void WeatherStation::performMeasurements(void) { |
| loicguibert | 6:5f56e551f84f | 75 | m_logger.log("Performing measurements:\r\n"); |
| loicguibert | 4:bfe306335065 | 76 | |
| loicguibert | 4:bfe306335065 | 77 | // get and log pressure |
| loicguibert | 6:5f56e551f84f | 78 | m_logger.log("Pressure: %.02f hPa\r\n", m_lps25hb.getPressure()); |
| loicguibert | 4:bfe306335065 | 79 | |
| loicguibert | 4:bfe306335065 | 80 | // get and log temperature |
| loicguibert | 6:5f56e551f84f | 81 | m_logger.log("Temperature: %.02f C\r\n", m_hdc1000.getTemperature()); |
| loicguibert | 4:bfe306335065 | 82 | |
| loicguibert | 4:bfe306335065 | 83 | // get and log humidity |
| loicguibert | 6:5f56e551f84f | 84 | m_logger.log("Humidiy: %.02f %%\r\n", m_hdc1000.getHumidity()); |
| loicguibert | 4:bfe306335065 | 85 | |
| loicguibert | 4:bfe306335065 | 86 | // get and log the battery level |
| loicguibert | 11:dbc310addbf6 | 87 | m_logger.log("Battery: %u mV\r\n", m_iDevKit.getBatteryLevel()); |
| loicguibert | 6:5f56e551f84f | 88 | |
| loicguibert | 6:5f56e551f84f | 89 | //Cleaning the console by the addition of a blank line |
| loicguibert | 6:5f56e551f84f | 90 | m_logger.log("\r\n"); |
| loicguibert | 4:bfe306335065 | 91 | |
| loicguibert | 5:0d9e292a9d06 | 92 | } |