14 Commits
Author SHA1 Message Date
Kostas Missos 3294abb705 Version 5.2.0 update 2018-04-23 23:03:17 +03:00
Kostas Missos 9d0c4636cc Tiny code deduplication 2018-04-23 22:42:39 +03:00
Kostas Missos ebff8b393a Finalize Sensor Cal & Stick dev editing 2018-04-22 18:22:53 +03:00
Kostas Missos 0aebab229f [SPI] Add Stick/Sensor encoding/decoding utils 2018-04-22 18:07:01 +03:00
Kostas Missos 5a6ba9bd49 Improvements and bugfixes
Better IR settings panel organization
Cosmetic changes
Add cmd line option for not checking connection status
Better Gyroscope calibration
Simplified connection handling
Added some comments
2018-04-18 23:32:09 +03:00
Kostas Missos 52f0454b8c [IR] Software Auto Exposure (experimental)
Although, I highly doubt that the camera sensor does not support AE, it seems that the register for this is absent...
Otherwise it depends on more than 1 register.

So, until it is found, implement it manually.
2018-04-11 20:53:50 +03:00
Kostas Missos c77c008499 [IR] Bugfixes 2018-04-11 20:44:13 +03:00
Kostas Missos 3860347aba [IR] Organize settings. Plus some bugfixes 2018-04-08 21:56:32 +03:00
Kostas Missos 1efa0d497c Add app screenshot in readme 2018-04-07 21:49:23 +03:00
Kostas Missos 8c0d440f1b [IR] Optimize get_ir_registers() + some other fixes 2018-04-07 20:56:32 +03:00
Kostas Missos 8de445eb73 [IR] Add loads of new IR settings 2018-04-07 20:53:20 +03:00
Kostas Missos 2529699b13 [IR] Add IR feature registers 2018-04-03 02:08:24 +03:00
Kostas Missos 94283adeff [IR] Get IR registers function + bugfixes 2018-04-03 02:06:42 +03:00
Kostas Missos 3f41f406d5 [IR] Add live configuration support
This allows to change the camera settings on the fly when it's streaming images.
2018-03-30 17:03:39 +03:00
5 changed files with 1953 additions and 810 deletions

No files matched your search

