mirror of
https://github.com/cccb/servicepoint.git
synced 2025-01-18 18:10:14 +01:00
split out more files
This commit is contained in:
parent
db8370e642
commit
6b5fdca654
|
@ -1,17 +1,9 @@
|
||||||
use crate::Packet;
|
|
||||||
|
|
||||||
/// A vector of bits
|
/// A vector of bits
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct BitVec {
|
pub struct BitVec {
|
||||||
data: Vec<u8>,
|
data: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<BitVec> for Packet {
|
|
||||||
fn from(value: BitVec) -> Self {
|
|
||||||
value.data
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BitVec {
|
impl BitVec {
|
||||||
pub fn new(size: usize) -> BitVec {
|
pub fn new(size: usize) -> BitVec {
|
||||||
assert_eq!(size % 8, 0);
|
assert_eq!(size % 8, 0);
|
||||||
|
@ -42,3 +34,9 @@ impl BitVec {
|
||||||
return self.data[byte_index] & bit_mask != 0;
|
return self.data[byte_index] & bit_mask != 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Into<Vec<u8>> for BitVec {
|
||||||
|
fn into(self) -> Vec<u8> {
|
||||||
|
self.data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
63
src/command.rs
Normal file
63
src/command.rs
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
use crate::{BitVec, Header, Packet, PixelGrid, ToPacket};
|
||||||
|
|
||||||
|
/// A window
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Window(pub Origin, pub Size);
|
||||||
|
|
||||||
|
/// An origin marks the top left position of the
|
||||||
|
/// data sent to the display.
|
||||||
|
/// A window
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Origin(pub u16, pub u16);
|
||||||
|
|
||||||
|
/// Size defines the width and height of a window
|
||||||
|
/// A window
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Size(pub u16, pub u16);
|
||||||
|
|
||||||
|
type Offset = u16;
|
||||||
|
|
||||||
|
type Brightness = u8;
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Command {
|
||||||
|
Clear,
|
||||||
|
HardReset,
|
||||||
|
FadeOut,
|
||||||
|
CharBrightness(Window, Vec<Brightness>),
|
||||||
|
Brightness(Brightness),
|
||||||
|
BitmapLinear(Offset, BitVec),
|
||||||
|
BitmapLinearAnd(Offset, BitVec),
|
||||||
|
BitmapLinearOr(Offset, BitVec),
|
||||||
|
BitmapLinearXor(Offset, BitVec),
|
||||||
|
Cp437Data(Window, Vec<u8>),
|
||||||
|
BitmapLinearWin(Window, PixelGrid),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn offset_and_payload(command: u16, offset: Offset, payload: Vec<u8>) -> Packet {
|
||||||
|
Packet(Header(command, offset, payload.len() as u16, 0, 0), payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn window_and_payload(command: u16, window: Window, payload: Vec<u8>) -> Packet {
|
||||||
|
let Window(Origin(x, y), Size(w, h)) = window;
|
||||||
|
Packet(Header(command, x, y, w, h), payload.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToPacket for Command {
|
||||||
|
fn to_packet(self) -> Packet {
|
||||||
|
match self {
|
||||||
|
Command::Clear => Packet(Header(0x0002, 0x0000, 0x0000, 0x0000, 0x0000), vec!()),
|
||||||
|
Command::CharBrightness(window, payload) => window_and_payload(0x0005, window, payload),
|
||||||
|
Command::Brightness(brightness) => Packet(Header(0x0007, 0x00000, 0x0000, 0x0000, 0x0000), vec!(brightness)),
|
||||||
|
Command::HardReset => Packet(Header(0x000b, 0x0000, 0x0000, 0x0000, 0x0000), vec!()),
|
||||||
|
Command::FadeOut => Packet(Header(0x000d, 0x0000, 0x0000, 0x0000, 0x0000), vec!()),
|
||||||
|
Command::BitmapLinear(offset, bits) => offset_and_payload(0x0012, offset, bits.into()),
|
||||||
|
Command::BitmapLinearWin(window, pixels) => window_and_payload(0x0013, window, pixels.into()),
|
||||||
|
Command::BitmapLinearAnd(offset, bits) => offset_and_payload(0x0014, offset, bits.into()),
|
||||||
|
Command::BitmapLinearOr(offset, bits) => offset_and_payload(0x0015, offset, bits.into()),
|
||||||
|
Command::BitmapLinearXor(offset, bits) => offset_and_payload(0x0016, offset, bits.into()),
|
||||||
|
Command::Cp437Data(window, payload) => window_and_payload(0x0003, window, payload),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,10 +1,14 @@
|
||||||
use std::net::{ToSocketAddrs, UdpSocket};
|
use std::net::{ToSocketAddrs, UdpSocket};
|
||||||
use crate::{Command, Packet};
|
use crate::{Packet};
|
||||||
|
|
||||||
pub struct Connection {
|
pub struct Connection {
|
||||||
socket: UdpSocket,
|
socket: UdpSocket,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait ToPacket {
|
||||||
|
fn to_packet(self) -> Packet;
|
||||||
|
}
|
||||||
|
|
||||||
impl Connection {
|
impl Connection {
|
||||||
/// Open a new UDP socket and create a display instance
|
/// Open a new UDP socket and create a display instance
|
||||||
pub fn open(addr: impl ToSocketAddrs) -> std::io::Result<Self> {
|
pub fn open(addr: impl ToSocketAddrs) -> std::io::Result<Self> {
|
||||||
|
@ -14,9 +18,9 @@ impl Connection {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send a command to the display
|
/// Send a command to the display
|
||||||
pub fn send(&self, command: Command) -> std::io::Result<()> {
|
pub fn send(&self, packet: impl ToPacket) -> std::io::Result<()> {
|
||||||
let packet: Packet = command.into();
|
let packet = packet.to_packet();
|
||||||
self.socket.send(&packet)?;
|
self.socket.send(&*packet.to_bytes())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
94
src/lib.rs
94
src/lib.rs
|
@ -1,98 +1,18 @@
|
||||||
mod connection;
|
mod connection;
|
||||||
mod pixel_grid;
|
mod pixel_grid;
|
||||||
mod bit_vec;
|
mod bit_vec;
|
||||||
|
mod packet;
|
||||||
|
mod command;
|
||||||
|
|
||||||
pub use crate::connection::*;
|
pub use crate::connection::{Connection, ToPacket};
|
||||||
pub use crate::pixel_grid::*;
|
pub use crate::pixel_grid::{PixelGrid};
|
||||||
pub use crate::bit_vec::*;
|
pub use crate::bit_vec::{BitVec};
|
||||||
|
pub use crate::packet::{Packet, Header, Payload};
|
||||||
|
pub use crate::command::{Command, Size, Origin, Window};
|
||||||
|
|
||||||
pub const TILE_SIZE: u16 = 8;
|
pub const TILE_SIZE: u16 = 8;
|
||||||
pub const TILE_WIDTH: u16 = 56;
|
pub const TILE_WIDTH: u16 = 56;
|
||||||
pub const TILE_HEIGHT: u16 = 20;
|
pub const TILE_HEIGHT: u16 = 20;
|
||||||
pub const PIXEL_WIDTH: u16 = TILE_WIDTH * TILE_SIZE;
|
pub const PIXEL_WIDTH: u16 = TILE_WIDTH * TILE_SIZE;
|
||||||
pub const PIXEL_HEIGHT: u16 = TILE_HEIGHT * TILE_SIZE;
|
pub const PIXEL_HEIGHT: u16 = TILE_HEIGHT * TILE_SIZE;
|
||||||
|
|
||||||
pub const PIXEL_COUNT: usize = PIXEL_WIDTH as usize * PIXEL_HEIGHT as usize;
|
pub const PIXEL_COUNT: usize = PIXEL_WIDTH as usize * PIXEL_HEIGHT as usize;
|
||||||
|
|
||||||
/// A window
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Window(pub Origin, pub Size);
|
|
||||||
|
|
||||||
/// An origin marks the top left position of the
|
|
||||||
/// data sent to the display.
|
|
||||||
/// A window
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Origin(pub u16, pub u16);
|
|
||||||
|
|
||||||
/// Size defines the width and height of a window
|
|
||||||
/// A window
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Size(pub u16, pub u16);
|
|
||||||
|
|
||||||
type Offset = u16;
|
|
||||||
|
|
||||||
type Brightness = u8;
|
|
||||||
|
|
||||||
type Packet = Vec<u8>;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum Command {
|
|
||||||
Clear,
|
|
||||||
HardReset,
|
|
||||||
FadeOut,
|
|
||||||
CharBrightness(Window, Vec<Brightness>),
|
|
||||||
Brightness(Brightness),
|
|
||||||
BitmapLinear(Offset, BitVec),
|
|
||||||
BitmapLinearAnd(Offset, BitVec),
|
|
||||||
BitmapLinearOr(Offset, BitVec),
|
|
||||||
BitmapLinearXor(Offset, BitVec),
|
|
||||||
Cp437Data(Window, Vec<u8>),
|
|
||||||
BitmapLinearWin(Window, PixelGrid),
|
|
||||||
}
|
|
||||||
|
|
||||||
fn offset_and_payload(command: u16, offset: Offset, payload: Vec<u8>) -> Packet {
|
|
||||||
let mut packet = vec!(0u8; 10 + payload.len());
|
|
||||||
|
|
||||||
packet[0..=1].copy_from_slice(&u16::to_be_bytes(command));
|
|
||||||
packet[2..=3].copy_from_slice(&u16::to_be_bytes(offset));
|
|
||||||
packet[4..=5].copy_from_slice(&u16::to_be_bytes(payload.len() as u16));
|
|
||||||
packet[6..=7].copy_from_slice(&[0x00, 0x00]); // subcommand 0 => no compression
|
|
||||||
packet[8..=9].copy_from_slice(&[0x00, 0x00]); // reserved
|
|
||||||
|
|
||||||
packet[10..].copy_from_slice(&*payload);
|
|
||||||
|
|
||||||
packet
|
|
||||||
}
|
|
||||||
|
|
||||||
fn window_and_payload(command: u16, window: Window, payload: Vec<u8>) -> Packet {
|
|
||||||
let Window(Origin(x, y), Size(w, h)) = window;
|
|
||||||
|
|
||||||
let mut packet = vec!(0u8; 10 + payload.len());
|
|
||||||
packet[0..=1].copy_from_slice(&u16::to_be_bytes(command));
|
|
||||||
packet[2..=3].copy_from_slice(&u16::to_be_bytes(x));
|
|
||||||
packet[4..=5].copy_from_slice(&u16::to_be_bytes(y));
|
|
||||||
packet[6..=7].copy_from_slice(&u16::to_be_bytes(w));
|
|
||||||
packet[8..=9].copy_from_slice(&u16::to_be_bytes(h));
|
|
||||||
|
|
||||||
packet[10..].copy_from_slice(&*payload);
|
|
||||||
|
|
||||||
packet
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Command> for Packet {
|
|
||||||
fn from(value: Command) -> Self {
|
|
||||||
match value {
|
|
||||||
Command::Clear => vec!(0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
||||||
Command::CharBrightness(window, payload) => window_and_payload(0x0005, window, payload),
|
|
||||||
Command::Brightness(brightness) => vec!(0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, brightness),
|
|
||||||
Command::HardReset => vec!(0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
||||||
Command::FadeOut => vec!(0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
|
|
||||||
Command::BitmapLinear(offset, payload) => offset_and_payload(0x0012, offset, payload.into()),
|
|
||||||
Command::BitmapLinearWin(window, payload) => window_and_payload(0x0013, window, payload.into()),
|
|
||||||
Command::BitmapLinearAnd(offset, payload) => offset_and_payload(0x0014, offset, payload.into()),
|
|
||||||
Command::BitmapLinearOr(offset, payload) => offset_and_payload(0x0015, offset, payload.into()),
|
|
||||||
Command::BitmapLinearXor(offset, payload) => offset_and_payload(0x0016, offset, payload.into()),
|
|
||||||
Command::Cp437Data(window, payload) => window_and_payload(0x0003, window, payload),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -13,8 +13,8 @@ fn main() {
|
||||||
for x_offset in 0..usize::MAX {
|
for x_offset in 0..usize::MAX {
|
||||||
let mut pixels = PixelGrid::new(PIXEL_WIDTH as usize, PIXEL_HEIGHT as usize);
|
let mut pixels = PixelGrid::new(PIXEL_WIDTH as usize, PIXEL_HEIGHT as usize);
|
||||||
for y in 0..PIXEL_HEIGHT as usize {
|
for y in 0..PIXEL_HEIGHT as usize {
|
||||||
for x_add in 0..=y%8 {
|
for x_add in 0..=y % 8 {
|
||||||
pixels.set((y + x_offset +x_add) % PIXEL_WIDTH as usize, y, true);
|
pixels.set((y + x_offset + x_add) % PIXEL_WIDTH as usize, y, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
22
src/packet.rs
Normal file
22
src/packet.rs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
pub struct Header(pub u16, pub u16, pub u16, pub u16, pub u16);
|
||||||
|
|
||||||
|
pub type Payload = Vec<u8>;
|
||||||
|
|
||||||
|
pub struct Packet(pub Header, pub Payload);
|
||||||
|
|
||||||
|
impl Packet {
|
||||||
|
pub fn to_bytes(&self) -> Vec<u8> {
|
||||||
|
let Packet(Header(mode, a, b, c, d), payload) = self;
|
||||||
|
|
||||||
|
let mut packet = vec!(0u8; 10 + payload.len());
|
||||||
|
packet[0..=1].copy_from_slice(&u16::to_be_bytes(*mode));
|
||||||
|
packet[2..=3].copy_from_slice(&u16::to_be_bytes(*a));
|
||||||
|
packet[4..=5].copy_from_slice(&u16::to_be_bytes(*b));
|
||||||
|
packet[6..=7].copy_from_slice(&u16::to_be_bytes(*c));
|
||||||
|
packet[8..=9].copy_from_slice(&u16::to_be_bytes(*d));
|
||||||
|
|
||||||
|
packet[10..].copy_from_slice(&*payload);
|
||||||
|
|
||||||
|
return packet;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::{BitVec, Packet};
|
use crate::{BitVec};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct PixelGrid {
|
pub struct PixelGrid {
|
||||||
|
@ -27,8 +27,8 @@ impl PixelGrid {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<PixelGrid> for Packet {
|
impl Into<Vec<u8>> for PixelGrid {
|
||||||
fn from(value: PixelGrid) -> Self {
|
fn into(self) -> Vec<u8> {
|
||||||
value.bit_vec.into()
|
self.bit_vec.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue