aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/ble/SimpleWeatherService.cpp
diff options
context:
space:
mode:
authorJean-François Milants <jf@codingfield.com>2023-12-18 18:07:36 +0100
committerJF <JF002@users.noreply.github.com>2023-12-23 21:12:25 +0100
commit3a8c7dc038605f7a0ebefa479b204d6d019743cd (patch)
tree28e759d16ae5cc41740819246e02d42bfc331ebe /src/components/ble/SimpleWeatherService.cpp
parentfe4b07c610261df6b2d63e5b216248a6c8e78d8c (diff)
Simple Weather Service - code cleaning and improvements
Add missing icons (heavy clouds, thunderstorm, snow). Remove unneeded comparison operator (!=), improve conversion of Timestamp and MessageType, order includes. Fix typo in documentation. Remove not related change in StopWatch.
Diffstat (limited to 'src/components/ble/SimpleWeatherService.cpp')
-rw-r--r--src/components/ble/SimpleWeatherService.cpp39
1 files changed, 15 insertions, 24 deletions
diff --git a/src/components/ble/SimpleWeatherService.cpp b/src/components/ble/SimpleWeatherService.cpp
index 9735f2d1..3635fdfd 100644
--- a/src/components/ble/SimpleWeatherService.cpp
+++ b/src/components/ble/SimpleWeatherService.cpp
@@ -15,24 +15,27 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
+
+#include "components/ble/SimpleWeatherService.h"
+
#include <algorithm>
-#include "SimpleWeatherService.h"
#include <cstring>
#include <nrf_log.h>
-#include <array>
using namespace Pinetime::Controllers;
namespace {
- enum class MessageType { CurrentWeather, Forecast, Unknown };
+ enum class MessageType : uint8_t { CurrentWeather, Forecast, Unknown };
+
+ uint64_t ToUInt64(const uint8_t* data) {
+ return *(reinterpret_cast<const uint64_t*>(data));
+ }
SimpleWeatherService::CurrentWeather CreateCurrentWeather(const uint8_t* dataBuffer) {
char cityName[33];
std::memcpy(&cityName[0], &dataBuffer[13], 32);
cityName[32] = '\0';
- return SimpleWeatherService::CurrentWeather {dataBuffer[2] + (dataBuffer[3] << 8) + (dataBuffer[4] << 16) + (dataBuffer[5] << 24) +
- ((uint64_t) dataBuffer[6] << 32) + ((uint64_t) dataBuffer[7] << 40) +
- ((uint64_t) dataBuffer[8] << 48) + ((uint64_t) dataBuffer[9] << 54),
+ return SimpleWeatherService::CurrentWeather {ToUInt64(&dataBuffer[2]),
dataBuffer[10],
dataBuffer[11],
dataBuffer[12],
@@ -41,9 +44,7 @@ namespace {
}
SimpleWeatherService::Forecast CreateForecast(const uint8_t* dataBuffer) {
- uint64_t timestamp = static_cast<uint64_t>(dataBuffer[2] + (dataBuffer[3] << 8) + (dataBuffer[4] << 16) + (dataBuffer[5] << 24) +
- ((uint64_t) dataBuffer[6] << 32) + ((uint64_t) dataBuffer[7] << 40) +
- ((uint64_t) dataBuffer[8] << 48) + ((uint64_t) dataBuffer[9] << 54));
+ auto timestamp = static_cast<uint64_t>(ToUInt64(&dataBuffer[2]));
std::array<SimpleWeatherService::Forecast::Day, SimpleWeatherService::MaxNbForecastDays> days;
const uint8_t nbDaysInBuffer = dataBuffer[10];
@@ -54,19 +55,13 @@ namespace {
return SimpleWeatherService::Forecast {timestamp, nbDays, days};
}
- MessageType GetMessageType(const uint8_t* dataBuffer) {
- switch (dataBuffer[0]) {
- case 0:
- return MessageType::CurrentWeather;
- break;
- case 1:
- return MessageType::Forecast;
- break;
- default:
+ MessageType GetMessageType(const uint8_t* data) {
+ auto messageType = static_cast<MessageType>(*data);
+ if(messageType > MessageType::Unknown) {
return MessageType::Unknown;
- break;
+ }
+ return messageType;
}
- }
uint8_t GetVersion(const uint8_t* dataBuffer) {
return dataBuffer[1];
@@ -154,7 +149,3 @@ bool SimpleWeatherService::CurrentWeather::operator==(const SimpleWeatherService
return this->iconId == other.iconId && this->temperature == other.temperature && this->timestamp == other.timestamp &&
this->maxTemperature == other.maxTemperature && this->minTemperature == other.maxTemperature;
}
-
-bool SimpleWeatherService::CurrentWeather::operator!=(const SimpleWeatherService::CurrentWeather& other) const {
- return !operator==(other);
-}