Browse Source

First commit

master
Ilya 4 years ago
commit
69feadbccc
  1. 2
      .gitignore
  2. 2033
      Cargo.lock
  3. 22
      Cargo.toml
  4. 74
      src/display/epd.rs
  5. 11
      src/display/mod.rs
  6. 33
      src/display/simulation.rs
  7. 63
      src/main.rs
  8. 69
      src/user.rs

2
.gitignore vendored

@ -0,0 +1,2 @@
/target
users.json

2033
Cargo.lock generated

File diff suppressed because it is too large Load Diff

22
Cargo.toml

@ -0,0 +1,22 @@
[package]
name = "eink_calendar"
version = "0.1.0"
edition = "2021"
[features]
epd = []
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
embedded-graphics = "0.7.1"
linux-embedded-hal = "0.3"
embedded-hal = "0.2.4"
embedded-components = { git = "https://gitea.anshorei.me/Anshorei/embedded-components.git" }
epd-waveshare = { git = "https://github.com/Anshorei/epd-waveshare", rev = "3011b5c" }
chrono = "*"
kitchen-fridge = { git = "https://gitea.anshorei.me/Anshorei/kitchen-fridge.git", branch = "feature/events" }
tokio = "*"
serde = "*"
serde_json = "*"
embedded-graphics-simulator = "*"

74
src/display/epd.rs

@ -0,0 +1,74 @@
use epd_waveshare::{
color::OctColor as Color,
epd5in65f::{Display5in65f as EpdDisplay, Epd5in65f as Epd},
prelude::*,
};
use embedded_hal::prelude::*;
use linux_embedded_hal::{
spidev::{self, SpidevOptions},
sysfs_gpio::Direction,
Delay, Pin, Spidev,
};
pub struct Display {
pub target: EpdDisplay,
epd: Epd<Spidev, Pin, Pin, Pin, Pin, Delay>,
spi: Spidev,
delay: Delay,
}
impl Display {
pub fn new() -> Self {
let mut spi = Spidev::open("/dev/spidev0.0").expect("spidev directory");
let options = SpidevOptions::new()
.bits_per_word(8)
.max_speed_hz(4_000_000)
.mode(spidev::SpiModeFlags::SPI_MODE_0)
.build();
spi.configure(&options).expect("spi configuration");
let cs = Pin::new(26);
cs.export().expect("cs export");
while !cs.is_exported() {}
cs.set_direction(Direction::Out).expect("CS direction");
cs.set_value(1).expect("CS value set to 1");
let busy = Pin::new(24);
busy.export().expect("busy export");
while !busy.is_exported() {}
busy.set_direction(Direction::In).expect("busy Direction");
let dc = Pin::new(25);
dc.export().expect("dc export");
while !dc.is_exported() {}
dc.set_direction(Direction::Out).expect("dc Direction");
dc.set_value(1).expect("dc Value set to 1");
let rst = Pin::new(17);
rst.export().expect("rst export");
while !rst.is_exported() {}
rst.set_direction(Direction::Out).expect("rst Direction");
rst.set_value(1).expect("rst Value set to 1");
let mut delay = Delay {};
let mut epd =
Epd::new(&mut spi, cs, busy, dc, rst, &mut delay).expect("eink initialize error");
let mut display = EpdDisplay::default();
return Display {
target: display,
epd,
spi,
delay,
};
}
pub fn update(&mut self) {
self.epd
.update_and_display_frame(&mut self.spi, self.target.buffer(), &mut self.delay)
.expect("updating");
}
}

11
src/display/mod.rs

@ -0,0 +1,11 @@
#[cfg(feature = "epd")]
mod epd;
#[cfg(feature = "epd")]
pub use epd::Display;
#[cfg(not(feature = "epd"))]
mod simulation;
#[cfg(not(feature = "epd"))]
pub use simulation::Display;

33
src/display/simulation.rs

