@@ -8,16 +8,34 @@ namespace GHelper.USB
88 public static class XGM
99 {
1010 const int ASUS_ID = 0x0b05 ;
11+ static readonly int [ ] deviceIds = { 0x1970 , 0x1a9a , 0x1C29 } ;
1112
12- static int [ ] deviceIds = { 0x1970 , 0x1a9a } ;
13+ public static HidDevice ? GetDevice ( )
14+ {
15+ try
16+ {
17+ return DeviceList . Local . GetHidDevices ( ASUS_ID ) . FirstOrDefault ( device =>
18+ deviceIds . Contains ( device . ProductID ) &&
19+ device . CanOpen &&
20+ device . GetMaxFeatureReportLength ( ) >= 300 ) ;
21+ }
22+ catch ( Exception ex )
23+ {
24+ Logger . WriteLine ( $ "Error getting XGM device: { ex } ") ;
25+ return null ;
26+ }
27+ }
28+
29+ public static bool IsConnected ( )
30+ {
31+ return GetDevice ( ) is not null ;
32+ }
1333
1434 public static void Write ( byte [ ] data )
1535 {
16- HidDeviceLoader loader = new HidDeviceLoader ( ) ;
1736 try
1837 {
19- HidDevice device = loader . GetDevices ( ASUS_ID ) . Where ( device => deviceIds . Contains ( device . ProductID ) && device . CanOpen && device . GetMaxFeatureReportLength ( ) >= 300 ) . FirstOrDefault ( ) ;
20-
38+ HidDevice ? device = GetDevice ( ) ;
2139 if ( device is null )
2240 {
2341 Logger . WriteLine ( "XGM SUB device not found" ) ;
@@ -26,13 +44,10 @@ public static void Write(byte[] data)
2644
2745 using ( HidStream hidStream = device . Open ( ) )
2846 {
29- var payload = new byte [ 300 ] ;
30- Array . Copy ( data , payload , data . Length ) ;
31-
47+ byte [ ] payload = new byte [ 300 ] ;
48+ data . CopyTo ( payload , 0 ) ;
3249 hidStream . SetFeature ( payload ) ;
33- Logger . WriteLine ( "XGM-" + device . ProductID + "|" + device . GetMaxFeatureReportLength ( ) + ":" + BitConverter . ToString ( data ) ) ;
34-
35- hidStream . Close ( ) ;
50+ Logger . WriteLine ( $ "XGM-{ device . ProductID } |{ device . GetMaxFeatureReportLength ( ) } :{ BitConverter . ToString ( data ) } ") ;
3651 }
3752 }
3853 catch ( Exception ex )
@@ -44,33 +59,50 @@ public static void Write(byte[] data)
4459
4560 public static void Init ( )
4661 {
47- Write ( Encoding . ASCII . GetBytes ( "^ASUS Tech.Inc." ) ) ;
62+ Task . Run ( ( ) =>
63+ {
64+ if ( IsConnected ( ) )
65+ {
66+ Write ( Encoding . ASCII . GetBytes ( "^ASUS Tech.Inc." ) ) ;
67+ Light ( AppConfig . Is ( "xmg_light" ) ) ;
68+ }
69+ } ) ;
4870 }
4971
5072 public static void Light ( bool status )
5173 {
52- Write ( new byte [ ] { 0x5e , 0xc5 , status ? ( byte ) 0x50 : ( byte ) 0 } ) ;
74+ Write ( [ 0x5e , 0xc5 , status ? ( byte ) 0x50 : ( byte ) 0 ] ) ;
5375 }
5476
5577 public static void InitLight ( )
5678 {
57- if ( Program . acpi . IsXGConnected ( ) ) Light ( AppConfig . Is ( "xmg_light" ) ) ;
79+ Task . Run ( ( ) =>
80+ {
81+ if ( IsConnected ( ) ) Light ( AppConfig . Is ( "xmg_light" ) ) ;
82+ } ) ;
5883 }
5984
6085 public static void Reset ( )
6186 {
62- Write ( new byte [ ] { 0x5e , 0xd1 , 0x02 } ) ;
87+ Task . Run ( ( ) =>
88+ {
89+ if ( IsConnected ( ) ) Write ( [ 0x5e , 0xd1 , 0x02 ] ) ;
90+ } ) ;
6391 }
6492
6593 public static void SetFan ( byte [ ] curve )
6694 {
67- if ( AsusACPI . IsInvalidCurve ( curve ) ) return ;
68-
69- byte [ ] msg = new byte [ 19 ] ;
70- Array . Copy ( new byte [ ] { 0x5e , 0xd1 , 0x01 } , msg , 3 ) ;
71- Array . Copy ( curve , 0 , msg , 3 , curve . Length ) ;
72-
73- Write ( msg ) ;
95+ Task . Run ( ( ) =>
96+ {
97+ if ( IsConnected ( ) )
98+ {
99+ if ( AsusACPI . IsInvalidCurve ( curve ) ) return ;
100+ byte [ ] msg = new byte [ 19 ] ;
101+ Array . Copy ( new byte [ ] { 0x5e , 0xd1 , 0x01 } , msg , 3 ) ;
102+ Array . Copy ( curve , 0 , msg , 3 , curve . Length ) ;
103+ Write ( msg ) ;
104+ }
105+ } ) ;
74106 }
75107 }
76108}
0 commit comments