1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#include "wmplugin.h"
#include <math.h>
cwiid_wiimote_t *wiimote;
static unsigned char info_init = 0;
static struct wmplugin_info info;
static struct wmplugin_data data;
wmplugin_info_t wmplugin_info;
wmplugin_init_t wmplugin_init;
wmplugin_exec_t wmplugin_exec;
static int Led1 = 0;
static int Led2 = 0;
static int Led3 = 0;
static int Led4 = 0;
static int Battery = 0;
static uint8_t Button = CWIID_BTN_A + CWIID_BTN_B;
static void show_battery();
struct wmplugin_info *wmplugin_info()
{
if (!info_init) {
info.button_count = 0;
info.axis_count = 0;
info.param_count = 6;
info.param_info[0].name = "Led1";
info.param_info[0].type = WMPLUGIN_PARAM_INT;
info.param_info[0].ptr = &Led1;
info.param_info[1].name = "Led2";
info.param_info[1].type = WMPLUGIN_PARAM_INT;
info.param_info[1].ptr = &Led2;
info.param_info[2].name = "Led3";
info.param_info[2].type = WMPLUGIN_PARAM_INT;
info.param_info[2].ptr = &Led3;
info.param_info[3].name = "Led4";
info.param_info[3].type = WMPLUGIN_PARAM_INT;
info.param_info[3].ptr = &Led4;
info.param_info[4].name = "Battery";
info.param_info[4].type = WMPLUGIN_PARAM_INT;
info.param_info[4].ptr = &Battery;
info.param_info[5].name = "Button";
info.param_info[5].type = WMPLUGIN_PARAM_INT;
info.param_info[5].ptr = &Button;
info_init = 1;
}
return &info;
}
int wmplugin_init(int id, cwiid_wiimote_t *arg_wiimote)
{
wiimote = arg_wiimote;
uint8_t led_state = (Led1 ? CWIID_LED1_ON : 0)
| (Led2 ? CWIID_LED2_ON : 0)
| (Led3 ? CWIID_LED3_ON : 0)
| (Led4 ? CWIID_LED4_ON : 0);
cwiid_command(wiimote, CWIID_CMD_LED, led_state);
if (wmplugin_set_rpt_mode(id, CWIID_RPT_BTN)) {
return -1;
}
return 0;
}
struct wmplugin_data *wmplugin_exec(int mesg_count, union cwiid_mesg mesg[])
{
int i;
uint8_t button;
struct cwiid_btn_message *btn_mesg;
uint8_t led_state = (Led1 ? CWIID_LED1_ON : 0)
| (Led2 ? CWIID_LED2_ON : 0)
| (Led3 ? CWIID_LED3_ON : 0)
| (Led4 ? CWIID_LED4_ON : 0);
if(Battery != 0) {
btn_mesg = NULL;
for (i=0; i < mesg_count; i++) {
if (mesg[i].type == CWIID_MESG_BTN) {
btn_mesg = &mesg[i].btn_mesg;
button = mesg[i].btn_mesg.buttons;
}
}
if (!btn_mesg) {
return NULL;
}
if(button == Button) {
show_battery();
}
else {
cwiid_command(wiimote, CWIID_CMD_LED, led_state);
}
}
return &data;
}
static void show_battery()
{
struct cwiid_state state;
float battery_percent;
int battery_led;
cwiid_get_state(wiimote, &state);
// calculate battery as a percent, then decide how many leds to light up
battery_percent = (100*state.battery / CWIID_BATTERY_MAX);
battery_led = ceil(battery_percent / 25);
switch(battery_led) {
case 1:
cwiid_set_led(wiimote, CWIID_LED1_ON);
break;
case 2:
cwiid_set_led(wiimote, CWIID_LED1_ON | CWIID_LED2_ON );
break;
case 3:
cwiid_set_led(wiimote, CWIID_LED1_ON | CWIID_LED2_ON | CWIID_LED3_ON );
break;
case 4:
cwiid_set_led(wiimote, CWIID_LED1_ON | CWIID_LED2_ON | CWIID_LED3_ON | CWIID_LED4_ON );
break;
default:
break;
}
}
|