@ -0,0 +1,33 @@
use embedded_graphics::{
pixelcolor::{BinaryColor, Rgb888},
prelude::*,
};
use embedded_graphics_simulator::{
BinaryColorTheme, OutputSettings, OutputSettingsBuilder, SimulatorDisplay, Window,
};
pub struct Display<C> {
pub target: SimulatorDisplay<C>,
output_settings: OutputSettings,
}
impl<C> Display<C>
where
C: PixelColor + From<BinaryColor> + Into<Rgb888> + From<Rgb888>,
{
pub fn new() -> Self {
let display = SimulatorDisplay::<C>::new(Size::new(600, 448));
let output_settings = OutputSettingsBuilder::new()
.theme(BinaryColorTheme::Default)
.build();
Self {
target: display,
output_settings,
}
}
pub fn update(&mut self) {
Window::new("Simulator", &self.output_settings).show_static(&self.target);
}
}

63
src/main.rs

@ -0,0 +1,63 @@
mod display;
mod user;
use display::Display;
use embedded_components::{
Component, ComponentStyle, List, ListItemData, ScrollingCalendar, VSplit,
};
use embedded_graphics::prelude::*;
use epd_waveshare::color::OctColor as Color;
use user::{get_user_data, User};
#[tokio::main]
async fn main() {
let contents = std::fs::read_to_string("users.json").expect("No user configuration found");
let users: Vec<User> = serde_json::from_str(&contents).expect("User configuration invalid");
let mut incomplete_tasks = vec![];
let mut completed_tasks = vec![];
for user in users.iter() {
let mut user_data = get_user_data(user).await;
for task in user_data.tasks.into_iter() {
if task.progress >= 100f32 {
completed_tasks.push(task);
} else {
incomplete_tasks.push(task);
}
}
}
incomplete_tasks.sort_by(|a, b| b.progress.partial_cmp(&a.progress).unwrap());
let mut tasks = vec![];
completed_tasks.truncate(3);
tasks.append(&mut completed_tasks);
tasks.append(&mut incomplete_tasks);
let mut display = Display::new();
let style = ComponentStyle {
fg_color: Color::Black,
bg_color: Color::White,
hi_color: Color::Red,
border_color: Color::Black,
border: 1,
bezel: 1,
};
let list_style = ComponentStyle {
hi_color: Color::Green,
..style.clone()
};
let screen = VSplit::new(
ScrollingCalendar::new(style, 2, 5),
List::new(list_style, tasks, 10),
)
.with_ratio(0.55);
screen.draw(&mut display.target).expect("Could not draw!");
display.update();
}

69
src/user.rs

@ -0,0 +1,69 @@
use embedded_components::ListItemData;
use kitchen_fridge::{
cache::Cache,
client::Client,
traits::{CalDavSource, CompleteCalendar},
CalDavProvider,
};
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
pub struct User {
pub username: String,
pub url: String,
pub password: String,
pub calendar_urls: Vec<String>,
}
pub struct UserData {
pub tasks: Vec<ListItemData>,
}
pub async fn get_user_data(user: &User) -> UserData {
let cache_path = format!("test_cache/{}", user.username);
let cache_path = std::path::Path::new(&cache_path);
let client = Client::new(&user.url, &user.username, &user.password).unwrap();
let cache = Cache::from_folder(&cache_path).unwrap_or(Cache::new(&cache_path));
let mut provider = CalDavProvider::new(client, cache);
provider.sync().await;
provider.local().save_to_folder().unwrap();
let mut tasks = vec![];
let cals = provider.local().get_calendars().await.unwrap();
for (url, cal) in cals {
if !user
.calendar_urls
.iter()
.any(|calendar_url| url == calendar_url.parse().unwrap())
{
continue;
}
let cal = cal.lock().unwrap();
let items = cal.get_items().await.unwrap();
for (_, item) in items {
match item {
kitchen_fridge::Item::Event(_) => continue,
kitchen_fridge::Item::Task(task) => {
tasks.push(
ListItemData::new(
&user
.username
.chars()
.nth(0)
.unwrap_or(' ')
.to_uppercase()
.to_string(),
&task.name().to_string(),
)
.with_progress(task.completion_percent()),
);
}
}
}
}
println!("Found {} tasks for {}", tasks.len(), user.username);
return UserData { tasks };
}
Loading…
Cancel
Save