Add code! :3

This commit is contained in:
Leonie 2023-11-11 15:22:24 +01:00
commit 415428d790
5 changed files with 3621 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

3421
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

12
Cargo.toml Normal file
View file

@ -0,0 +1,12 @@
[package]
name = "kontroller"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
iced="0.10.0"
reqwest="0.11.22"
tokio = { version = "1", features = ["full"] }
serde = "1.0.192"

9
src/lib.rs Normal file
View file

@ -0,0 +1,9 @@
use tokio;
use reqwest::Client;
use serde::ser::Serialize;
#[tokio::main]
pub async fn post<T: Serialize>(addr: &String, form_a: &str, form_b: T) {
let c = Client::new();
let p = [(form_a,form_b)];
c.post(addr).form(&p).send().await;
}

178
src/main.rs Normal file
View file

@ -0,0 +1,178 @@
use iced::executor;
use iced::widget::Column;
use iced::widget::container;
use iced::widget::{button, text_input,Slider};
use iced::Renderer;
use iced::{
Alignment, Application, Command, Element, Length, Settings, Theme,
};
use reqwest::Client;
use std::sync::mpsc::Sender;
use std::thread;
use std::sync::mpsc;
use kontroller::post;
pub fn main() -> iced::Result {
State::run(Settings {
exit_on_close_request: true,
..Settings::default()
})
}
struct State {
addr: String,
speed: f64,
tx: Sender<Task>
}
impl Default for State {
fn default() -> Self {
let (tx, rx) = mpsc::channel();
thread::spawn (move|| {
let mut addr = String::new();
let mut speed: f64;
for tasks in rx {
match tasks {
Task::FW => {
if addr.len() != 0{
post(&addr,"state","1");
println!("Recieved FW ");
}
}
Task::BW => {
if addr.len() != 0 {
post(&addr,"state","2");
println!("Recieved BW");
}
}
Task::OFF => {
if addr.len() != 0 {
post(&addr,"state","3");
println!("Recieved OFF")
}
}
Task::Addr(s) => {
addr=s;
println!("Changed addr to {}", &addr);
}
Task::Speed(i) => {
speed=i;
if addr.len() != 0 {
post(&addr, "speed", speed);
println!("Changed speed to {}", &speed.round());
}
}
}
}
}
);
State {
addr: String::default(),
speed: f64::default(),
tx: tx,
}
}
}
#[derive(Debug,Clone)]
enum Message {
Addr(String),
FW,
BW,
Stop,
Speed(f64)
}
enum Task {
FW,
BW,
OFF,
Speed(f64),
Addr(String)
}
impl Application for State {
type Message = Message;
type Theme = Theme;
type Executor = executor::Default;
type Flags = ();
fn new(_flags: ()) -> (State, Command<Message>) {
(State::default(), Command::none())
}
fn title(&self) -> String {
String::from("Click counter")
}
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
match message {
Message::FW => {
self.tx.send(Task::FW).unwrap();
Command::none()
}
Message::BW => {
self.tx.send(Task::BW).unwrap();
Command::none()
}
Message::Stop => {
self.tx.send(Task::OFF).unwrap();
Command::none()
}
Message::Addr(addr)=>{
self.tx.send(Task::Addr(addr.clone())).unwrap();
self.addr=addr;
Command::none()
}
Message::Speed(speed)=> {
self.tx.send(Task::Speed(self.speed.clone())).unwrap();
self.speed=speed;
Command::none()
}
}
}
fn view(&self) -> Element<Message> {
let input = text_input("ip with /motor attached",&self.addr)
.on_input(Message::Addr)
.padding(30)
.width(200);
let fw_button: iced::widget::Button<'_, Message, Renderer> = button("Forwords").on_press(Message::FW).padding(40);
let bw_button: iced::widget::Button<'_, Message, Renderer> = button("Backwords").on_press(Message::BW).padding(40);
let stop_button: iced::widget::Button<'_, Message, Renderer> = button("OFF").on_press(Message::Stop).padding(40);
let slider = Slider::new(0.0..=255.0, self.speed, Message::Speed).width(200);
let content= Column::new()
.align_items(Alignment::Center)
.spacing(20)
.push(input)
.push(fw_button)
.push(bw_button)
.push(stop_button)
.push(slider);
container(content)
.width(Length::Fill)
.height(Length::Fill)
.center_x()
.center_y()
.into()
}
}