Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull Request for extending Network API #618

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 54 additions & 14 deletions core/calibration.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,24 @@ static void cal_display()
}
}

void cal_save()
{
current_cal = NONE;
if (cfgw_modify_file(gimx_params.config_file)) {
gwarn(_("error writting the config file %s\n"), gimx_params.config_file);
}
ginfo(_("calibration done\n"));
}

void cal_update_display()
{
if (gimx_params.curses) {
display_calibration();
} else {
cal_display();
}
}

/*
* Use keys to calibrate the mouse.
*/
Expand Down Expand Up @@ -319,12 +337,7 @@ void cal_key(int sym, int down)
}
else
{
current_cal = NONE;
if (cfgw_modify_file(gimx_params.config_file))
{
gwarn(_("error writting the config file %s\n"), gimx_params.config_file);
}
ginfo(_("calibration done\n"));
cal_save();
}
}
else if(current_cal != NONE)
Expand Down Expand Up @@ -442,20 +455,47 @@ void cal_key(int sym, int down)

if(prev != current_cal)
{
if(gimx_params.curses)
{
display_calibration();
}
else
{
cal_display();
}
cal_update_display();
}
}

#define DEADZONE_MAX(AXIS) \
(controller_get_mean_unsigned(ctype, AXIS) / controller_get_axis_scale(ctype, AXIS) / 2)

void cal_setSensibility(double s)
Lucashsmello marked this conversation as resolved.
Show resolved Hide resolved
{
s_mouse_cal* mcal = cal_get_mouse(current_mouse, current_conf);
double ratio = *mcal->my / *mcal->mx;
*mcal->mx = s;
*mcal->my = *mcal->mx * ratio;
}

void cal_setDeadzoneX(int dx)
{
e_controller_type ctype = adapter_get(cal_get_controller(current_mouse))->ctype;
s_mouse_control* mc = cfg_get_mouse_control(current_mouse);
s_mouse_cal* mcal = cal_get_mouse(current_mouse, current_conf);
if (mcal->dzx && dx < DEADZONE_MAX(rel_axis_rstick_x)) {
*mcal->dzx = dx;
mc->merge[mc->index].x = 1;
mc->merge[mc->index].y = 0;
mc->change = 1;
}

}
void cal_setDeadzoneY(int dy)
{
e_controller_type ctype = adapter_get(cal_get_controller(current_mouse))->ctype;
s_mouse_control* mc = cfg_get_mouse_control(current_mouse);
s_mouse_cal* mcal = cal_get_mouse(current_mouse, current_conf);
if (mcal->dzy && dy < DEADZONE_MAX(rel_axis_rstick_y)) {
*mcal->dzy = dy;
mc->merge[mc->index].x = 0;
mc->merge[mc->index].y = 1;
mc->change = 1;
}
}

/*
* Use the mouse wheel to calibrate the mouse.
*/
Expand Down
68 changes: 68 additions & 0 deletions core/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <gimxpoll/include/gpoll.h>

#include <haptic/haptic_core.h>
#include "calibration.h"
#include <float.h>

#define BAUDRATE 500000 //bps
#define SERIAL_TIMEOUT 1000 //millisecond
Expand Down Expand Up @@ -136,6 +138,48 @@ static void adapter_dump_state(int adapter)
gstatus("\n");
}

static float float_swap(float x)
{
union V {
float f;
uint32_t i;
};
union V val;
val.f = x;
val.i = htonl(val.i);
return val.f;
}

static void handlePacketConfig(const s_network_packet_config* buf)
{
float s = float_swap(buf->sensibility);
if (s < FLT_MAX && s >= 0) {
printf("setting sensibility=%f\n", s);
Lucashsmello marked this conversation as resolved.
Show resolved Hide resolved
cal_setSensibility(s);
}
int16_t dx = ntohs(buf->dead_zone_X);
if (dx < INT16_MAX) {
Lucashsmello marked this conversation as resolved.
Show resolved Hide resolved
printf("setting dx=%d\n", dx);
cal_setDeadzoneX(dx);
}
int16_t dy = ntohs(buf->dead_zone_Y);
if (dy < INT16_MAX) {
Lucashsmello marked this conversation as resolved.
Show resolved Hide resolved
printf("setting dy=%d\n", dy);
cal_setDeadzoneY(dy);
}
}

static s_network_packet_config handlePacketGetConfig()
{
s_network_packet_config config_pkg;
const s_mouse_cal* mcal = cal_get_mouse(current_mouse, current_conf);
config_pkg.packet_type = E_NETWORK_PACKET_GETCONFIG;
config_pkg.sensibility = float_swap(*mcal->mx);
config_pkg.dead_zone_X = htons(*mcal->dzx);
config_pkg.dead_zone_Y = htons(*mcal->dzy);
return config_pkg;
}

/*
* Read a packet from a remote GIMX client.
* The packet can be:
Expand Down Expand Up @@ -203,6 +247,30 @@ static int network_read_callback(void * user)
adapters[adapter].send_command = 1;
}
break;
case E_NETWORK_PACKET_EXIT:
set_done(1);
break;
case E_NETWORK_PACKET_SETCONFIG:

handlePacketConfig((s_network_packet_config*) buf);
Lucashsmello marked this conversation as resolved.
Show resolved Hide resolved
break;
case E_NETWORK_PACKET_GETCONFIG:
{
s_network_packet_config config_pkg = handlePacketGetConfig();
if (udp_sendto(adapters[adapter].src_fd, (void *) &config_pkg, sizeof(config_pkg), (struct sockaddr*) &sa, salen) < 0) {
gwarn("%s: can't send configuration values\n", __func__);
return 0;
}
break;
}
case E_NETWORK_PACKET_SAVECALIBRATION:
{
cal_save();
cal_update_display();
break;
}
default:
gwarn("%s: packet_type not recognized",__func__);
Lucashsmello marked this conversation as resolved.
Show resolved Hide resolved
}
// require a report to be sent immediately, except for a Sixaxis controller working over bluetooth
if(adapters[adapter].ctype == C_TYPE_SIXAXIS && adapters[adapter].atype == E_ADAPTER_TYPE_BLUETOOTH)
Expand Down
5 changes: 5 additions & 0 deletions core/include/calibration.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@ void cal_init();
int cal_get_controller(int);
void cal_set_controller(int, int);
void calibration_test();
void cal_setSensibility(double);
void cal_setDeadzoneX(int);
void cal_setDeadzoneY(int);
void cal_save();
void cal_update_display();

#endif /* CALIBRATION_H_ */
2 changes: 1 addition & 1 deletion shared/gimx-network-protocol