Reads states and returns updates
(&self, timeout: Option<Duration>)
| 673 | impl DeviceStateReader { |
| 674 | /// Reads states and returns updates |
| 675 | pub fn read(&self, timeout: Option<Duration>) -> Result<Vec<DeviceStateUpdate>, StreamDeckError> { |
| 676 | let input = self.device.read_input(timeout)?; |
| 677 | let mut my_states = self.states.lock()?; |
| 678 | |
| 679 | let mut updates = vec![]; |
| 680 | |
| 681 | match input { |
| 682 | StreamDeckInput::ButtonStateChange(buttons) => { |
| 683 | for (index, (their, mine)) in zip(buttons.iter(), my_states.buttons.iter()).enumerate() { |
| 684 | if their != mine { |
| 685 | let key_count = self.device.kind.key_count(); |
| 686 | if index < key_count as usize { |
| 687 | if *their { |
| 688 | updates.push(DeviceStateUpdate::ButtonDown(index as u8)); |
| 689 | } else { |
| 690 | updates.push(DeviceStateUpdate::ButtonUp(index as u8)); |
| 691 | } |
| 692 | } else if *their { |
| 693 | updates.push(DeviceStateUpdate::TouchPointDown(index as u8 - key_count)); |
| 694 | } else { |
| 695 | updates.push(DeviceStateUpdate::TouchPointUp(index as u8 - key_count)); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | my_states.buttons = buttons; |
| 701 | } |
| 702 | |
| 703 | StreamDeckInput::EncoderStateChange(encoders) => { |
| 704 | for (index, (their, mine)) in zip(encoders.iter(), my_states.encoders.iter()).enumerate() { |
| 705 | if *their != *mine { |
| 706 | if *their { |
| 707 | updates.push(DeviceStateUpdate::EncoderDown(index as u8)); |
| 708 | } else { |
| 709 | updates.push(DeviceStateUpdate::EncoderUp(index as u8)); |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | my_states.encoders = encoders; |
| 715 | } |
| 716 | |
| 717 | StreamDeckInput::EncoderTwist(twist) => { |
| 718 | for (index, change) in twist.iter().enumerate() { |
| 719 | if *change != 0 { |
| 720 | updates.push(DeviceStateUpdate::EncoderTwist(index as u8, *change)); |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | StreamDeckInput::TouchScreenPress(x, y) => { |
| 726 | updates.push(DeviceStateUpdate::TouchScreenPress(x, y)); |
| 727 | } |
| 728 | |
| 729 | StreamDeckInput::TouchScreenLongPress(x, y) => { |
| 730 | updates.push(DeviceStateUpdate::TouchScreenLongPress(x, y)); |
| 731 | } |
| 732 |