From 2d2ca44ef5cab26ddcf60d7fcd2a336217d2f58e Mon Sep 17 00:00:00 2001 From: Felix Suchert Date: Tue, 28 Mar 2023 19:09:35 +0200 Subject: [PATCH] feat: use different bitmaps for weather condition encoding --- assets/cloudsun.bmp | Bin 0 -> 402 bytes assets/cloudy.bmp | Bin 0 -> 402 bytes assets/drizzle.bmp | Bin 0 -> 402 bytes assets/rain.bmp | Bin 0 -> 402 bytes assets/snow.bmp | Bin 0 -> 402 bytes assets/sunny.bmp | Bin 0 -> 402 bytes assets/thunder.bmp | Bin 0 -> 402 bytes rain2.bmp | Bin 402 -> 0 bytes src/lib.rs | 2 +- src/weather/data.rs | 40 +++++++++++++++++++++++++++++++++++++++- src/weather/icons.rs | 25 +++++++++++++++++++++++++ src/weather/mod.rs | 5 ++--- 12 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 assets/cloudsun.bmp create mode 100644 assets/cloudy.bmp create mode 100644 assets/drizzle.bmp create mode 100644 assets/rain.bmp create mode 100644 assets/snow.bmp create mode 100644 assets/sunny.bmp create mode 100644 assets/thunder.bmp delete mode 100644 rain2.bmp create mode 100644 src/weather/icons.rs diff --git a/assets/cloudsun.bmp b/assets/cloudsun.bmp new file mode 100644 index 0000000000000000000000000000000000000000..f7be29de47fa46ed175c46e85288babd728e18c3 GIT binary patch literal 402 zcma*iF-ikL6o>I|wMbzq58!$P+f)N-QrUO{?_ebeYiA<}=>#EH38b)dk64mjNs%IjAPXoXWP<}YuF`mdKt@PqFbG#J4PHR21YD@p3IZ2yrA~rF zPdG_wQsw17eg_}-ck_P9m~#ORz#H%iWUP%0UY?hFfVsfmF=w;geJ!*9C++n&Djo1B zwCN5)tq#IjjY6eP0Yb7^-L8JF{rzIxuj@v*s{IJPdc47?=JjHzAnKGCvylK@IeoMR zwB?Z5KvQ&ivN~Oz{eD^;3a!$4XNLfdxm^i$+E(dq_MK`Dz@U{34~n0tJtN?G14k&5?JzNHk(AOeC+Ve zzWHH_W0?_E0vBKp?0}3^$Y8r3V+Aol(~Bw&r}LQoFWdDiNfw5MgAe214gTBj)kZ(4 zkDU9`ye)Sy7;^|-(j9s$*NM%-*8GNd@WvRR=#<-KRPlgYVO=Z_67v7#-wJ(he3|8&{?)C+jCJEa%St2zi~$ ujU)$h(%gUs>9+$#X|nsHRNfJ#(dto$M`O}=WBk)s`QQBM3Nu}ywFO6feW*YH literal 0 HcmV?d00001 diff --git a/assets/snow.bmp b/assets/snow.bmp new file mode 100644 index 0000000000000000000000000000000000000000..e157c0665f118e19e7710000f52d4a489c2ddc89 GIT binary patch literal 402 zcma)%F%H5o3`Kug7}&S~9Dy4kbzx%RDy%5ELk~o5LOB85+NyDCCM;Pm*0-JS z`r1^;(ExY=H~}~Us8ECo*zYtqKu(OndkY+N6!qnHFVDY2cKrr3gLxK!xo?$IP?A>` zJ$mx3U)eu8In51Pz!yr)X$F0u5LaV>1Kp|N?R#@18vn%Yv&KkRH>^G)e9 k%JpA8%WM!G#E{-XLIOW#d)06~nRzSnvYg#Mq_G5mKg%O9~Ae@+~tj zL9z1V4Zk<nt}?cvAqka8qCdH{lcttNz7k;`F{69>~@5G?OE^->Kw6+ literal 0 HcmV?d00001 diff --git a/rain2.bmp b/rain2.bmp deleted file mode 100644 index 223b185917df5563851724772183c4966fad4592..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 402 zcma)%y$!-Z428eY(lG%eutB6WR5YwY8AQYiSs;@{hzYVmQl`A}K>2ydmjnuT%s0zC1#RsaA1 diff --git a/src/lib.rs b/src/lib.rs index 3dd090b..1e0d548 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,7 +69,7 @@ where .expect("error drawing text"); } - let bmp_data = include_bytes!("../rain2.bmp"); + let bmp_data = include_bytes!("../assets/rain.bmp"); // Load 16 BPP 8x8px image. // Note: The color type is specified explicitly to match the format used by the BMP image. diff --git a/src/weather/data.rs b/src/weather/data.rs index 3d189b4..57506b5 100644 --- a/src/weather/data.rs +++ b/src/weather/data.rs @@ -7,6 +7,9 @@ use embedded_plots::curve::PlotPoint; use serde::Deserialize; +use ssd1675::Color; +use tinybmp::Bmp; +use super::icons; /// Response from the OpenWeather API with both current and forecast data for a given location. #[derive(Deserialize)] @@ -55,6 +58,10 @@ impl WeatherData { self.current.data.uvi.round() as u8 } + pub fn current_condition(&self) -> Bmp<'static, Color> { + unimplemented!() + } + pub fn forecast_plot_data(&self) -> Vec { let data: Vec = self.hourly.iter().take(24).map(|h| h.data.temp).collect(); @@ -186,6 +193,7 @@ pub enum WeatherCondition { Squall, Tornado, Clear, + SunClouds, Clouds, } @@ -207,9 +215,39 @@ impl From for WeatherCondition { 771 => Self::Squall, 781 => Self::Tornado, 800 => Self::Clear, - 801..=804 => Self::Clouds, + 801|802 => Self::SunClouds, + 803|804 => Self::Clouds, // NOTE(feliix42): There's no `Error` mapping or something like that. _ => Self::Clear, } } } + +impl Into> for WeatherCondition { + /// gets a bitmap for each current weather condition. + fn into(self) -> Bmp<'static, Color> { + let bmp = match self { + Self::Thunderstorm => icons::THUNDER, + Self::Drizzle => icons::DRIZZLE, + Self::Rain => icons::RAIN, + Self::Snow => icons::SNOW, + Self::Mist => icons::MIST, + Self::Smoke => icons::SMOKE, + Self::Haze => icons::HAZE, + Self::SandDust => icons::SANDDUST, + Self::Fog => icons::FOG, + Self::Sand => icons::SAND, + Self::Dust => icons::DUST, + Self::Ash => icons::ASH, + Self::Squall => icons::SQUALL, + Self::Tornado => icons::TORNADO, + // TODO(feliix42): Show Moon(phases) here after sundown + Self::Clear => icons::CLEAR, + Self::Clouds => icons::CLOUDS, + Self::SunClouds => icons::SUNCLOUDS, + }; + + Bmp::from_slice(bmp).unwrap() + } +} + diff --git a/src/weather/icons.rs b/src/weather/icons.rs new file mode 100644 index 0000000..f063051 --- /dev/null +++ b/src/weather/icons.rs @@ -0,0 +1,25 @@ +//! Icons for different weather conditions. +//! +//! This module contains raw byte arrays for different weather conditions supported by the +//! OpenWeather API v3. They are loaded and parsed by the `WeatherCondition::into` function. + +pub static THUNDER: &'static [u8] = include_bytes!("../../assets/thunder.bmp"); +pub static DRIZZLE: &'static [u8] = include_bytes!("../../assets/drizzle.bmp"); +pub static RAIN: &'static [u8] = include_bytes!("../../assets/rain.bmp"); +pub static SNOW: &'static [u8] = include_bytes!("../../assets/snow.bmp"); + +// TODO(feliix42): replace assets +pub static MIST: &'static [u8] = include_bytes!("../../assets/rain.bmp"); +pub static SMOKE: &'static [u8] = include_bytes!("../../assets/rain.bmp"); +pub static HAZE: &'static [u8] = include_bytes!("../../assets/rain.bmp"); +pub static SANDDUST: &'static [u8] = include_bytes!("../../assets/rain.bmp"); +pub static FOG: &'static [u8] = include_bytes!("../../assets/rain.bmp"); +pub static SAND: &'static [u8] = include_bytes!("../../assets/rain.bmp"); +pub static DUST: &'static [u8] = include_bytes!("../../assets/rain.bmp"); +pub static ASH: &'static [u8] = include_bytes!("../../assets/rain.bmp"); +pub static SQUALL: &'static [u8] = include_bytes!("../../assets/rain.bmp"); +pub static TORNADO: &'static [u8] = include_bytes!("../../assets/rain.bmp"); + +pub static CLEAR: &'static [u8] = include_bytes!("../../assets/sunny.bmp"); +pub static CLOUDS: &'static [u8] = include_bytes!("../../assets/cloudy.bmp"); +pub static SUNCLOUDS: &'static [u8] = include_bytes!("../../assets/cloudsun.bmp"); diff --git a/src/weather/mod.rs b/src/weather/mod.rs index ac1c447..e1baf32 100644 --- a/src/weather/mod.rs +++ b/src/weather/mod.rs @@ -12,11 +12,11 @@ use profont::{ use ssd1675::Color; use std::error::Error; use std::fmt::Debug; -use tinybmp::Bmp; mod api; mod data; mod secret; +mod icons; pub fn get_weather(display: &mut D) where @@ -32,8 +32,7 @@ where }; // draw image - let bmp_data = include_bytes!("../../rain2.bmp"); - let bmp = Bmp::::from_slice(bmp_data).unwrap(); + let bmp = weather_data.current_condition(); Image::new(&bmp, Point::new(10, 10)) .draw(display) .expect("Failed to draw image");