forked from alixinne/hyperion.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
240 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[instances.0.instance] | ||
friendlyName = 'LGTV' | ||
enabled = true | ||
|
||
[instances.0.device] | ||
type = 'adalight' | ||
hardwareLedCount = 224 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use async_trait::async_trait; | ||
use tokio::io::AsyncWriteExt; | ||
use tokio_serial::{self, SerialPort, SerialPortBuilderExt, SerialStream}; | ||
use palette::{Pixel, rgb::Rgb}; | ||
|
||
use crate::{color::AnsiDisplayExt, models}; | ||
|
||
use super::{common::*, DeviceError}; | ||
|
||
pub type AdalightDevice = Rewriter<AdalightDeviceImpl>; | ||
|
||
const HEADER_SIZE: usize = 6; | ||
|
||
pub struct AdalightDeviceImpl { | ||
/// Handle to UART character device | ||
dev_handle: SerialStream, | ||
// Data buffer containing whole UART message | ||
adalight_data: Vec<u8>, | ||
} | ||
|
||
#[async_trait] | ||
impl WritingDevice for AdalightDeviceImpl { | ||
type Config = models::Adalight; | ||
|
||
fn new(config: &Self::Config) -> Result<Self, DeviceError> { | ||
let handle = tokio_serial::new( | ||
&config.device_name, | ||
config.baudrate | ||
) | ||
.open_native_async() | ||
.map_err(|_| DeviceError::FailedOpen(config.device_name.clone()))?; | ||
|
||
|
||
// Used for header assembly only | ||
let total_led_count = config.hardware_led_count - 1; | ||
|
||
let buffer_size: usize = HEADER_SIZE + (config.hardware_led_count as usize * 3); | ||
let mut buffer = vec![0x00; buffer_size]; | ||
buffer[0] = 'a' as u8; | ||
buffer[1] = 'd' as u8; | ||
buffer[2] = 'a' as u8; | ||
buffer[3] = ((total_led_count & 0xFF00) >> 8) as u8; | ||
buffer[4] = ((total_led_count & 0xFF)) as u8; | ||
// checksum | ||
buffer[5] = buffer[3] ^ buffer[4] ^ 0x55; | ||
|
||
debug!("Adalight header for {} leds: {}{}{} hi={:#02x} lo={:#02x} chk={:#02x}", | ||
config.hardware_led_count, | ||
buffer[0] as char, buffer[1] as char, buffer[2] as char, | ||
buffer[3], buffer[4], buffer[5] | ||
); | ||
|
||
Ok(Self { | ||
dev_handle: handle, | ||
adalight_data: buffer, | ||
}) | ||
} | ||
|
||
async fn set_let_data( | ||
&mut self, | ||
_config: &Self::Config, | ||
led_data: &[models::Color], | ||
) -> Result<(), DeviceError> { | ||
trace!("Adalight: {} LEDs were set", led_data.len()); | ||
for (index, &led) in led_data.into_iter().enumerate() { | ||
let raw: [u8; 3] = Rgb::into_raw(led); | ||
let start_offset = HEADER_SIZE + (index * 3); | ||
self.adalight_data[start_offset..start_offset + 3].copy_from_slice(&raw); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn write(&mut self) -> Result<(), DeviceError> { | ||
trace!("Adalight: About to write out LED data over serial"); | ||
self.dev_handle.write(&self.adalight_data).await | ||
.map(|_| DeviceError::SerialError("Failed writing LED data".to_string()))?; | ||
|
||
self.dev_handle.flush().await | ||
.map(|_| DeviceError::SerialError("Failed flushing serial".to_string()))?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters