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@5:0d9e292a9d06, 2019-03-08 (annotated)
- Committer:
- loicguibert
- Date:
- Fri Mar 08 08:28:14 2019 +0000
- Revision:
- 5:0d9e292a9d06
- Parent:
- 4:bfe306335065
- Child:
- 6:5f56e551f84f
Libraries done, except Logger.cpp.
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 | 4:bfe306335065 | 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 | 4:bfe306335065 | 8 | WeatherStation::WeatherStation(NRFDevKit& nrfDevKit, Logger& logger) |
| loicguibert | 5:0d9e292a9d06 | 9 | : |
| loicguibert | 5:0d9e292a9d06 | 10 | m_nrfDevKit(nrfDevKit), |
| loicguibert | 5:0d9e292a9d06 | 11 | m_logger(logger), |
| loicguibert | 5:0d9e292a9d06 | 12 | m_lps25hb(m_nrfDevKit.getSDA(), m_nrfDevKit.getSCL(), logger), |
| loicguibert | 5:0d9e292a9d06 | 13 | m_hdc1000(m_nrfDevKit.getSDA(), m_nrfDevKit.getSCL(), m_nrfDevKit.getDataRdy(), logger){ |
| loicguibert | 4:bfe306335065 | 14 | } |
| loicguibert | 4:bfe306335065 | 15 | |
| loicguibert | 4:bfe306335065 | 16 | void WeatherStation::start() { |
| loicguibert | 4:bfe306335065 | 17 | m_logger.log("WeatherStation is starting\r\n"); |
| loicguibert | 5:0d9e292a9d06 | 18 | |
| loicguibert | 5:0d9e292a9d06 | 19 | |
| loicguibert | 4:bfe306335065 | 20 | // make sure that the LPW25HB device is present |
| loicguibert | 4:bfe306335065 | 21 | // if not log an error and return |
| loicguibert | 5:0d9e292a9d06 | 22 | if (m_lps25hb.probe()) { |
| loicguibert | 5:0d9e292a9d06 | 23 | m_logger.log("LPS25HB device found\r\n"); |
| loicguibert | 5:0d9e292a9d06 | 24 | } else { |
| loicguibert | 5:0d9e292a9d06 | 25 | m_logger.log("Device LPS25HB not found!\r\n"); |
| loicguibert | 5:0d9e292a9d06 | 26 | return; |
| loicguibert | 5:0d9e292a9d06 | 27 | } |
| loicguibert | 5:0d9e292a9d06 | 28 | |
| loicguibert | 5:0d9e292a9d06 | 29 | |
| loicguibert | 4:bfe306335065 | 30 | // make sure that the HDC1000 device is present |
| loicguibert | 4:bfe306335065 | 31 | // if not log an error and return |
| loicguibert | 5:0d9e292a9d06 | 32 | if (m_hdc1000.probe()) { |
| loicguibert | 5:0d9e292a9d06 | 33 | m_logger.log("HDC1000 device found\r\n"); |
| loicguibert | 5:0d9e292a9d06 | 34 | } else { |
| loicguibert | 5:0d9e292a9d06 | 35 | m_logger.log("Device HDC1000 not found!\r\n"); |
| loicguibert | 5:0d9e292a9d06 | 36 | return; |
| loicguibert | 5:0d9e292a9d06 | 37 | } |
| loicguibert | 5:0d9e292a9d06 | 38 | |
| loicguibert | 4:bfe306335065 | 39 | |
| loicguibert | 4:bfe306335065 | 40 | // to show we're running we'll blink every m_blinkInterval milliseconds |
| loicguibert | 4:bfe306335065 | 41 | // the syntax used here is the following: |
| loicguibert | 4:bfe306335065 | 42 | // 1. the first argument represents the interval in milliseconds at which the event is executed |
| loicguibert | 4:bfe306335065 | 43 | // 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 | 44 | // 3. the third argument is the address of the method to be called (applied to the object passed as argument 2) |
| loicguibert | 4:bfe306335065 | 45 | m_eventQueue.call_every(m_blinkInterval, &m_nrfDevKit, &NRFDevKit::blink1); |
| loicguibert | 4:bfe306335065 | 46 | |
| loicguibert | 4:bfe306335065 | 47 | // schedule measurements every m_measurementInterval milliseconds |
| loicguibert | 4:bfe306335065 | 48 | // apply the same syntax as in the previous call |
| loicguibert | 4:bfe306335065 | 49 | // 1. the measurement interval is m_measurementInterval |
| loicguibert | 4:bfe306335065 | 50 | // 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 | 51 | // 3. the method to call is performMeasurements() |
| loicguibert | 4:bfe306335065 | 52 | // m_eventQueue.call_every(TO COMPLETED); |
| loicguibert | 4:bfe306335065 | 53 | |
| loicguibert | 4:bfe306335065 | 54 | m_eventQueue.call_every(m_measurementInterval, this, &WeatherStation::performMeasurements); |
| loicguibert | 4:bfe306335065 | 55 | |
| loicguibert | 4:bfe306335065 | 56 | // this will not return until shutdown |
| loicguibert | 4:bfe306335065 | 57 | m_eventQueue.dispatch_forever(); |
| loicguibert | 4:bfe306335065 | 58 | } |
| loicguibert | 4:bfe306335065 | 59 | |
| loicguibert | 4:bfe306335065 | 60 | void WeatherStation::performMeasurements(void) { |
| loicguibert | 5:0d9e292a9d06 | 61 | m_logger.log("\r\nPerforming measurements:\r\n"); |
| loicguibert | 4:bfe306335065 | 62 | |
| loicguibert | 4:bfe306335065 | 63 | // get and log pressure |
| loicguibert | 5:0d9e292a9d06 | 64 | m_logger.log(" Pressure: %.02f hPa\r\n", m_lps25hb.getPressure()); |
| loicguibert | 4:bfe306335065 | 65 | |
| loicguibert | 4:bfe306335065 | 66 | // get and log temperature |
| loicguibert | 5:0d9e292a9d06 | 67 | m_logger.log(" Temperature: %.02f C\r\n", m_hdc1000.getTemperature()); |
| loicguibert | 4:bfe306335065 | 68 | |
| loicguibert | 4:bfe306335065 | 69 | // get and log humidity |
| loicguibert | 5:0d9e292a9d06 | 70 | m_logger.log(" Humidiy: %.02f %%\r\n", m_hdc1000.getHumidity()); |
| loicguibert | 4:bfe306335065 | 71 | |
| loicguibert | 4:bfe306335065 | 72 | // get and log the battery level |
| loicguibert | 5:0d9e292a9d06 | 73 | m_logger.log(" Battery: %u mV\r\n", m_nrfDevKit.getBatteryLevel()); |
| loicguibert | 4:bfe306335065 | 74 | |
| loicguibert | 5:0d9e292a9d06 | 75 | } |