+3
View File
@@ -1,5 +1,8 @@
# Joy-Con Toolkit
![](http://ctcaer.com/wii/jc6.png)
This image is for reference.
## Prerequisites:
**Microsoft Visual C++ 2017 (x86) Redistributable** (All Windows versions)
+1608 -654
View File
File diff suppressed because it is too large. Load diff
-7
View File
@@ -56,10 +56,3 @@ static uint32_t iron_palette[] = {
0xfffffad1, 0xfffffad4, 0xfffffcdb, 0xfffffcdf, 0xfffffde5, 0xfffffde8, 0xfffffeee, 0xfffffff6
};
//ir registers to maximize output
static uint8_t ir_registers[] = {
0x00, 0x00, 0x51, 0x00, 0x0F, 0x7C, 0x01, 0x00, 0x44,
0x01, 0x08, 0x7C, 0x02, 0x00, 0x7B, 0x02, 0x01, 0x7C,
0x03, 0x00, 0x6E, 0x03, 0x06, 0x7C, 0x04, 0x00, 0x05,
0x04, 0x05, 0x1E, 0x04, 0x13, 0x7C
};
+317 -143
View File
@@ -12,6 +12,7 @@
#include "ir_sensor.h"
#include "tune.h"
#include "FormJoy.h"
#include "hidapi.h"
#include "hidapi_log.h"
using namespace CppWinFormJoy;
@@ -20,6 +21,9 @@ using namespace CppWinFormJoy;
bool enable_traffic_dump = false;
hid_device *handle;
hid_device *handle_l;
s16 uint16_to_int16(u16 a) {
s16 b;
char* aPointer = (char*)&a, *bPointer = (char*)&b;
@@ -28,6 +32,14 @@ s16 uint16_to_int16(u16 a) {
}
u16 int16_to_uint16(s16 a) {
u16 b;
char* aPointer = (char*)&a, *bPointer = (char*)&b;
memcpy(bPointer, aPointer, sizeof(a));
return b;
}
u8 mcu_crc8_calc(u8 *buf, u8 size) {
u8 crc8 = 0x0;
@@ -38,6 +50,19 @@ u8 mcu_crc8_calc(u8 *buf, u8 size) {
}
void decode_stick_params(u16 *decoded_stick_params, u8 *encoded_stick_params) {
decoded_stick_params[0] = (encoded_stick_params[1] << 8) & 0xF00 | encoded_stick_params[0];
decoded_stick_params[1] = (encoded_stick_params[2] << 4) | (encoded_stick_params[1] >> 4);
}
void encode_stick_params(u8 *encoded_stick_params, u16 *decoded_stick_params) {
encoded_stick_params[0] = decoded_stick_params[0] & 0xFF;
encoded_stick_params[1] = (decoded_stick_params[0] & 0xF00) >> 8 | (decoded_stick_params[1] & 0xF) << 4;
encoded_stick_params[2] = (decoded_stick_params[1] & 0xFF0) >> 4;
}
// Credit to Hypersect (Ryan Juckett)
// http://blog.hypersect.com/interpreting-analog-sticks/
void AnalogStickCalc(
@@ -758,8 +783,6 @@ int button_test() {
u8 stick_model[0x24];
u8 factory_sensor_cal[0x18];
u8 user_sensor_cal[0x1A];
u16 factory_sensor_cal_calm[0xC];
u16 user_sensor_cal_calm[0xC];
s16 sensor_cal[0x2][0x3];
u16 stick_cal_x_l[0x3];
u16 stick_cal_y_l[0x3];
@@ -771,8 +794,6 @@ int button_test() {
memset(stick_model, 0, 0x12);
memset(factory_sensor_cal, 0, 0x18);
memset(user_sensor_cal, 0, 0x1A);
memset(factory_sensor_cal_calm, 0, 0xC);
memset(user_sensor_cal_calm, 0, 0xC);
memset(sensor_cal, 0, sizeof(sensor_cal));
memset(stick_cal_x_l, 0, sizeof(stick_cal_x_l));
memset(stick_cal_y_l, 0, sizeof(stick_cal_y_l));
@@ -945,12 +966,12 @@ int button_test() {
res = hid_write(handle, buf_cmd, sizeof(buf_cmd));
res = hid_read_timeout(handle, buf_cmd, 0, 120);
// Use SPI calibration and convert them to SI acc unit
// Use SPI calibration and convert them to SI acc unit (m/s^2)
acc_cal_coeff[0] = (float)(1.0 / (float)(16384 - uint16_to_int16(sensor_cal[0][0]))) * 4.0f * 9.8f;
acc_cal_coeff[1] = (float)(1.0 / (float)(16384 - uint16_to_int16(sensor_cal[0][1]))) * 4.0f * 9.8f;
acc_cal_coeff[2] = (float)(1.0 / (float)(16384 - uint16_to_int16(sensor_cal[0][2]))) * 4.0f * 9.8f;
// Use SPI calibration and convert them to SI gyro unit
// Use SPI calibration and convert them to SI gyro unit (rad/s)
gyro_cal_coeff[0] = (float)(936.0 / (float)(13371 - uint16_to_int16(sensor_cal[1][0])) * 0.01745329251994);
gyro_cal_coeff[1] = (float)(936.0 / (float)(13371 - uint16_to_int16(sensor_cal[1][1])) * 0.01745329251994);
gyro_cal_coeff[2] = (float)(936.0 / (float)(13371 - uint16_to_int16(sensor_cal[1][2])) * 0.01745329251994);
@@ -975,8 +996,8 @@ int button_test() {
input_report_cmd += String::Format(L"Vibration decision: ");
input_report_cmd += String::Format(L"{0:X}, {1:X}\r\n", (buf_reply[12] >> 7) & 1, (buf_reply[12] >> 4) & 7);
input_report_cmd += String::Format(L"\r\nButtons: ");
input_report_cmd += String::Format(L"\r\nButtons: ");
for (int i = 3; i < 6; i++)
input_report_cmd += String::Format(L"{0:X2} ", buf_reply[i]);
@@ -1013,20 +1034,20 @@ int button_test() {
input_report_sys = String::Format(L"Acc/meter (Raw/Cal):\r\n");
//The controller sends the sensor data 3 times with a little bit different values. Skip them
input_report_sys += String::Format(L"X: {0:X4} {1,6:F1} m/s\u00B2\r\n", buf_reply[13] | (buf_reply[14] << 8) & 0xFF00,
input_report_sys += String::Format(L"X: {0:X4} {1,7:F2} m/s\u00B2\r\n", buf_reply[13] | (buf_reply[14] << 8) & 0xFF00,
(float)(uint16_to_int16(buf_reply[13] | (buf_reply[14] << 8) & 0xFF00)) * acc_cal_coeff[0]);
input_report_sys += String::Format(L"Y: {0:X4} {1,6:F1} m/s\u00B2\r\n", buf_reply[15] | (buf_reply[16] << 8) & 0xFF00,
input_report_sys += String::Format(L"Y: {0:X4} {1,7:F2} m/s\u00B2\r\n", buf_reply[15] | (buf_reply[16] << 8) & 0xFF00,
(float)(uint16_to_int16(buf_reply[15] | (buf_reply[16] << 8) & 0xFF00)) * acc_cal_coeff[1]);
input_report_sys += String::Format(L"Z: {0:X4} {1,6:F1} m/s\u00B2\r\n", buf_reply[17] | (buf_reply[18] << 8) & 0xFF00,
input_report_sys += String::Format(L"Z: {0:X4} {1,7:F2} m/s\u00B2\r\n", buf_reply[17] | (buf_reply[18] << 8) & 0xFF00,
(float)(uint16_to_int16(buf_reply[17] | (buf_reply[18] << 8) & 0xFF00)) * acc_cal_coeff[2]);
input_report_sys += String::Format(L"\r\nGyroscope (Raw/Cal):\r\n");
input_report_sys += String::Format(L"X: {0:X4} {1,6:F1} rad/s\r\n", buf_reply[19] | (buf_reply[20] << 8) & 0xFF00,
(float)(uint16_to_int16(buf_reply[19] | (buf_reply[20] << 8) & 0xFF00)) * gyro_cal_coeff[0]);
input_report_sys += String::Format(L"Y: {0:X4} {1,6:F1} rad/s\r\n", buf_reply[21] | (buf_reply[22] << 8) & 0xFF00,
(float)(uint16_to_int16(buf_reply[21] | (buf_reply[22] << 8) & 0xFF00)) * gyro_cal_coeff[1]);
input_report_sys += String::Format(L"Z: {0:X4} {1,6:F1} rad/s\r\n", buf_reply[23] | (buf_reply[24] << 8) & 0xFF00,
(float)(uint16_to_int16(buf_reply[23] | (buf_reply[24] << 8) & 0xFF00)) * gyro_cal_coeff[2]);
input_report_sys += String::Format(L"X: {0:X4} {1,7:F2} rad/s\r\n", buf_reply[19] | (buf_reply[20] << 8) & 0xFF00,
(float)(uint16_to_int16(buf_reply[19] | (buf_reply[20] << 8) & 0xFF00) - uint16_to_int16(sensor_cal[1][0])) * gyro_cal_coeff[0]);
input_report_sys += String::Format(L"Y: {0:X4} {1,7:F2} rad/s\r\n", buf_reply[21] | (buf_reply[22] << 8) & 0xFF00,
(float)(uint16_to_int16(buf_reply[21] | (buf_reply[22] << 8) & 0xFF00) - uint16_to_int16(sensor_cal[1][1])) * gyro_cal_coeff[1]);
input_report_sys += String::Format(L"Z: {0:X4} {1,7:F2} rad/s\r\n", buf_reply[23] | (buf_reply[24] << 8) & 0xFF00,
(float)(uint16_to_int16(buf_reply[23] | (buf_reply[24] << 8) & 0xFF00) - uint16_to_int16(sensor_cal[1][2])) * gyro_cal_coeff[2]);
}
else if (buf_reply[0] == 0x3F) {
input_report_cmd = L"";
@@ -1035,7 +1056,7 @@ int button_test() {
}
if (limit_output == 1) {
FormJoy::myform1->textBox_btn_test_reply->Text = input_report_cmd;
FormJoy::myform1->textBox_btn_test_reply->Text = input_report_cmd;
FormJoy::myform1->textBox_btn_test_subreply->Text = input_report_sys;
}
//Only update every 75ms for better readability. No need for real time parsing.
@@ -1329,7 +1350,49 @@ int play_hd_rumble_file(int file_type, u16 sample_rate, int samples, int loop_st
}
int get_raw_ir_image(u8 max_frag_no, u8 show_status) {
int ir_sensor_auto_exposure(int white_pixels_percent) {
int res;
u8 buf[49];
u16 new_exposure = 0;
int old_exposure = (u16)FormJoy::myform1->numeric_IRExposure->Value;
// Calculate new exposure;
if (white_pixels_percent == 0)
old_exposure += 10;
else if (white_pixels_percent > 5)
old_exposure -= (white_pixels_percent / 4) * 20;
old_exposure = CLAMP(old_exposure, 0, 600);
FormJoy::myform1->numeric_IRExposure->Value = old_exposure;
new_exposure = old_exposure * 31200 / 1000;
memset(buf, 0, sizeof(buf));
auto hdr = (brcm_hdr *)buf;
auto pkt = (brcm_cmd_01 *)(hdr + 1);
hdr->cmd = 0x01;
hdr->timer = timming_byte & 0xF;
timming_byte++;
pkt->subcmd = 0x21;
pkt->subcmd_21_23_04.mcu_cmd = 0x23; // Write register cmd
pkt->subcmd_21_23_04.mcu_subcmd = 0x04; // Write register to IR mode subcmd
pkt->subcmd_21_23_04.no_of_reg = 0x03; // Number of registers to write. Max 9.
pkt->subcmd_21_23_04.reg1_addr = 0x3001; // R: 0x0130 - Set Exposure time LSByte
pkt->subcmd_21_23_04.reg1_val = new_exposure & 0xFF;
pkt->subcmd_21_23_04.reg2_addr = 0x3101; // R: 0x0131 - Set Exposure time MSByte
pkt->subcmd_21_23_04.reg2_val = (new_exposure & 0xFF00) >> 8;
pkt->subcmd_21_23_04.reg3_addr = 0x0700; // R: 0x0007 - Finalize config - Without this, the register changes do not have any effect.
pkt->subcmd_21_23_04.reg3_val = 0x01;
buf[48] = mcu_crc8_calc(buf + 12, 36);
res = hid_write(handle, buf, sizeof(buf));
return res;
}
int get_raw_ir_image(u8 show_status) {
std::stringstream ir_status;
int elapsed_time = 0;
@@ -1338,7 +1401,7 @@ int get_raw_ir_image(u8 max_frag_no, u8 show_status) {
u8 buf[49];
u8 buf_reply[0x170];
u8 *buf_image = new u8[76800]; // 8bpp greyscale image.
u8 *buf_image = new u8[19 * 4096]; // 8bpp greyscale image.
uint16_t bad_signal = 0;
int error_reading = 0;
float noise_level = 0.0f;
@@ -1348,7 +1411,7 @@ int get_raw_ir_image(u8 max_frag_no, u8 show_status) {
int missed_packet_no = 0;
bool missed_packet = false;
int initialization = 2;
int max_pixels = ((max_frag_no < 218 ? max_frag_no : 217) + 1) * 300;
int max_pixels = ((ir_max_frag_no < 218 ? ir_max_frag_no : 217) + 1) * 300;
int white_pixels_percent = 0;
memset(buf_image, 0, sizeof(buf_image));
@@ -1373,10 +1436,11 @@ int get_raw_ir_image(u8 max_frag_no, u8 show_status) {
while (enable_IRVideoPhoto || initialization) {
memset(buf_reply, 0, sizeof(buf_reply));
hid_read_timeout(handle, buf_reply, sizeof(buf_reply), 200);
//Check if new packet
if (buf_reply[0] == 0x31 && buf_reply[49] == 0x03) {
got_frag_no = buf_reply[52];
if (got_frag_no == (previous_frag_no + 1) % (max_frag_no + 1)) {
if (got_frag_no == (previous_frag_no + 1) % (ir_max_frag_no + 1)) {
previous_frag_no = got_frag_no;
@@ -1389,6 +1453,13 @@ int get_raw_ir_image(u8 max_frag_no, u8 show_status) {
memcpy(buf_image + (300 * got_frag_no), buf_reply + 59, 300);
// Auto exposure.
// TODO: Fix placement, so it doesn't drop next fragment.
if (enable_IRAutoExposure && initialization < 2 && got_frag_no == 0){
white_pixels_percent = (int)((*(u16*)&buf_reply[55] * 100) / max_pixels);
ir_sensor_auto_exposure(white_pixels_percent);
}
// Status percentage
ir_status.str("");
ir_status.clear();
@@ -1401,7 +1472,7 @@ int get_raw_ir_image(u8 max_frag_no, u8 show_status) {
else
ir_status << "Status: Initializing.. ";
ir_status << std::setfill(' ') << std::setw(3);
ir_status << std::fixed << std::setprecision(0) << (float)got_frag_no / (float)(max_frag_no + 1) * 100.0f;
ir_status << std::fixed << std::setprecision(0) << (float)got_frag_no / (float)(ir_max_frag_no + 1) * 100.0f;
ir_status << "% - ";
//debug
@@ -1411,7 +1482,7 @@ int get_raw_ir_image(u8 max_frag_no, u8 show_status) {
elapsed_time = sw->ElapsedMilliseconds;
// Check if final fragment. Draw the frame.
if (got_frag_no == max_frag_no) {
if (got_frag_no == ir_max_frag_no) {
// Update Viewport
elapsed_time2 = sw->ElapsedMilliseconds - elapsed_time2;
FormJoy::myform1->setIRPictureWindow(buf_image, true);
@@ -1427,7 +1498,7 @@ int get_raw_ir_image(u8 max_frag_no, u8 show_status) {
noise_level = (float)(*(u16*)&buf_reply[57]) / ((float)(*(u16*)&buf_reply[55]) + 1.0f);
white_pixels_percent = (int)((*(u16*)&buf_reply[55] * 100) / max_pixels);
avg_intensity_percent = (int)((buf_reply[53] * 100) / 255);
FormJoy::myform1->lbl_IRHelp->Text = String::Format("Amb Noise: {0:f2}, Int: {1:D}%, FPS: {2:D} ({3:D}ms)\nEXFilter: {4:D}, White Px: {5:D}%, ??: {6:D}",
FormJoy::myform1->lbl_IRHelp->Text = String::Format("Amb Noise: {0:f2}, Int: {1:D}%, FPS: {2:D} ({3:D}ms)\nEXFilter: {4:D}, White Px: {5:D}%, EXF Int: {6:D}",
noise_level, avg_intensity_percent, (int)(1000 / elapsed_time2), elapsed_time2, *(u16*)&buf_reply[57], white_pixels_percent, buf_reply[54]);
elapsed_time2 = sw->ElapsedMilliseconds;
@@ -1455,7 +1526,7 @@ int get_raw_ir_image(u8 max_frag_no, u8 show_status) {
}
// Check if missed fragment and request it.
else if(missed_packet_no != got_frag_no && !missed_packet) {
if (max_frag_no != 0x03) {
if (ir_max_frag_no != 0x03) {
//debug
//printf("%02X Frag: Missed %02X, Prev: %02X, PrevM: %02X\n", got_frag_no, previous_frag_no + 1, previous_frag_no, missed_packet_no);
@@ -1537,7 +1608,7 @@ int get_raw_ir_image(u8 max_frag_no, u8 show_status) {
else
ir_status << "Status: Initializing.. ";
ir_status << std::setfill(' ') << std::setw(3);
ir_status << std::fixed << std::setprecision(0) << (float)got_frag_no / (float)(max_frag_no + 1) * 100.0f;
ir_status << std::fixed << std::setprecision(0) << (float)got_frag_no / (float)(ir_max_frag_no + 1) * 100.0f;
ir_status << "% - ";
FormJoy::myform1->lbl_IRStatus->Text = gcnew String(ir_status.str().c_str()) + (sw->ElapsedMilliseconds - elapsed_time).ToString() + "ms";
@@ -1598,8 +1669,7 @@ int get_raw_ir_image(u8 max_frag_no, u8 show_status) {
}
int ir_sensor(u8* buf_image, u8 ir_res_reg, u8 ir_res_no_of_packets,
u16 ir_exposure, u8 ir_leds, u8 ir_digital_gain, u8 ir_ex_light_filter) {
int ir_sensor(ir_image_config &ir_cfg) {
int res;
u8 buf[0x170];
static int output_buffer_length = 49;
@@ -1666,7 +1736,7 @@ step1:
step2:
// Request MCU mode status
error_reading = 0;
while (1) {
while (1) { // Not necessary, but we keep to make sure the MCU is ready.
memset(buf, 0, sizeof(buf));
auto hdr = (brcm_hdr *)buf;
auto pkt = (brcm_cmd_01 *)(hdr + 1);
@@ -1709,9 +1779,9 @@ step3:
timming_byte++;
pkt->subcmd = 0x21;
pkt->subcmd_21_21.mcu_cmd = 0x21; // Set MCU mode cmd
pkt->subcmd_21_21.mcu_cmd = 0x21; // Set MCU mode cmd
pkt->subcmd_21_21.mcu_subcmd = 0x00; // Set MCU mode cmd
pkt->subcmd_21_21.mcu_mode = 0x05; // MCU mode - 1: Standby, 4: NFC, 5: IR, 6: Initializing/FW Update?
pkt->subcmd_21_21.mcu_mode = 0x05; // MCU mode - 1: Standby, 4: NFC, 5: IR, 6: Initializing/FW Update?
buf[48] = mcu_crc8_calc(buf + 12, 36);
res = hid_write(handle, buf, output_buffer_length);
@@ -1739,7 +1809,7 @@ step3:
step4:
// Request MCU mode status
error_reading = 0;
while (1) {
while (1) { // Not necessary, but we keep to make sure the MCU mode changed.
memset(buf, 0, sizeof(buf));
auto hdr = (brcm_hdr *)buf;
auto pkt = (brcm_cmd_01 *)(hdr + 1);
@@ -1782,11 +1852,11 @@ step5:
pkt->subcmd = 0x21;
pkt->subcmd_21_23_01.mcu_cmd = 0x23;
pkt->subcmd_21_23_01.mcu_subcmd = 0x01; // Set IR mode cmd
pkt->subcmd_21_23_01.mcu_ir_mode = 0x07; // IR mode - 2: No mode/Disable?, 3: Moment, 4: Dpd, 6: Clustering,
pkt->subcmd_21_23_01.mcu_ir_mode = 0x07; // IR mode - 2: No mode/Disable?, 3: Moment, 4: Dpd (Wii-style pointing), 6: Clustering,
// 7: Image transfer, 8-10: Hand analysis (Silhouette, Image, Silhouette/Image), 0,1/5/10+: Unknown
pkt->subcmd_21_23_01.no_of_frags = ir_res_no_of_packets; // Set number of packets to output per buffer
pkt->subcmd_21_23_01.mcu_major_v = 0x0300; // Set required IR MCU FW v3.09. Major 0x0003.
pkt->subcmd_21_23_01.mcu_minor_v = 0x0900; // Set required IR MCU FW v3.09. Minor 0x0009.
pkt->subcmd_21_23_01.no_of_frags = ir_max_frag_no; // Set number of packets to output per buffer
pkt->subcmd_21_23_01.mcu_major_v = 0x0500; // Set required IR MCU FW v5.18. Major 0x0005.
pkt->subcmd_21_23_01.mcu_minor_v = 0x1800; // Set required IR MCU FW v5.18. Minor 0x0018.
buf[48] = mcu_crc8_calc(buf + 12, 36);
res = hid_write(handle, buf, output_buffer_length);
@@ -1812,7 +1882,7 @@ step5:
step6:
// Request IR mode status
error_reading = 0;
while (1) {
while (0) { // Not necessary
memset(buf, 0, sizeof(buf));
auto hdr = (brcm_hdr *)buf;
auto pkt = (brcm_cmd_01 *)(hdr + 1);
@@ -1862,22 +1932,22 @@ step7:
pkt->subcmd_21_23_04.no_of_reg = 0x09; // Number of registers to write. Max 9.
pkt->subcmd_21_23_04.reg1_addr = 0x2e00; // R: 0x002e - Set Resolution based on sensor binning and skipping
pkt->subcmd_21_23_04.reg1_val = ir_res_reg;
pkt->subcmd_21_23_04.reg1_val = ir_cfg.ir_res_reg;
pkt->subcmd_21_23_04.reg2_addr = 0x3001; // R: 0x0130 - Set Exposure time LSByte - (31200 * us /1000) & 0xFF - Max: 600us, Max encoded: 0x4920.
pkt->subcmd_21_23_04.reg2_val = ir_exposure & 0xFF;
pkt->subcmd_21_23_04.reg2_val = ir_cfg.ir_exposure & 0xFF;
pkt->subcmd_21_23_04.reg3_addr = 0x3101; // R: 0x0131 - Set Exposure time MSByte - ((31200 * us /1000) & 0xFF00) >> 8
pkt->subcmd_21_23_04.reg3_val = (ir_exposure & 0xFF00) >> 8;
pkt->subcmd_21_23_04.reg3_val = (ir_cfg.ir_exposure & 0xFF00) >> 8;
pkt->subcmd_21_23_04.reg4_addr = 0x3201; // R: 0x0132 - Enable Max exposure Time - 0: Manual exposure, 1: Max exposure
pkt->subcmd_21_23_04.reg4_val = 0x00;
pkt->subcmd_21_23_04.reg5_addr = 0x1000; // R: 0x0010 - Set IR Leds groups state - Only 3 LSB usable
pkt->subcmd_21_23_04.reg5_val = ir_leds;
pkt->subcmd_21_23_04.reg6_addr = 0x2e01; // R: 0x012e - Set digital gain LSB 4 bits of the value
pkt->subcmd_21_23_04.reg6_val = (ir_digital_gain & 0xF) << 4;
pkt->subcmd_21_23_04.reg7_addr = 0x2f01; // R: 0x012f - Set digital gain MSB 4 bits of the value
pkt->subcmd_21_23_04.reg7_val = (ir_digital_gain & 0xF0) >> 4;
pkt->subcmd_21_23_04.reg5_val = ir_cfg.ir_leds;
pkt->subcmd_21_23_04.reg6_addr = 0x2e01; // R: 0x012e - Set digital gain LSB 4 bits of the value - 0-0xff
pkt->subcmd_21_23_04.reg6_val = (ir_cfg.ir_digital_gain & 0xF) << 4;
pkt->subcmd_21_23_04.reg7_addr = 0x2f01; // R: 0x012f - Set digital gain MSB 4 bits of the value - 0-0x7
pkt->subcmd_21_23_04.reg7_val = (ir_cfg.ir_digital_gain & 0xF0) >> 4;
pkt->subcmd_21_23_04.reg8_addr = 0x0e00; // R: 0x00e0 - External light filter - LS o bit0: Off/On, bit1: 0x/1x, bit2: ??, bit4,5: ??.
pkt->subcmd_21_23_04.reg8_val = ir_ex_light_filter; // Valid values: 0, 3, 7, 51, 55.
pkt->subcmd_21_23_04.reg9_addr = 0x4301; // R: 0x0143 - Unknown - 200: Default
pkt->subcmd_21_23_04.reg8_val = ir_cfg.ir_ex_light_filter;
pkt->subcmd_21_23_04.reg9_addr = 0x4301; // R: 0x0143 - ExLF/White pixel stats threshold - 200: Default
pkt->subcmd_21_23_04.reg9_val = 0xc8;
buf[48] = mcu_crc8_calc(buf + 12, 36);
@@ -1927,10 +1997,27 @@ step8:
pkt->subcmd_21_23_04.mcu_cmd = 0x23; // Write register cmd
pkt->subcmd_21_23_04.mcu_subcmd = 0x04; // Write register to IR mode subcmd
pkt->subcmd_21_23_04.no_of_reg = 0x01; // Number of registers to write. Max 9.
pkt->subcmd_21_23_04.no_of_reg = 0x08; // Number of registers to write. Max 9.
pkt->subcmd_21_23_04.reg1_addr = 0x0700; // R: 0x0007 - Finalize config - Without this, the register changes do not have any effect.
pkt->subcmd_21_23_04.reg1_val = 0x01;
pkt->subcmd_21_23_04.reg1_addr = 0x1100; // R: 0x0011 - Leds 1/2 Intensity - Max 0x0F.
pkt->subcmd_21_23_04.reg1_val = (ir_cfg.ir_leds_intensity >> 8) & 0xFF;
pkt->subcmd_21_23_04.reg2_addr = 0x1200; // R: 0x0012 - Leds 3/4 Intensity - Max 0x10.
pkt->subcmd_21_23_04.reg2_val = ir_cfg.ir_leds_intensity & 0xFF;
pkt->subcmd_21_23_04.reg3_addr = 0x2d00; // R: 0x002d - Flip image - 0: Normal, 1: Vertically, 2: Horizontally, 3: Both
pkt->subcmd_21_23_04.reg3_val = ir_cfg.ir_flip;
pkt->subcmd_21_23_04.reg4_addr = 0x6701; // R: 0x0167 - Enable De-noise smoothing algorithms - 0: Disable, 1: Enable.
pkt->subcmd_21_23_04.reg4_val = (ir_cfg.ir_denoise >> 16) & 0xFF;
pkt->subcmd_21_23_04.reg5_addr = 0x6801; // R: 0x0168 - Edge smoothing threshold - Max 0xFF, Default 0x23
pkt->subcmd_21_23_04.reg5_val = (ir_cfg.ir_denoise >> 8) & 0xFF;
pkt->subcmd_21_23_04.reg6_addr = 0x6901; // R: 0x0169 - Color Interpolation threshold - Max 0xFF, Default 0x44
pkt->subcmd_21_23_04.reg6_val = ir_cfg.ir_denoise & 0xFF;
pkt->subcmd_21_23_04.reg7_addr = 0x0400; // R: 0x0004 - LSB Buffer Update Time - Default 0x32
if (ir_cfg.ir_res_reg == 0x69)
pkt->subcmd_21_23_04.reg7_val = 0x2d; // A value of <= 0x2d is fast enough for 30 x 40, so the first fragment has the updated frame.
else
pkt->subcmd_21_23_04.reg7_val = 0x32; // All the other resolutions the default is enough. Otherwise a lower value can break hand analysis.
pkt->subcmd_21_23_04.reg8_addr = 0x0700; // R: 0x0007 - Finalize config - Without this, the register changes do not have any effect.
pkt->subcmd_21_23_04.reg8_val = 0x01;
buf[48] = mcu_crc8_calc(buf + 12, 36);
res = hid_write(handle, buf, output_buffer_length);
@@ -1959,67 +2046,11 @@ step8:
}
step9:
// Get the IR registers
error_reading = 0;
int pos_ir_registers = 0;
while (0) {
memset(buf, 0, sizeof(buf));
auto hdr = (brcm_hdr *)buf;
auto pkt = (brcm_cmd_01 *)(hdr + 1);
memset(buf, 0, sizeof(buf));
hdr->cmd = 0x11;
hdr->timer = timming_byte & 0xF;
timming_byte++;
pkt->subcmd = 0x03;
pkt->subcmd_arg.arg1 = 0x03;
buf[12] = 0x01; // seems to be always 0x01
buf[13] = ir_registers[pos_ir_registers]; //0-4
buf[14] = ir_registers[pos_ir_registers + 1]; //offset
buf[15] = ir_registers[pos_ir_registers + 2]; // CRC? Pass?
buf[48] = mcu_crc8_calc(buf + 11, 37);
res = hid_write(handle, buf, output_buffer_length);
Sleep(15);
res = hid_write(handle, buf, output_buffer_length);
int tries = 0;
while (1) {
res = hid_read_timeout(handle, buf, sizeof(buf), 64);
if (buf[49] == 0x1b && buf[52] == ir_registers[pos_ir_registers + 1] && buf[53] == ir_registers[pos_ir_registers + 2]) {
printf("%02X, %02X, %02X - SUCCESS:\n", buf[51], buf[52], buf[53]);
for (int i = 0; i < buf[52] + buf[53]; i++)
printf("%02X", buf[54 + i]);
printf("\n");
//buf[50] 0: success, out of range
break;
}
tries++;
if (tries > 8) {
printf("%02X, %02X, %02X - FAILED\n", ir_registers[pos_ir_registers], ir_registers[pos_ir_registers + 1], ir_registers[pos_ir_registers + 2]);
break;
}
}
pos_ir_registers += 3;
if (pos_ir_registers > 30) {
printf("\n");
printf("\n");
break;
}
//error_reading++;
//if (error_reading > 7) {
// res_get = 10;
// goto step10;
//}
}
// Stream or Capture images from NIR Camera
if (enable_IRVideoPhoto)
res_get = get_raw_ir_image(ir_res_no_of_packets, 2);
res_get = get_raw_ir_image(2);
else
res_get = get_raw_ir_image(ir_res_no_of_packets, 1);
res_get = get_raw_ir_image(1);
//////
// TODO: Should we send subcmd x21 with 'x230102' to disable IR mode before disabling MCU?
@@ -2070,6 +2101,143 @@ stepf:
}
int get_ir_registers(int start_reg, int reg_group) {
int res;
u8 buf[0x170];
static int output_buffer_length = 49;
int error_reading = 0;
int res_get = 0;
// Get the IR registers
error_reading = 0;
int pos_ir_registers = start_reg;
while (1) {
repeat_send:
memset(buf, 0, sizeof(buf));
auto hdr = (brcm_hdr *)buf;
auto pkt = (brcm_cmd_01 *)(hdr + 1);
memset(buf, 0, sizeof(buf));
hdr->cmd = 0x11;
hdr->timer = timming_byte & 0xF;
timming_byte++;
pkt->subcmd = 0x03;
pkt->subcmd_arg.arg1 = 0x03;
buf[12] = 0x1; // seems to be always 0x01
buf[13] = pos_ir_registers; // 0-4 registers page/group
buf[14] = 0x00; // offset. this plus the number of registers, must be less than x7f
buf[15] = 0x7f; // Number of registers to show + 1
buf[47] = mcu_crc8_calc(buf + 11, 36);
res = hid_write(handle, buf, output_buffer_length);
int tries = 0;
while (1) {
res = hid_read_timeout(handle, buf, sizeof(buf), 64);
if (buf[49] == 0x1b && buf[51] == pos_ir_registers && buf[52] == 0x00) {
error_reading = 0;
printf("--->%02X, %02X : %02X:\n", buf[51], buf[52], buf[53]);
for (int i = 0; i <= buf[52] + buf[53]; i++)
if ((i & 0xF) == 0xF)
printf("%02X | ", buf[54 + i]);
else
printf("%02X ", buf[54 + i]);
printf("\n");
break;
}
tries++;
if (tries > 8) {
error_reading++;
if (error_reading > 5) {
return 1;
}
goto repeat_send;
}
}
pos_ir_registers++;
if (pos_ir_registers > reg_group) {
break;
}
}
printf("\n");
return 0;
}
int ir_sensor_config_live(ir_image_config &ir_cfg) {
int res;
u8 buf[49];
memset(buf, 0, sizeof(buf));
auto hdr = (brcm_hdr *)buf;
auto pkt = (brcm_cmd_01 *)(hdr + 1);
hdr->cmd = 0x01;
hdr->timer = timming_byte & 0xF;
timming_byte++;
pkt->subcmd = 0x21;
pkt->subcmd_21_23_04.mcu_cmd = 0x23; // Write register cmd
pkt->subcmd_21_23_04.mcu_subcmd = 0x04; // Write register to IR mode subcmd
pkt->subcmd_21_23_04.no_of_reg = 0x09; // Number of registers to write. Max 9.
pkt->subcmd_21_23_04.reg1_addr = 0x3001; // R: 0x0130 - Set Exposure time LSByte
pkt->subcmd_21_23_04.reg1_val = ir_cfg.ir_exposure & 0xFF;
pkt->subcmd_21_23_04.reg2_addr = 0x3101; // R: 0x0131 - Set Exposure time MSByte
pkt->subcmd_21_23_04.reg2_val = (ir_cfg.ir_exposure & 0xFF00) >> 8;
pkt->subcmd_21_23_04.reg3_addr = 0x1000; // R: 0x0010 - Set IR Leds groups state
pkt->subcmd_21_23_04.reg3_val = ir_cfg.ir_leds;
pkt->subcmd_21_23_04.reg4_addr = 0x2e01; // R: 0x012e - Set digital gain LSB 4 bits
pkt->subcmd_21_23_04.reg4_val = (ir_cfg.ir_digital_gain & 0xF) << 4;
pkt->subcmd_21_23_04.reg5_addr = 0x2f01; // R: 0x012f - Set digital gain MSB 4 bits
pkt->subcmd_21_23_04.reg5_val = (ir_cfg.ir_digital_gain & 0xF0) >> 4;
pkt->subcmd_21_23_04.reg6_addr = 0x0e00; // R: 0x00e0 - External light filter
pkt->subcmd_21_23_04.reg6_val = ir_cfg.ir_ex_light_filter;
pkt->subcmd_21_23_04.reg7_addr = (ir_cfg.ir_custom_register & 0xFF) << 8 | (ir_cfg.ir_custom_register >> 8) & 0xFF;
pkt->subcmd_21_23_04.reg7_val = (ir_cfg.ir_custom_register >> 16) & 0xFF;
pkt->subcmd_21_23_04.reg8_addr = 0x1100; // R: 0x0011 - Leds 1/2 Intensity - Max 0x0F.
pkt->subcmd_21_23_04.reg8_val = (ir_cfg.ir_leds_intensity >> 8) & 0xFF;
pkt->subcmd_21_23_04.reg9_addr = 0x1200; // R: 0x0012 - Leds 3/4 Intensity - Max 0x10.
pkt->subcmd_21_23_04.reg9_val = ir_cfg.ir_leds_intensity & 0xFF;
buf[48] = mcu_crc8_calc(buf + 12, 36);
res = hid_write(handle, buf, sizeof(buf));
// Important. Otherwise we gonna have a dropped packet.
Sleep(15);
pkt->subcmd_21_23_04.no_of_reg = 0x06; // Number of registers to write. Max 9.
pkt->subcmd_21_23_04.reg1_addr = 0x2d00; // R: 0x002d - Flip image - 0: Normal, 1: Vertically, 2: Horizontally, 3: Both
pkt->subcmd_21_23_04.reg1_val = ir_cfg.ir_flip;
pkt->subcmd_21_23_04.reg2_addr = 0x6701; // R: 0x0167 - Enable De-noise smoothing algorithms - 0: Disable, 1: Enable.
pkt->subcmd_21_23_04.reg2_val = (ir_cfg.ir_denoise >> 16) & 0xFF;
pkt->subcmd_21_23_04.reg3_addr = 0x6801; // R: 0x0168 - Edge smoothing threshold - Max 0xFF, Default 0x23
pkt->subcmd_21_23_04.reg3_val = (ir_cfg.ir_denoise >> 8) & 0xFF;
pkt->subcmd_21_23_04.reg4_addr = 0x6901; // R: 0x0169 - Color Interpolation threshold - Max 0xFF, Default 0x44
pkt->subcmd_21_23_04.reg4_val = ir_cfg.ir_denoise & 0xFF;
pkt->subcmd_21_23_04.reg5_addr = 0x0400; // R: 0x0004 - LSB Buffer Update Time - Default 0x32
if (ir_cfg.ir_res_reg == 0x69)
pkt->subcmd_21_23_04.reg5_val = 0x2d; // A value of <= 0x2d is fast enough for 30 x 40, so the first fragment has the updated frame.
else
pkt->subcmd_21_23_04.reg5_val = 0x32; // All the other resolutions the default is enough. Otherwise a lower value can break hand analysis.
pkt->subcmd_21_23_04.reg6_addr = 0x0700; // R: 0x0007 - Finalize config - Without this, the register changes do not have any effect.
pkt->subcmd_21_23_04.reg6_val = 0x01;
buf[48] = mcu_crc8_calc(buf + 12, 36);
res = hid_write(handle, buf, sizeof(buf));
// get_ir_registers(0,4); // Get all register pages
// get_ir_registers((ir_cfg.ir_custom_register >> 8) & 0xFF, (ir_cfg.ir_custom_register >> 8) & 0xFF); // Get all registers based on changed register's page
return res;
}
int nfc_tag_info() {
/////////////////////////////////////////////////////
// Kudos to Eric Betts (https://github.com/bettse) //
@@ -2186,9 +2354,9 @@ int nfc_tag_info() {
timming_byte++;
pkt->subcmd = 0x21;
pkt->subcmd_21_21.mcu_cmd = 0x21; // Set MCU mode cmd
pkt->subcmd_21_21.mcu_cmd = 0x21; // Set MCU mode cmd
pkt->subcmd_21_21.mcu_subcmd = 0x00; // Set MCU mode cmd
pkt->subcmd_21_21.mcu_mode = 0x04; // MCU mode - 1: Standby, 4: NFC, 5: IR, 6: Initializing/FW Update?
pkt->subcmd_21_21.mcu_mode = 0x04; // MCU mode - 1: Standby, 4: NFC, 5: IR, 6: Initializing/FW Update?
buf[48] = mcu_crc8_calc(buf + 12, 36);
res = hid_write(handle, buf, output_buffer_length);
@@ -2320,6 +2488,7 @@ int nfc_tag_info() {
// buf[55] always x31?
// buf[56]: MCU/NFC state
// buf[62]: nfc tag type
// buf[62]: MSB size?
// buf[64]: size of following data and it's the last NFC header byte
if (buf[49] == 0x2a && *(u16*)&buf[50] == 0x0500 && buf[56] == 0x09) { // buf[56] == 0x09: Tag detected
//for (int i = 0; i < 8; i++) {
@@ -2600,16 +2769,27 @@ int test_chamber() {
}
int device_connection(){
handle_ok = 0;
if (handle = hid_open(0x57e, 0x2006, nullptr))
handle_ok = 1;
if (!handle_ok) {
if (handle = hid_open(0x57e, 0x2007, nullptr))
if (check_connection_ok) {
handle_ok = 0;
// Joy-Con (L)
if (handle = hid_open(0x57e, 0x2006, nullptr)) {
handle_ok = 1;
return 1;
}
// Joy-Con (R)
if (handle = hid_open(0x57e, 0x2007, nullptr)) {
handle_ok = 2;
}
if (!handle_ok) {
if (handle = hid_open(0x57e, 0x2009, nullptr))
return 2;
}
// Pro Controller
if (handle = hid_open(0x57e, 0x2009, nullptr)) {
handle_ok = 3;
return 3;
}
// Nothing found
else {
return 0;
}
}
/*
//usb test
@@ -2617,12 +2797,6 @@ int device_connection(){
hid_init();
struct hid_device_info *devs = hid_enumerate(0x057E, 0x200e);
if (devs){
BOOL chk = AllocConsole();
if (chk)
{
freopen("CONOUT$", "w", stdout);
printf(" printing to console\n");
}
usb_device_print(devs);
handle_l = hid_open_path(devs->path);
devs = devs->next;
@@ -2635,37 +2809,37 @@ int device_connection(){
hid_free_enumeration(devs);
}
*/
if (handle_ok == 0)
return 0;
return 1;
return handle_ok;
}
[STAThread]
int Main(array<String^>^ args) {
/*
BOOL chk = AllocConsole();
if (chk) {
freopen("CONOUT$", "w", stdout);
}
*/
check_connection_ok = true;
while (!device_connection()) {
if (MessageBox::Show(L"The device is not paired or the device was disconnected!\n\n" +
"To pair:\n 1. Press and hold the sync button until the leds are on\n" +
" 2. Pair the Bluetooth controller in Windows\n\nTo connect again:\n" +
" 1. Press a button on the controller\n (If this doesn\'t work, re-pair.)\n\n" +
"To re-pair:\n 1. Go to 'Settings -> Devices' or Devices and Printers'\n" +
" 2. Remove the controller\n 3. Follow the pair instructions",
if (MessageBox::Show(
L"The device is not paired or the device was disconnected!\n\n" +
L"To pair:\n 1. Press and hold the sync button until the leds are on\n" +
L" 2. Pair the Bluetooth controller in Windows\n\nTo connect again:\n" +
L" 1. Press a button on the controller\n (If this doesn\'t work, re-pair.)\n\n" +
L"To re-pair:\n 1. Go to 'Settings -> Devices' or Devices and Printers'\n" +
L" 2. Remove the controller\n 3. Follow the pair instructions",
L"CTCaer's Joy-Con Toolkit - Connection Error!",
MessageBoxButtons::RetryCancel, MessageBoxIcon::Stop) == System::Windows::Forms::DialogResult::Cancel)
return 1;
}
// Enable hid traffic debug
// Enable debugging
if (args->Length > 0) {
if (args[0] == "-d")
enable_traffic_dump = true;
enable_traffic_dump = true; // Enable hid_write/read logging to text file
else if (args[0] == "-f")
check_connection_ok = false; // Don't check connection after the 1st successful one
}
//BOOL chk = AllocConsole();
//if (chk)
//{
//freopen("CONOUT$", "w", stdout);
//printf(" printing to console\n");
//}
timming_byte = 0x0;
+25 -6
View File
@@ -1,6 +1,5 @@
#pragma once
#include <cstdint>
#include "hidapi.h"
using namespace System;
@@ -81,10 +80,28 @@ struct brcm_cmd_01 {
};
};
struct ir_image_config {
u8 ir_res_reg;
u16 ir_exposure;
u8 ir_leds; // Leds to enable, Strobe/Flashlight modes
u16 ir_leds_intensity; // MSByte: Leds 1/2, LSB: Leds 3/4
u8 ir_digital_gain;
u8 ir_ex_light_filter;
u32 ir_custom_register; // MSByte: Enable/Disable, Middle Byte: Edge smoothing, LSB: Color interpolation
u16 ir_buffer_update_time;
u8 ir_hand_analysis_mode;
u8 ir_hand_analysis_threshold;
u32 ir_denoise; // MSByte: Enable/Disable, Middle Byte: Edge smoothing, LSB: Color interpolation
u8 ir_flip;
};
#pragma pack(pop)
extern std::string get_sn(u32 offset, const u16 read_len);
extern s16 uint16_to_int16(u16 a);
extern void decode_stick_params(u16 *decoded_stick_params, u8 *encoded_stick_params);
extern void encode_stick_params(u8 *encoded_stick_params, u16 *decoded_stick_params);
extern std::string get_sn(u32 offset, const u16 read_len);
extern int get_spi_data(u32 offset, const u16 read_len, u8 *test_buf);
extern int write_spi_data(u32 offset, const u16 write_len, u8* test_buf);
extern int get_device_info(u8* test_buf);
@@ -98,18 +115,20 @@ extern int send_custom_command(u8* arg);
extern int device_connection();
extern int set_led_busy();
extern int button_test();
extern int ir_sensor(u8* buf_image, u8 ir_res_reg, u8 ir_res_no_of_packets, u16 ir_exposure, u8 ir_leds, u8 ir_digital_gain, u8 ir_ex_light_filter);
extern int ir_sensor(ir_image_config &ir_cfg);
extern int ir_sensor_config_live(ir_image_config &ir_cfg);
extern int nfc_tag_info();
extern int silence_input_report();
int handle_ok;
int handle_ok;
bool enable_button_test;
bool enable_IRVideoPhoto;
bool enable_IRAutoExposure;
bool enable_NFCScanning;
bool cancel_spi_dump;
bool check_connection_ok;
hid_device *handle;
hid_device *handle_l;
u8 timming_byte;
u8 ir_max_frag_no;
namespace CppWinFormJoy {
class images