Пример #1
0
static PyObject *Wiimote_get_balance_cal(Wiimote *self)
{
	struct balance_cal balance_cal;
	PyObject *PyBalCal;

	if (cwiid_get_balance_cal(self->wiimote, &balance_cal)) {
		PyErr_SetString(PyExc_RuntimeError,
		                "Error getting balance board calibration");
		return NULL;
	}

	if (!(PyBalCal = Py_BuildValue("([i,i,i],[i,i,i],[i,i,i],[i,i,i])",
	                               balance_cal.right_top[0],
	                               balance_cal.right_top[1],
	                               balance_cal.right_top[2],
	                               balance_cal.right_bottom[0],
	                               balance_cal.right_bottom[1],
	                               balance_cal.right_bottom[2],
	                               balance_cal.left_top[0],
	                               balance_cal.left_top[1],
	                               balance_cal.left_top[2],
	                               balance_cal.left_bottom[0],
	                               balance_cal.left_bottom[1],
	                               balance_cal.left_bottom[2]))) {
		return NULL;
	}

	return PyBalCal;
}
Пример #2
0
int main()
{
	cwiid_wiimote_t *wiimote = NULL;

	struct cwiid_state state;

	double wlt, wrt, wlb, wrb;
	double bal_x, bal_y;

	if ((wiimote = cwiid_open(BDADDR_ANY, 0)) == NULL) {
		fputs("Unable to connect\n", stderr);
		return EXIT_FAILURE;
	}
	fputs("connected\n", stdout);

	sleep(2);

	if (cwiid_set_led(wiimote, 1))
		fputs("Unable to set LED state\n", stderr);

	if (cwiid_get_balance_cal(wiimote, &balance_cal))
		fputs("unable to retrieve balance calibration\n", stderr);

	printf("bcal %d/%d/%d %d/%d/%d\n     %d/%d/%d %d/%d/%d\n",
		balance_cal.left_top[0],
		balance_cal.left_top[1],
		balance_cal.left_top[2],
		balance_cal.right_top[0],
		balance_cal.right_top[1],
		balance_cal.right_top[2],
		balance_cal.left_bottom[0],
		balance_cal.left_bottom[1],
		balance_cal.left_bottom[2],
		balance_cal.right_bottom[0],
		balance_cal.right_bottom[1],
		balance_cal.right_bottom[2]
	);

	if (!cwiid_get_state(wiimote, &state))
		printf("battery at %d%%\n",
			(int)(100.0 * state.battery / CWIID_BATTERY_MAX));

	if (cwiid_set_mesg_callback(wiimote, cwiid_callback))
		fputs("cannot set callback. buttons won't work.\n", stderr);
	
	if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC))
		fputs("cannot enable callback. buttons won't work.\n", stderr);

	if (cwiid_set_rpt_mode(wiimote,
			CWIID_RPT_ACC | CWIID_RPT_STATUS | CWIID_RPT_EXT))
		fputs("cannot set report mode. buttons won't work.\n", stderr);

	while (1) {
		cwiid_get_state(wiimote, &state);

		wlt = weight(state.ext.balance.left_top, balance_cal.left_top);
		wrt = weight(state.ext.balance.right_top, balance_cal.right_top);
		wlb = weight(state.ext.balance.left_bottom, balance_cal.left_bottom);
		wrb = weight(state.ext.balance.right_bottom, balance_cal.right_bottom);

		bal_x = (wrt + wrb) / (wlt + wlb);
		if (bal_x > 1)
			bal_x = ((wlt + wlb) / (wrt + wrb) * (-1.0)) + 1.0;
		else
			bal_x -= 1;

		bal_y = (wlt + wrt) / (wlb + wrb);
		if (bal_y > 1)
			bal_y = ((wlb + wrb) / (wlt + wrt) * (-1.0)) + 1.0;
		else
			bal_y -= 1;

		printf("%6.1f kg  %6.1f kg    %04x %04x     (%5.1f kg)\n%6.1f kg  %6.1f kg    %04x %04x\n\n",
			wlt, wrt,
			state.ext.balance.left_top, state.ext.balance.right_top,
			wlt + wrt + wlb + wrb,
			wlb, wrb,
			state.ext.balance.left_bottom, state.ext.balance.right_bottom
		);
		printf("balance %6f %6f\n\n", bal_x, bal_y);
		sleep(1);
	}

	return EXIT_SUCCESS;
}
Пример #3
0
static void wiimote_cwiid_message(t_wiimote *x, union cwiid_mesg*mesg) {
  if(NULL==x){
    return;
  }
  if(NULL==mesg) {
    return;
  }
   switch (mesg->type) {
   case CWIID_MESG_STATUS:
      wiimote_cwiid_battery(x, mesg->status_mesg.battery);
      switch (mesg->status_mesg.ext_type) {
      case CWIID_EXT_NONE:
         verbose(1, "No extension attached");
         break;
#ifdef CWIID_RPT_NUNCHUK
      case CWIID_EXT_NUNCHUK:
         post("Nunchuk extension attached");
         if(cwiid_get_acc_cal(x->wiimote, CWIID_EXT_NUNCHUK, &x->nc_acc_cal)) {
            post("Unable to retrieve nunchuk calibration");
         } else {
            post("Retrieved nunchuk calibration: zero=(%02d,%02d,%02d) one=(%02d,%02d,%02d)",
                   x->nc_acc_cal.zero[CWIID_X],
                   x->nc_acc_cal.zero[CWIID_Y],
                   x->nc_acc_cal.zero[CWIID_Z],
                   x->nc_acc_cal.one [CWIID_X],
                   x->nc_acc_cal.one [CWIID_Y],
                   x->nc_acc_cal.one [CWIID_Z]);
         }
         break;
#endif
#ifdef CWIID_RPT_CLASSIC
      case CWIID_EXT_CLASSIC:
         post("Classic controller attached. There is no real support for this yet.");
         break;
#endif
#ifdef CWIID_RPT_BALANCE
      case CWIID_EXT_BALANCE:
         if(cwiid_get_balance_cal(x->wiimote, &x->balance_cal)) {
            post("Unable to retrieve balance calibration");
      }
         break;
#endif
#ifdef CWIID_RPT_MOTIONPLUS
      case CWIID_EXT_MOTIONPLUS:
         post("MotionPlus controller attached.");
         /* no calibration needed for MotionPlus */
         break;
#endif
      case CWIID_EXT_UNKNOWN:
         post("Unknown extension attached");
         break;
      default:
	post("ext mesg %d/%d unknown", mesg->type, mesg->status_mesg.ext_type);
         break;
      }
      break;
   case CWIID_MESG_BTN:
      wiimote_cwiid_btn(x, &mesg->btn_mesg);
      break;
   case CWIID_MESG_ACC:
      wiimote_cwiid_acc(x, &mesg->acc_mesg);
      break;
   case CWIID_MESG_IR:
      wiimote_cwiid_ir(x, &mesg->ir_mesg);
      break;
#ifdef CWIID_RPT_NUNCHUK
   case CWIID_MESG_NUNCHUK:
      wiimote_cwiid_nunchuk(x, &mesg->nunchuk_mesg);
      break;
#endif
#ifdef CWIID_RPT_CLASSIC
   case CWIID_MESG_CLASSIC:
      wiimote_cwiid_classic(x, &mesg->classic_mesg);
      // todo
      break;
#endif
#ifdef CWIID_RPT_MOTIONPLUS
   case CWIID_MESG_MOTIONPLUS:
      wiimote_cwiid_motionplus(x, &mesg->motionplus_mesg);
      break;
#endif
#ifdef CWIID_RPT_BALANCE
   case CWIID_MESG_BALANCE:
      wiimote_cwiid_balance(x, &mesg->balance_mesg);
      break;
#endif
   case CWIID_MESG_ERROR:
     wiimote_cwiid_error(x, &mesg->error_mesg);
   default:
      post("mesg %d unknown", (mesg->type));
      break;
   }
}