Skip to content

Commit

Permalink
messing with text stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
PGIII committed Apr 11, 2024
1 parent 80fa5d2 commit ff48847
Show file tree
Hide file tree
Showing 50 changed files with 88 additions and 73 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ thiserror = "1.0.58"
tinybmp = "0.5.0"
rust-embed = { version = "8.3.0", features = ["debug-embed"] }
toml = "0.8.12"
profont = "0.7.0"

[build-dependencies]
embuild = "0.31.3"
Binary file added images/bmp/20/10.bmp
Binary file not shown.
Binary file added images/bmp/20/11.bmp
Binary file not shown.
Binary file added images/bmp/20/12.bmp
Binary file not shown.
Binary file added images/bmp/20/13.bmp
Binary file not shown.
Binary file added images/bmp/20/14.bmp
Binary file not shown.
Binary file added images/bmp/20/15.bmp
Binary file not shown.
Binary file added images/bmp/20/16.bmp
Binary file not shown.
Binary file added images/bmp/20/17.bmp
Binary file not shown.
Binary file added images/bmp/20/18.bmp
Binary file not shown.
Binary file added images/bmp/20/19.bmp
Binary file not shown.
Binary file added images/bmp/20/2.bmp
Binary file not shown.
Binary file added images/bmp/20/20.bmp
Binary file not shown.
Binary file added images/bmp/20/21.bmp
Binary file not shown.
Binary file added images/bmp/20/22.bmp
Binary file not shown.
Binary file added images/bmp/20/23.bmp
Binary file not shown.
Binary file added images/bmp/20/24.bmp
Binary file not shown.
Binary file added images/bmp/20/25.bmp
Binary file not shown.
Binary file added images/bmp/20/26.bmp
Binary file not shown.
Binary file added images/bmp/20/27.bmp
Binary file not shown.
Binary file added images/bmp/20/28.bmp
Binary file not shown.
Binary file added images/bmp/20/29.bmp
Binary file not shown.
Binary file added images/bmp/20/3.bmp
Binary file not shown.
Binary file added images/bmp/20/30.bmp
Binary file not shown.
Binary file added images/bmp/20/31.bmp
Binary file not shown.
Binary file added images/bmp/20/32.bmp
Binary file not shown.
Binary file added images/bmp/20/33.bmp
Binary file not shown.
Binary file added images/bmp/20/34.bmp
Binary file not shown.
Binary file added images/bmp/20/35.bmp
Binary file not shown.
Binary file added images/bmp/20/36.bmp
Binary file not shown.
Binary file added images/bmp/20/37.bmp
Binary file not shown.
Binary file added images/bmp/20/38.bmp
Binary file not shown.
Binary file added images/bmp/20/39.bmp
Binary file not shown.
Binary file added images/bmp/20/4.bmp
Binary file not shown.
Binary file added images/bmp/20/40.bmp
Binary file not shown.
Binary file added images/bmp/20/41.bmp
Binary file not shown.
Binary file added images/bmp/20/42.bmp
Binary file not shown.
Binary file added images/bmp/20/43.bmp
Binary file not shown.
Binary file added images/bmp/20/44.bmp
Binary file not shown.
Binary file added images/bmp/20/45.bmp
Binary file not shown.
Binary file added images/bmp/20/46.bmp
Binary file not shown.
Binary file added images/bmp/20/47.bmp
Binary file not shown.
Binary file added images/bmp/20/5.bmp
Binary file not shown.
Binary file added images/bmp/20/6.bmp
Binary file not shown.
Binary file added images/bmp/20/7.bmp
Binary file not shown.
Binary file added images/bmp/20/8.bmp
Binary file not shown.
Binary file added images/bmp/20/9.bmp
Binary file not shown.
84 changes: 11 additions & 73 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
pub mod openweather;
pub mod pages;

use std::thread::sleep;
use std::time::Duration;

use anyhow::anyhow;
use embedded_graphics::geometry::Point;
use embedded_graphics::image::Image;
use embedded_graphics::mono_font::{ascii::FONT_10X20, MonoTextStyle};
use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::prelude::*;
use embedded_graphics::primitives::{
Circle, Primitive, PrimitiveStyle, PrimitiveStyleBuilder, StrokeAlignment,
};
use embedded_graphics::text::Text;
use embedded_graphics::Drawable;
use esp_idf_svc::hal::delay::FreeRtos;
use esp_idf_svc::hal::gpio::{AnyIOPin, PinDriver};
use esp_idf_svc::hal::modem::WifiModemPeripheral;
use esp_idf_svc::hal::peripherals::Peripherals;
use esp_idf_svc::hal::spi::{SpiDeviceDriver, SpiDriver, SpiDriverConfig};
use esp_idf_svc::sys::EspError;
use esp_idf_svc::{hal::delay::Ets, log::EspLogger};
use log::info;
use openweather::api::get_forecast;
use rust_embed::RustEmbed;
use serde::Deserialize;
use thiserror::Error;

use esp_idf_svc::hal::gpio::{AnyIOPin, PinDriver};
use esp_idf_svc::hal::peripherals::Peripherals;
use ssd1680::driver::DisplayError;
use ssd1680::prelude::*;
use tinybmp::Bmp;
use std::thread::sleep;
use std::time::Duration;

#[derive(RustEmbed)]
#[folder = "images/bmp/ow/40/"]
struct Icons40;
struct IconsOW40;

#[derive(RustEmbed)]
#[folder = "images/bmp/20/"]
struct Icons20;

