From 8c26252aeb5a83fa2a351005efdbc2f5f0ddbce8 Mon Sep 17 00:00:00 2001 From: c3ma Date: Wed, 31 May 2023 21:30:21 +0200 Subject: [PATCH] weather icon initial --- .vscode/extensions.json | 10 +++++ client/Cargo.toml | 2 + client/src/main.rs | 97 ++++++++++++++++++++++++++-------------- client/src/sun.bmp | Bin 0 -> 4938 bytes 4 files changed, 75 insertions(+), 34 deletions(-) create mode 100644 .vscode/extensions.json create mode 100644 client/src/sun.bmp diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/client/Cargo.toml b/client/Cargo.toml index f24be74..d0377ca 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -9,3 +9,5 @@ edition = "2021" bit = "0.1.1" embedded-graphics = "0.8.0" openweathermap = "0.2.4" +substring = "1.4.5" +tinybmp = "0.5.0" diff --git a/client/src/main.rs b/client/src/main.rs index a5882ff..4c54ee9 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -1,4 +1,6 @@ use bit::BitIndex; +use substring::Substring; +use tinybmp::Bmp; use core::time; use embedded_graphics::{ image::{Image, ImageRaw}, @@ -8,7 +10,7 @@ use embedded_graphics::{ primitives::{Line, PrimitiveStyle}, text::Text, }; -use openweathermap::{self, CurrentWeather}; +use openweathermap::{self, CurrentWeather, Weather}; use std::net::UdpSocket; use std::{env, sync::mpsc::Receiver, thread}; @@ -21,6 +23,11 @@ const IMAGE_HEIGHT_BYTE: u32 = 40; const IMAGE_LENGTH: usize = (IMAGE_WIDTH_BYTE * IMAGE_HEIGHT_BYTE) as usize; const PACKAGE_LENGTH: usize = (IMAGE_LENGTH + 1) as usize; +const PRIMITIVE_STYLE:PrimitiveStyle = PrimitiveStyle::with_stroke(BinaryColor::On, 1); + + + + struct UdpDisplay { image: [u8; IMAGE_SIZE_BYTE], } @@ -98,6 +105,53 @@ impl DrawTarget for UdpDisplay { } } +fn renderWeather(display: &mut UdpDisplay ,data: &Option>){ + let text_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On); + + match data { + Some(v) => match v { + Err(error) => { + Text::new(&error, Point::new(0, 5), text_style) + .draw(display) + .unwrap(); + println!("{}", &error); + } + Ok(result) => { + if(!result.weather.is_empty()){ + let condition = &result.weather[0]; + let short_icon_code = condition.icon.substring(0,2); + + println!("Weather info: {} desc: {} icon {}", condition.main, condition.description, condition.icon); + + match short_icon_code { + "01" => { + let sun_icon = include_bytes!("sun.bmp"); + let sun_icon_image = Bmp::from_slice(sun_icon).unwrap(); + Image::new(&sun_icon_image, Point::new((IMAGE_WIDTH-40) as i32, 0)).draw(display).unwrap(); + + }, + _ => { + println!("Missing icon for {short_icon_code}"); + Text::new(&condition.description, Point::new(0, 0), text_style) + .draw(display) + .unwrap(); + } + } + } + } + }, + None => { + Text::new("Waiting for data", Point::new(0, 0), text_style) + .draw(display) + .unwrap(); + println!("{}", "no result"); + } + } + + + +} + fn send_package(ipaddress: String, data: &Option>) { let mut package: [u8; PACKAGE_LENGTH] = [0; PACKAGE_LENGTH]; @@ -108,43 +162,16 @@ fn send_package(ipaddress: String, data: &Option> image: [0; IMAGE_SIZE_BYTE], }; - let style = PrimitiveStyle::with_stroke(BinaryColor::On, 1); - let text_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On); - Line::new(Point::new(0, 0), Point::new((IMAGE_WIDTH - 1) as i32, 0)) - .into_styled(style) - .draw(&mut display) - .unwrap(); + // Line::new(Point::new(0, 0), Point::new((IMAGE_WIDTH - 1) as i32, 0)) + // .into_styled(PRIMITIVE_STYLE) + // .draw(&mut display) + // .unwrap(); - match data { - Some(v) => match v { - Err(error) => { - Text::new(&error, Point::new(0, 5), text_style) - .draw(&mut display) - .unwrap(); - println!("{}", &error); - } - Ok(result) => { - let mut y = 10; - for condition in result.weather.as_slice() { + renderWeather(&mut display, data); - let text = condition.main.to_owned() + " " + &condition.description + " " + &condition.icon; - Text::new(&text, Point::new(0, y), text_style) - .draw(&mut display) - .unwrap(); - println!("{}", &condition.main); - y += 10; - } - } - }, - None => { - Text::new("Waiting for data", Point::new(20, 30), text_style) - .draw(&mut display) - .unwrap(); - println!("{}", "no result"); - } - } + package[1..PACKAGE_LENGTH].copy_from_slice(&display.image); @@ -185,6 +212,8 @@ fn main() { ); let mut lastData = Option::None; + + loop { let delay = time::Duration::from_millis(10000); diff --git a/client/src/sun.bmp b/client/src/sun.bmp new file mode 100644 index 0000000000000000000000000000000000000000..35acabf18643c772885d417314864528c6699920 GIT binary patch literal 4938 zcmeH_QBDIf3`A3oP<}W9=jcx*B!0LGznq|_>dh9DRD#u)-ZId@kuBuKD zp;YC2{;-5^QdC~#(1e<=pw2`Cw+L+pQe_r7F|E!(qsY=IX?A2>GJ7rm=SSSi-l)K{ z5+&Z{fxRM6N5|42PR}DcICL06v*po@O)oWPS&AT>I*#4a08}zn^?TSVr|Q3}-hGGP zm(pK+AEA^Iv$^B|qUd8J=aDkB5(;Jvnd*|60ztq)ZAL7aO%_W)WWbEwdB}L?)U0UE zPx0!FFAZ~rR(iwjPi0VgOGjjeGw_(mSPnm)T7=(YY8?a_5DW(Rjb|Aj!8x@rs5!xr zXeqK@5QgLcPF$Cy#Wtx}8kU}zKzC=RQDkWp$|;Eym={Z3+MKVTPL0aX9;&M68;7&X Q_x%5*w|dTp|E_+4Z>??Yx&QzG literal 0 HcmV?d00001