@@ -93,17 +93,24 @@ public enum Button : int {
9393 private Int16 [ ] gyr_neutral = { 0 , 0 , 0 } ;
9494 private Int16 [ ] gyr_sensiti = { 0 , 0 , 0 } ;
9595 private Vector3 gyr_g ;
96+
97+ private Int16 [ ] pro_hor_offset = { - 710 , 0 , 0 } ;
98+ private Int16 [ ] left_hor_offset = { 0 , 0 , 0 } ;
99+ private Int16 [ ] right_hor_offset = { 0 , 0 , 0 } ;
100+
96101 private bool do_localize ;
97102 private float filterweight ;
98103 private const uint report_len = 49 ;
99104 private struct Report {
100105 byte [ ] r ;
101106 System . DateTime t ;
102107 public ulong ts ;
108+
109+ // To-do: get timestamp from report (0th byte); send to server for every 5ms
103110 public Report ( byte [ ] report , System . DateTime time , ulong timestamp ) {
104111 r = report ;
105112 t = time ;
106- ts = timestamp ;
113+ ts = ( ulong ) ( ( timestamp / ( double ) Stopwatch . Frequency ) * 1000000 ) ; // meant to be in microseconds
107114 }
108115 public System . DateTime GetTime ( ) {
109116 return t ;
@@ -264,7 +271,8 @@ public int Attach(byte leds_ = 0x0) {
264271 state = state_ . ATTACHED ;
265272 byte [ ] a = { 0x0 } ;
266273 // Input report mode
267- Subcommand ( 0x3 , new byte [ ] { 0x3f } , 1 , false ) ;
274+ Subcommand ( 0x3 , new byte [ ] { 0x30 } , 1 , false ) ;
275+ Subcommand ( 0x3 , new byte [ ] { 0x03 , 0x00 , 0x00 , 0x01 } , 4 , false ) ; // higher gyro performance rate
268276 a [ 0 ] = 0x1 ;
269277 dump_calibration_data ( ) ;
270278 // Connect
@@ -290,10 +298,10 @@ public void Detach() {
290298 PrintArray ( max , format : "Max {0:S}" , d : DebugType . IMU ) ;
291299 PrintArray ( sum , format : "Sum {0:S}" , d : DebugType . IMU ) ;
292300 if ( state > state_ . NO_JOYCONS ) {
293- Subcommand ( 0x30 , new byte [ ] { 0x0 } , 1 ) ;
301+ // Subcommand(0x30, new byte[] { 0x0 }, 1); // Turn off LEDS after pair
294302 Subcommand ( 0x40 , new byte [ ] { 0x0 } , 1 ) ;
295303 Subcommand ( 0x48 , new byte [ ] { 0x0 } , 1 ) ;
296- Subcommand ( 0x3 , new byte [ ] { 0x3f } , 1 ) ;
304+ // Subcommand(0x3, new byte[] { 0x3f }, 1); // Turn on basic HID mode - not needed
297305 }
298306 if ( state > state_ . DROPPED ) {
299307 HIDapi . hid_close ( handle ) ;
@@ -358,6 +366,18 @@ public void Update() {
358366 ProcessIMU ( report_buf ) ;
359367 } else {
360368 ExtractIMUValues ( report_buf , 0 ) ;
369+ // 3 values for 5ms precision instead of 15ms
370+ /*for (int n = 0; n < 3; n++) {
371+ ExtractIMUValues(report_buf, n);
372+
373+ Timestamp = rep.ts + (ulong) n * 5000; // 5ms difference
374+
375+ if (n == 0)
376+ ProcessButtonsAndStick(report_buf);
377+
378+ packetCounter++;
379+ Program.server.NewReportIncoming(this);
380+ }*/
361381 }
362382 }
363383 if ( ts_de == report_buf [ 1 ] ) {
@@ -367,20 +387,20 @@ public void Update() {
367387 DebugPrint ( String . Format ( "Dequeue. Queue length: {0}. Packet ID: {1}. Timestamp: {2}. Lag to dequeue: {3}. Lag between packets (expect 15ms): {4}" , reports . Count , report_buf [ 0 ] , report_buf [ 1 ] , System . DateTime . Now . Subtract ( rep . GetTime ( ) ) , rep . GetTime ( ) . Subtract ( ts_prev ) ) , DebugType . THREADING ) ;
368388 ts_prev = rep . GetTime ( ) ;
369389
370- // set timestamp and packet count for server
390+ // Sending values at 5ms is not reliable
371391 Timestamp = rep . ts ;
392+ ProcessButtonsAndStick ( report_buf ) ;
393+ packetCounter ++ ;
394+ Program . server . NewReportIncoming ( this ) ;
372395 }
373- ProcessButtonsAndStick ( report_buf ) ;
396+
374397 if ( rumble_obj . timed_rumble ) {
375398 if ( rumble_obj . t < 0 ) {
376399 rumble_obj . set_vals ( 160 , 320 , 0 , 0 ) ;
377400 } else {
378401 rumble_obj . t -= ( 1 / timing ) ;
379402 }
380403 }
381-
382- packetCounter ++ ;
383- Program . server . NewReportIncoming ( this ) ;
384404 }
385405 }
386406 private int ProcessButtonsAndStick ( byte [ ] report_buf ) {
@@ -454,29 +474,34 @@ private void ExtractIMUValues(byte[] report_buf, int n = 0) {
454474 acc_r [ 0 ] = ( Int16 ) ( report_buf [ 13 + n * 12 ] | ( ( report_buf [ 14 + n * 12 ] << 8 ) & 0xff00 ) ) ;
455475 acc_r [ 1 ] = ( Int16 ) ( report_buf [ 15 + n * 12 ] | ( ( report_buf [ 16 + n * 12 ] << 8 ) & 0xff00 ) ) ;
456476 acc_r [ 2 ] = ( Int16 ) ( report_buf [ 17 + n * 12 ] | ( ( report_buf [ 18 + n * 12 ] << 8 ) & 0xff00 ) ) ;
477+
478+ Int16 [ ] offset ;
479+ if ( isPro )
480+ offset = pro_hor_offset ;
481+ else if ( isLeft )
482+ offset = left_hor_offset ;
483+ else
484+ offset = right_hor_offset ;
485+
486+ //Console.WriteLine("{0} {1} {2}", gyr_r[0], gyr_r[1], gyr_r[2]);
487+
457488 for ( int i = 0 ; i < 3 ; ++ i ) {
458489 switch ( i ) {
459490 case 0 :
460- acc_g . X = acc_r [ i ] * ( 1.0f / ( acc_sensiti [ i ] - acc_neutral [ i ] ) ) * 4.0f ;
461- //gyr_g.X = (gyr_r[i] - gyr_neutral[i]) * 0.00122187695f;
462- gyr_g . X = gyr_r [ i ] * ( 816.0f / ( gyr_sensiti [ i ] - gyr_neutral [ i ] ) ) * 0.5f ; // Neutrals may be read wrong
463- if ( Math . Abs ( acc_g . X ) > Math . Abs ( max [ i ] ) )
464- max [ i ] = acc_g . X ;
491+ acc_g . X = ( acc_r [ i ] - offset [ i ] ) * ( 1.0f / ( acc_sensiti [ i ] - acc_neutral [ i ] ) ) * 4.0f ;
492+ gyr_g . X = gyr_r [ i ] * ( 816.0f / ( gyr_sensiti [ i ] - gyr_neutral [ i ] ) ) ;
493+
465494 break ;
466495 case 1 :
467- acc_g . Y = acc_r [ i ] * ( 1.0f / ( acc_sensiti [ i ] - acc_neutral [ i ] ) ) * 4.0f ;
468- //gyr_g.Y = (gyr_r[i] - gyr_neutral[i]) * 0.00122187695f;
469- gyr_g . Y = - gyr_r [ i ] * ( 816.0f / ( gyr_sensiti [ i ] - gyr_neutral [ i ] ) ) * 0.5f ;
470- if ( Math . Abs ( acc_g . Y ) > Math . Abs ( max [ i ] ) )
471- max [ i ] = acc_g . Y ;
496+ acc_g . Y = ( acc_r [ i ] - offset [ i ] ) * ( 1.0f / ( acc_sensiti [ i ] - acc_neutral [ i ] ) ) * 4.0f ;
497+ gyr_g . Y = - gyr_r [ i ] * ( 816.0f / ( gyr_sensiti [ i ] - gyr_neutral [ i ] ) ) ;
498+
472499 break ;
473500 case 2 :
474- acc_g . Z = acc_r [ i ] * ( 1.0f / ( acc_sensiti [ i ] - acc_neutral [ i ] ) ) * 4.0f ;
475- //gyr_g.Z = (gyr_r[i] - gyr_neutral[i]) * 0.00122187695f;
476- gyr_g . Z = - gyr_r [ i ] * ( 816.0f / ( gyr_sensiti [ i ] - gyr_neutral [ i ] ) ) * 0.5f ;
477- if ( Math . Abs ( acc_g . Z ) > Math . Abs ( max [ i ] ) )
478- max [ i ] = acc_g . Z ;
479- break ;
501+ acc_g . Z = ( acc_r [ i ] - offset [ i ] ) * ( 1.0f / ( acc_sensiti [ i ] - acc_neutral [ i ] ) ) * 4.0f ;
502+ gyr_g . Z = - gyr_r [ i ] * ( 816.0f / ( gyr_sensiti [ i ] - gyr_neutral [ i ] ) ) ;
503+
504+ break ;
480505 }
481506 }
482507 }
0 commit comments