#[derive(Debug, Deserialize)]
pub struct Config {
Expand Down Expand Up @@ -95,63 +83,13 @@ pub fn main() -> anyhow::Result<()> {
display_bw.set_rotation(DisplayRotation::Rotate90);

loop {
ssd1680.clear_bw_frame().unwrap();
draw_weather_page(&mut display_bw, &config.weather)?;
pages::weather::draw(&mut display_bw, &config.weather)?;
ssd1680.update_bw_frame(display_bw.buffer()).unwrap();
ssd1680.display_frame(&mut FreeRtos).unwrap();
sleep(Duration::from_secs(60 * 10)); //10min
}
}

#[derive(Debug, Error)]
enum WeatherPageError {
#[error("Display Error")]
Display(DisplayError),
#[error("BMP")]
BMP(tinybmp::ParseError),
#[error(transparent)]
Any(#[from] anyhow::Error),
}

fn draw_weather_page<D>(display: &mut D, config: &ConfigWeather) -> Result<(), WeatherPageError>
where
D: DrawTarget<Color = BinaryColor, Error = DisplayError>,
{
let border_stroke = PrimitiveStyleBuilder::new()
.stroke_color(BinaryColor::Off)
.stroke_width(3)
.stroke_alignment(StrokeAlignment::Inside)
.build();
display
.bounding_box()
.into_styled(border_stroke)
.draw(display)
.map_err(|e| WeatherPageError::Display(e))?;

let weather = get_forecast(config)?;
let weather_str = format!(
"Forecast For {}\nH{:.0}F L{:.0}F Now{:.0}F\n{}",
weather.city.name,
weather.list[0].main.temp_max,
weather.list[0].main.temp_min,
weather.list[0].main.temp,
weather.list[0].weather[0].main
);
let style = MonoTextStyle::new(&FONT_10X20, BinaryColor::Off);
let icon_path = format!("{}.bmp", &weather.list[0].weather[0].icon);
let Some(icon) = Icons40::get(&icon_path) else {
return Err(anyhow!("Couldn't find icon: {}", icon_path).into());
};
let bmp = Bmp::<BinaryColor>::from_slice(&icon.data).map_err(|e| WeatherPageError::BMP(e))?;
Text::new(&weather_str, Point::new(52, 20), style)
.draw(display)
.map_err(|e| WeatherPageError::Display(e))?;
Image::new(&bmp, Point::new(10, 2))
.draw(display)
.map_err(|e| WeatherPageError::Display(e))?;
Ok(())
}

fn wifi_create(
modem: impl WifiModemPeripheral + 'static,
config: &ConfigWifi,
Expand Down
1 change: 1 addition & 0 deletions src/pages.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod weather;
75 changes: 75 additions & 0 deletions src/pages/weather.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use anyhow::anyhow;
use embedded_graphics::{
image::Image,
mono_font::MonoTextStyle,
pixelcolor::BinaryColor,
prelude::*,
primitives::{PrimitiveStyleBuilder, StrokeAlignment},
text::{renderer::TextRenderer, Text},
};
use profont::{PROFONT_14_POINT, PROFONT_24_POINT};
use ssd1680::driver::DisplayError;
use thiserror::Error;
use tinybmp::Bmp;

use crate::{openweather::api::get_forecast, ConfigWeather, IconsOW40};

#[derive(Debug, Error)]
pub enum WeatherPageError {
#[error("Display Error")]
Display(DisplayError),
#[error("BMP")]
BMP(tinybmp::ParseError),
#[error(transparent)]
Any(#[from] anyhow::Error),
}

pub fn draw<D>(display: &mut D, config: &ConfigWeather) -> Result<(), WeatherPageError>
where
D: DrawTarget<Color = BinaryColor, Error = DisplayError>,
{
let weather = get_forecast(config)?;
let Size { width, height } = display.bounding_box().size;
let width = width as i32;
let height = height as i32;
let icon_path = format!("{}.bmp", &weather.list[0].weather[0].icon);
let Some(icon) = IconsOW40::get(&icon_path) else {
return Err(anyhow!("Couldn't find icon: {}", icon_path).into());
};
let bmp = Bmp::<BinaryColor>::from_slice(&icon.data).map_err(|e| WeatherPageError::BMP(e))?;
let large_text = MonoTextStyle::new(&PROFONT_24_POINT, BinaryColor::Off);
let small_text = MonoTextStyle::new(&PROFONT_14_POINT, BinaryColor::Off);
let temp_str = format!("{:.0}F", weather.list[0].main.temp);
let weather_str = format!(
"Forecast For {}\nH{:.0}F L{:.0}F Now{:.0}F\n{}",
weather.city.name,
weather.list[0].main.temp_max,
weather.list[0].main.temp_min,
weather.list[0].main.temp,
weather.list[0].weather[0].main
);
// background fill
display
.fill_solid(&display.bounding_box(), BinaryColor::On)
.map_err(|e| WeatherPageError::Display(e))?;
Text::with_baseline(
&weather.city.name,
Point::new(0, 0),
small_text,
embedded_graphics::text::Baseline::Top,
)
.draw(display)
.map_err(|e| WeatherPageError::Display(e))?;
Text::with_baseline(
&temp_str,
Point::new(0, small_text.line_height() as i32),
large_text,
embedded_graphics::text::Baseline::Top,
)
.draw(display)
.map_err(|e| WeatherPageError::Display(e))?;
Image::new(&bmp, Point::new(width - 40, 0))
.draw(display)
.map_err(|e| WeatherPageError::Display(e))?;
Ok(())
}

0 comments on commit ff48847

Please sign in to comment.