@@ -7,17 +7,20 @@ use std::{
77
88use anyhow:: Result ;
99use futures:: future:: join_all;
10+ use jukebox_util:: { color:: RgbProfile , peripheral:: DeviceType } ;
1011use tokio:: sync:: {
1112 mpsc:: { UnboundedReceiver , UnboundedSender } ,
1213 Mutex ,
1314} ;
1415
1516use crate :: {
16- config:: JukeBoxConfig ,
17+ config:: { ActionConfig , JukeBoxConfig } ,
1718 input:: InputKey ,
1819 serial:: { SerialCommand , SerialEvent } ,
1920} ;
2021
22+ use super :: types:: get_icon_bytes;
23+
2124pub async fn action_task (
2225 mut s_evnt_rx : UnboundedReceiver < SerialEvent > ,
2326 config : Arc < Mutex < JukeBoxConfig > > ,
@@ -44,9 +47,60 @@ pub async fn action_task(
4447 . and_then ( |p| Some ( ( p. key_map , p. rgb_profile ) ) )
4548 . unwrap_or ( ( HashMap :: new ( ) , None ) ) ;
4649
47- ( profile, rgb, c. current_profile . clone ( ) ) // TODO: add hardware input info
50+ let device_type = c
51+ . devices
52+ . get ( device_uid)
53+ . and_then ( |d| Some ( d. 0 ) )
54+ . unwrap ( )
55+ . clone ( ) ;
56+
57+ ( device_type, profile, rgb, c. current_profile . clone ( ) ) // TODO: add hardware input info
4858 } ;
4959
60+ let update_device_configs =
61+ async |scmd_txs : Arc < Mutex < HashMap < String , UnboundedSender < SerialCommand > > > > ,
62+ device_uid : & String ,
63+ device_type : DeviceType ,
64+ keys : HashMap < InputKey , ActionConfig > ,
65+ rgb_profile : RgbProfile | {
66+ let txs = scmd_txs. lock ( ) . await ;
67+ let tx = if let Some ( tx) = txs. get ( device_uid) {
68+ tx
69+ } else {
70+ return ;
71+ } ;
72+
73+ if device_type == DeviceType :: KeyPad {
74+ // send rgb profile
75+ let _ = tx. send ( SerialCommand :: SetRgbMode ( rgb_profile) ) ;
76+
77+ // set icons on screen
78+ let slots = [
79+ InputKey :: KeySwitch1 ,
80+ InputKey :: KeySwitch2 ,
81+ InputKey :: KeySwitch3 ,
82+ InputKey :: KeySwitch4 ,
83+ InputKey :: KeySwitch5 ,
84+ InputKey :: KeySwitch6 ,
85+ InputKey :: KeySwitch7 ,
86+ InputKey :: KeySwitch8 ,
87+ InputKey :: KeySwitch9 ,
88+ InputKey :: KeySwitch10 ,
89+ InputKey :: KeySwitch11 ,
90+ InputKey :: KeySwitch12 ,
91+ ] ;
92+
93+ for ( i, k) in slots. iter ( ) . enumerate ( ) {
94+ if let Some ( a) = keys. get ( k) {
95+ let bytes = get_icon_bytes ( a. action . icon_source ( ) ) ;
96+ let _ = tx. send ( SerialCommand :: SetScrIcon ( i as u8 , bytes) ) ;
97+ }
98+ }
99+ }
100+
101+ // TODO: set hardware inputs here
102+ } ;
103+
50104 while let Some ( evnt) = s_evnt_rx. recv ( ) . await {
51105 match evnt {
52106 SerialEvent :: Connected { device_info } => {
@@ -55,13 +109,16 @@ pub async fn action_task(
55109 clear_set ( & mut prevkeys, device_uid) . await ;
56110
57111 // TODO: set hardware inputs here
58- let ( _, current_rgb_profile, _) = get_profile_info ( & config, device_uid) . await ;
59- let rgb_profile =
60- current_rgb_profile. unwrap_or ( jukebox_util:: color:: RgbProfile :: Off ) ;
61- let txs = scmd_txs. lock ( ) . await ;
62- let _ = txs
63- . get ( device_uid)
64- . and_then ( |t| Some ( t. send ( SerialCommand :: SetRgbMode ( rgb_profile) ) ) ) ;
112+ let ( device_type, keys, rgb_profile, _) =
113+ get_profile_info ( & config, device_uid) . await ;
114+ update_device_configs (
115+ scmd_txs. clone ( ) ,
116+ device_uid,
117+ device_type,
118+ keys,
119+ rgb_profile. unwrap_or ( RgbProfile :: default_gui_profile ( ) ) ,
120+ )
121+ . await ;
65122 }
66123 SerialEvent :: GetInputKeys { device_uid, keys } => {
67124 if !prevkeys. contains_key ( & device_uid) {
@@ -73,7 +130,7 @@ pub async fn action_task(
73130 let scmd_txs = scmd_txs. clone ( ) ;
74131
75132 tokio:: spawn ( async move {
76- let ( current_profile, _, current_profile_name) =
133+ let ( _ , current_profile, _, current_profile_name) =
77134 get_profile_info ( & config, & device_uid) . await ;
78135
79136 let mut prevkeys = prevkeys. lock ( ) . await ;
@@ -101,22 +158,18 @@ pub async fn action_task(
101158
102159 * prevkeys = keys;
103160
104- let ( _ , new_rgb_profile, new_profile_name) =
161+ let ( device_type , new_keys , new_rgb_profile, new_profile_name) =
105162 get_profile_info ( & config, & device_uid) . await ;
106163
107164 if current_profile_name != new_profile_name {
108- let rgb_profile =
109- new_rgb_profile. unwrap_or ( jukebox_util:: color:: RgbProfile :: Off ) ;
110- let txs = scmd_txs. lock ( ) . await ;
111- let tx = if let Some ( tx) = txs. get ( & device_uid) {
112- tx
113- } else {
114- return ;
115- } ;
116-
117- let _ = tx. send ( SerialCommand :: SetRgbMode ( rgb_profile) ) ;
118-
119- // TODO: set hardware inputs here
165+ update_device_configs (
166+ scmd_txs. clone ( ) ,
167+ & device_uid,
168+ device_type,
169+ new_keys,
170+ new_rgb_profile. unwrap_or ( RgbProfile :: default_gui_profile ( ) ) ,
171+ )
172+ . await ;
120173 }
121174 } ) ;
122175 }
0 commit comments