riot_coap_handler_demos/
graphics.rsuse coap_message::{Code, MinimalWritableMessage, MutableWritableMessage, ReadableMessage};
use coap_message_utils::Error;
use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::prelude::*;
struct TargetWrapper<T: DrawTarget<BinaryColor>> {
target: T,
}
impl<T: DrawTarget<BinaryColor>> coap_handler::Handler for TargetWrapper<T> {
type RequestData = ();
type ExtractRequestError = Error;
type BuildResponseError<M: MinimalWritableMessage> = M::UnionError;
fn extract_request_data<M: ReadableMessage>(
&mut self,
request: &M,
) -> Result<Self::RequestData, Self::ExtractRequestError> {
let p = request.payload();
if p.len() == 25 {
for (line, linedata) in p.chunks_exact(5).enumerate() {
for (row, c) in linedata.iter().enumerate() {
Pixel(
Point::new(row as _, line as _),
match c {
0x20 => BinaryColor::Off,
_ => BinaryColor::On,
},
)
.draw(&mut self.target)
.map_err(|_| Error::internal_server_error())?;
}
}
Ok(())
} else {
Err(Error::internal_server_error())
}
}
fn estimate_length(&mut self, _request: &Self::RequestData) -> usize {
100 }
fn build_response<M: MutableWritableMessage>(
&mut self,
response: &mut M,
_request: Self::RequestData,
) -> Result<(), M::UnionError> {
response.set_code(Code::new(coap_numbers::code::CHANGED)?);
Ok(())
}
}
pub fn handler_for<T: DrawTarget<BinaryColor>>(target: T) -> impl coap_handler::Handler {
TargetWrapper { target }
}