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 all 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
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
url = https://github.com/matlo/gimxcommon.git
[submodule "shared/gimx-network-protocol"]
path = shared/gimx-network-protocol
url = https://github.com/matlo/gimx-network-protocol.git
url = https://github.com/Lucashsmello/gimx-network-protocol.git
[submodule "shared/gimxlog"]
path = shared/gimxlog
url = https://github.com/matlo/gimxlog.git
Expand Down
97 changes: 83 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,76 @@ 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_set_sensibility(double s)
{
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_set_deadzone_x(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_set_deadzone_y(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;
}
}

void cal_set_exponent_x(double e)
{
s_mouse_cal* mcal = cal_get_mouse(current_mouse, current_conf);
if (mcal->ex)
{
*mcal->ex = e;
}
}

void cal_set_exponent_y(double e)
{
s_mouse_cal* mcal = cal_get_mouse(current_mouse, current_conf);
if (mcal->ey)
{
*mcal->ey = e;
}
}

/**
* This function does *only* changes the sensibility on the vertical axis so that y/x = r.
*/
void cal_set_yxratio(double r){
s_mouse_cal* mcal = cal_get_mouse(current_mouse, current_conf);
if (mcal->mx && mcal->my)
{
*mcal->my = *mcal->mx * r;
}
}

/*
* Use the mouse wheel to calibrate the mouse.
*/
Expand Down
8 changes: 8 additions & 0 deletions core/calibration.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,13 @@ void cal_init();
int cal_get_controller(int);
void cal_set_controller(int, int);
void calibration_test();
void cal_set_sensibility(double);
void cal_set_deadzone_x(int);
void cal_set_deadzone_y(int);
void cal_set_exponent_x(double);
void cal_set_exponent_y(double);
void cal_set_yxratio(double);
void cal_save();
void cal_update_display();

#endif /* CALIBRATION_H_ */
87 changes: 87 additions & 0 deletions core/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <gimxlog/include/glog.h>

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

#define DEFAULT_BAUDRATE 500000 //bps
static const int baudrates[] = { 2000000, 1000000, DEFAULT_BAUDRATE }; //bps
Expand Down Expand Up @@ -310,6 +312,61 @@ static int proxy_dst_read_callback(void * user, const void * buf, int status, st
return adapter_process_packet(i, (s_packet*) buf);
}

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

static void handle_packet_config(const s_network_packet_config* buf)
{
float ex, ey, yx_ratio;
float s = float_swap(buf->sensibility);
if (s >= 0) { //Does negative sensibility makes sense?
cal_set_sensibility(s);
}
int16_t dx = gudp_ntohs(buf->dead_zone_x);
if (dx < INT16_MAX) {
Lucashsmello marked this conversation as resolved.
Show resolved Hide resolved
cal_set_deadzone_x(dx);
}
int16_t dy = gudp_ntohs(buf->dead_zone_y);
if (dy < INT16_MAX) {
Lucashsmello marked this conversation as resolved.
Show resolved Hide resolved
cal_set_deadzone_y(dy);
}
ex = float_swap(buf->exponent_x);
if (ex >= 0) {
cal_set_exponent_x(ex);
}
ey = float_swap(buf->exponent_y);
if (ey >= 0) {
cal_set_exponent_y(ey);
}
yx_ratio = float_swap(buf->yx_ratio);
if (yx_ratio >= 0) {
cal_set_yxratio(yx_ratio);
}
}

static s_network_packet_config handle_packet_get_config()
{
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 = gudp_htons(*mcal->dzx);
config_pkg.dead_zone_y = gudp_htons(*mcal->dzy);
config_pkg.yx_ratio = float_swap((*mcal->my) / (*mcal->mx));
config_pkg.exponent_x = float_swap(*mcal->ex);
config_pkg.exponent_y = float_swap(*mcal->ey);
return config_pkg;
}

/*
* Read a packet from a remote GIMX client.
* The packet can be:
Expand Down Expand Up @@ -373,6 +430,36 @@ static int network_read_callback(void * user, const void * buf, int status, stru
adapters[adapter].send_command = 1;
}
break;
case E_NETWORK_PACKET_EXIT:
set_done(1);
break;
case E_NETWORK_PACKET_SETCONFIG:
if ((unsigned int) status != sizeof(s_network_packet_config))
{
gwarn("%s: wrong packet size: %u %zu\n", __func__, status, sizeof(s_network_packet_config));
return 0;
}
handle_packet_config((s_network_packet_config*) buf);
cal_update_display();
break;
case E_NETWORK_PACKET_GETCONFIG:
{
s_network_packet_config config_pkg = handle_packet_get_config();
if (gudp_send(adapters[adapter].src_socket, (void *) &config_pkg, sizeof(config_pkg), address) < 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
break;
}
return (gimx_params.clock_source == CLOCK_INPUT);
}
Expand Down
2 changes: 1 addition & 1 deletion shared/gimx-network-protocol
2 changes: 1 addition & 1 deletion shared/gimxudp
Submodule gimxudp updated 2 files
+19 −1 include/gudp.h
+9 −0 src/posix/gudp.c