PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/GiiMote/Nunchuck.h

https://code.google.com/p/giimote/
C Header | 585 lines | 358 code | 22 blank | 205 comment | 11 complexity | 661d6ebedaf02b9c2cbc36978b11de0c MD5 | raw file
Possible License(s): AGPL-3.0, CC-BY-SA-3.0, GPL-3.0, LGPL-3.0
  1. // Nunchuck.h - Contains functions that deal with the Nunchuck.
  2. // Copyright 2007 Sam Whited
  3. //
  4. // This file is part of GiiMote.
  5. //
  6. // GiiMote is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Lesser General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GiiMote is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Lesser General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Lesser General Public License
  17. // along with GiiMote. If not, see <http://www.gnu.org/licenses/>.
  18. // Normalized Data
  19. /// <summary>Normalized accelerometer data</summary>
  20. /// <remarks>Domain: [0,?)</remarks>
  21. /// <returns>Normalized acceleration</returns>
  22. double wm_nunchuck_get_accel_x()
  23. {
  24. return ( wm_calc_accel(GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.AccelState.Values.X, 3) );
  25. }
  26. /// <summary>Normalized accelerometer data</summary>
  27. /// <remarks>Domain: [0,?)</remarks>
  28. /// <returns>Normalized acceleration</returns>
  29. double wm_nunchuck_get_accel_y()
  30. {
  31. return ( wm_calc_accel(GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.AccelState.Values.Y, 4) );
  32. }
  33. /// <summary>Normalized accelerometer data</summary>
  34. /// <remarks>Domain: [0,?)</remarks>
  35. /// <returns>Normalized acceleration</returns>
  36. double wm_nunchuck_get_accel_z()
  37. {
  38. return ( wm_calc_accel(GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.AccelState.Values.Z, 5) );
  39. }
  40. // Raw Data
  41. /// <summary>Raw accelerometer data</summary>
  42. /// <remarks>Domain: [0,255]</remarks>
  43. /// <returns>Raw acceleration</returns>
  44. double wm_nunchuck_get_accel_rawx()
  45. {
  46. return ( (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.AccelState.RawValues.X );
  47. }
  48. /// <summary>Raw accelerometer data</summary>
  49. /// <remarks>Domain: [0,255]</remarks>
  50. /// <returns>Raw acceleration</returns>
  51. double wm_nunchuck_get_accel_rawy()
  52. {
  53. return ( (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.AccelState.RawValues.Y );
  54. }
  55. /// <summary>Raw accelerometer data</summary>
  56. /// <remarks>Domain: [0,255]</remarks>
  57. /// <returns>Raw acceleration</returns>
  58. double wm_nunchuck_get_accel_rawz()
  59. {
  60. return ( (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.AccelState.RawValues.Z );
  61. }
  62. /// <summary>Sets the dead-zone value</summary>
  63. /// <remarks>Domain: [0,Infinity]</remarks>
  64. /// <param name="val">The dead-zone value</param>
  65. /// <returns>1</returns>
  66. double wm_nunchuck_set_accel_dead_zone_x(double val)
  67. {
  68. GiiMote::gm->accel_dead_zone[GiiMote::gm->wmIndex, 3] = in_domain(val, 0, -1);
  69. return ( 1 );
  70. }
  71. /// <summary>Sets the dead-zone value</summary>
  72. /// <remarks>Domain: [0,Infinity]</remarks>
  73. /// <param name="val">The dead-zone value</param>
  74. /// <returns>1</returns>
  75. double wm_nunchuck_set_accel_dead_zone_y(double val)
  76. {
  77. GiiMote::gm->accel_dead_zone[GiiMote::gm->wmIndex, 4] = in_domain(val, 0, -1);
  78. return ( 1 );
  79. }
  80. /// <summary>Sets the dead-zone value</summary>
  81. /// <remarks>Domain: [0,Infinity]</remarks>
  82. /// <param name="val">The dead-zone value</param>
  83. /// <returns>1</returns>
  84. double wm_nunchuck_set_accel_dead_zone_z(double val)
  85. {
  86. GiiMote::gm->accel_dead_zone[GiiMote::gm->wmIndex, 5] = in_domain(val, 0, -1);
  87. return ( 1 );
  88. }
  89. /// <summary>Gets the dead-zone value</summary>
  90. /// <returns>X-axis dead zone</returns>
  91. double wm_nunchuck_get_accel_dead_zone_x()
  92. {
  93. return ( GiiMote::gm->accel_dead_zone[GiiMote::gm->wmIndex, 3] );
  94. }
  95. /// <summary>Gets the dead-zone value</summary>
  96. /// <returns>Y-axis dead zone</returns>
  97. double wm_nunchuck_get_accel_dead_zone_y()
  98. {
  99. return ( GiiMote::gm->accel_dead_zone[GiiMote::gm->wmIndex, 4] );
  100. }
  101. /// <summary>Gets the dead-zone value</summary>
  102. /// <returns>Z-axis dead zone</returns>
  103. double wm_nunchuck_get_accel_dead_zone_z()
  104. {
  105. return ( GiiMote::gm->accel_dead_zone[GiiMote::gm->wmIndex, 5] );
  106. }
  107. /// <summary>
  108. /// Checks the state of a button
  109. /// </summary>
  110. /// <param name="key_code">
  111. /// The button to check
  112. /// <list type="bullet">
  113. /// <listheader>
  114. /// <term>Key Code</term>
  115. /// <description>Button</description>
  116. /// </listheader>
  117. /// <item>
  118. /// <term>btnC</term>
  119. /// <description>C Button</description>
  120. /// </item>
  121. /// <item>
  122. /// <term>btnZ</term>
  123. /// <description>Z Button</description>
  124. /// </item>
  125. /// </list>
  126. /// </param>
  127. /// <returns>Button pressed</returns>
  128. double wm_nunchuck_check_button(double key_code)
  129. {
  130. bool is_pressed = 0;
  131. switch ((int)key_code)
  132. {
  133. case btnC:
  134. is_pressed = GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.C;
  135. break;
  136. case btnZ:
  137. is_pressed = GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.Z;
  138. break;
  139. default:
  140. is_pressed = false;
  141. break;
  142. }
  143. return ((double)is_pressed);
  144. }
  145. // Normalized Functions
  146. /// <summary>Normalized joystick position</summary>
  147. /// <remarks>Domain: [-0.5,0.5]</remarks>
  148. /// <returns>The normalized X-position of the joystick</returns>
  149. double wm_nunchuck_xpos()
  150. {
  151. double xx;
  152. try
  153. {
  154. xx = (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.Joystick.X;
  155. }
  156. catch(...)
  157. {
  158. return ( -1 );
  159. }
  160. if ( System::Math::Abs(xx) < (GiiMote::gm->joystick_dead_zone[GiiMote::gm->wmIndex]) )
  161. {
  162. xx = 0;
  163. }
  164. return ( xx );
  165. }
  166. /// <summary>Normalized joystick position</summary>
  167. /// <remarks>Domain: [-0.5,0.5]</remarks>
  168. /// <returns>The normalized Y-position of the joystick</returns>
  169. double wm_nunchuck_ypos()
  170. {
  171. double yy;
  172. try
  173. {
  174. yy = (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.Joystick.Y;
  175. }
  176. catch(...)
  177. {
  178. return ( -1 );
  179. }
  180. if ( System::Math::Abs(yy) < (GiiMote::gm->joystick_dead_zone[GiiMote::gm->wmIndex]) )
  181. {
  182. yy = 0;
  183. }
  184. return ( yy );
  185. }
  186. /// <summary>Joystick direction</summary>
  187. /// <returns>The direction of the joystick in degrees</returns>
  188. double wm_nunchuck_direction()
  189. {
  190. double xx = wm_nunchuck_xpos();
  191. double yy = wm_nunchuck_ypos();
  192. if (xx == -1 || yy == -1)
  193. {
  194. return ( -1 );
  195. }
  196. else
  197. {
  198. return ( joystick_direction(xx,yy) );
  199. }
  200. }
  201. /// <summary>Joystick pressure</summary>
  202. /// <returns>The pressure on the joystick</returns>
  203. double wm_nunchuck_pressure()
  204. {
  205. double xx = wm_nunchuck_xpos();
  206. double yy = wm_nunchuck_ypos();
  207. if (xx == -1 || yy == -1)
  208. {
  209. return ( -1 );
  210. }
  211. else
  212. {
  213. return ( joystick_pressure(xx,yy) );
  214. }
  215. }
  216. // Raw Functions
  217. /// <summary>Raw joystick X-position</summary>
  218. /// <remarks>Domain: [0,255]</remarks>
  219. /// <returns>Raw joystick X-position</returns>
  220. double wm_nunchuck_rawx()
  221. {
  222. double rawX;
  223. try
  224. {
  225. rawX = (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.RawJoystick.X;
  226. }
  227. catch(...)
  228. {
  229. return ( -1 );
  230. }
  231. return ( rawX );
  232. }
  233. /// <summary>Raw joystick Y-position</summary>
  234. /// <remarks>Domain: [0,255]</remarks>
  235. /// <returns>Raw joystick Y-position</returns>
  236. double wm_nunchuck_rawy()
  237. {
  238. double rawY;
  239. try
  240. {
  241. rawY = (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.RawJoystick.Y;
  242. }
  243. catch(...)
  244. {
  245. return ( -1 );
  246. }
  247. return ( rawY );
  248. }
  249. // Calibration Reading Functions
  250. /// <summary>
  251. /// Gets the zero point of the accelerometer
  252. /// </summary>
  253. /// <returns>Normalized calibration data</returns>
  254. double wm_nunchuck_get_calibration_x0()
  255. {
  256. return ( (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.AccelCalibration.X0 );
  257. }
  258. /// <summary>
  259. /// Gets the gravity at rest of the accelerometer
  260. /// </summary>
  261. /// <returns>Normalized calibration data</returns>
  262. double wm_nunchuck_get_calibration_xg()
  263. {
  264. return ( (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.AccelCalibration.XG );
  265. }
  266. // Calibration Reading Functions
  267. /// <summary>
  268. /// Gets the zero point of the accelerometer
  269. /// </summary>
  270. /// <returns>Normalized calibration data</returns>
  271. double wm_nunchuck_get_calibration_y0()
  272. {
  273. return ( (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.AccelCalibration.Y0 );
  274. }
  275. /// <summary>
  276. /// Gets the gravity at rest of the accelerometer
  277. /// </summary>
  278. /// <returns>Normalized calibration data</returns>
  279. double wm_nunchuck_get_calibration_yg()
  280. {
  281. return ( (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.AccelCalibration.YG );
  282. }
  283. /// <summary>
  284. /// Gets the zero point of the accelerometer
  285. /// </summary>
  286. /// <returns>Normalized calibration data</returns>
  287. double wm_nunchuck_get_calibration_z0()
  288. {
  289. return ( (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.AccelCalibration.Z0 );
  290. }
  291. /// <summary>
  292. /// Gets the gravity at rest of the accelerometer
  293. /// </summary>
  294. /// <returns>Normalized calibration data</returns>
  295. double wm_nunchuck_get_calibration_zg()
  296. {
  297. return ( (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.AccelCalibration.ZG );
  298. }
  299. /// <summary>
  300. /// Gets the joystick X-axis calibration
  301. /// </summary>
  302. /// <returns>Normalized calibration data</returns>
  303. double wm_nunchuck_get_calibration_maxx()
  304. {
  305. return ( (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.MaxX );
  306. }
  307. /// <summary>
  308. /// Gets the joystick Y-axis calibration
  309. /// </summary>
  310. /// <returns>Normalized calibration data</returns>
  311. double wm_nunchuck_get_calibration_maxy()
  312. {
  313. return ( (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.MaxY );
  314. }
  315. /// <summary>
  316. /// Gets the joystick X-axis calibration
  317. /// </summary>
  318. /// <returns>Normalized calibration data</returns>
  319. double wm_nunchuck_get_calibration_midx()
  320. {
  321. return ( (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.MidX );
  322. }
  323. /// <summary>
  324. /// Gets the joystick Y-axis calibration
  325. /// </summary>
  326. /// <returns>Normalized calibration data</returns>
  327. double wm_nunchuck_get_calibration_midy()
  328. {
  329. return ( (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.MidY );
  330. }
  331. /// <summary>
  332. /// Gets the joystick X-axis calibration
  333. /// </summary>
  334. /// <returns>Normalized calibration data</returns>
  335. double wm_nunchuck_get_calibration_minx()
  336. {
  337. return ( (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.MinX );
  338. }
  339. /// <summary>
  340. /// Gets the joystick Y-axis calibration
  341. /// </summary>
  342. /// <returns>Normalized calibration data</returns>
  343. double wm_nunchuck_get_calibration_miny()
  344. {
  345. return ( (double)GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.MinY );
  346. }
  347. // Calibration Writing Functions
  348. /// <summary>
  349. /// Sets the zero point of the accelerometer
  350. /// </summary>
  351. /// <param name="val">X0 Value</param>
  352. /// <returns>Success</returns>
  353. double wm_nunchuck_set_calibration_x0(double val)
  354. {
  355. unsigned char calData = (unsigned char)val;
  356. try
  357. {
  358. GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.AccelCalibration.X0 = calData;
  359. }
  360. catch(...)
  361. {
  362. return ( 0 );
  363. }
  364. return ( 1 );
  365. }
  366. /// <summary>
  367. /// Sets the gravity at rest of the accelerometer
  368. /// </summary>
  369. /// <param name="val">XG Value</param>
  370. /// <returns>Success</returns>
  371. double wm_nunchuck_set_calibration_xg(double val)
  372. {
  373. unsigned char calData = (unsigned char)val;
  374. try
  375. {
  376. GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.AccelCalibration.XG = calData;
  377. }
  378. catch(...)
  379. {
  380. return ( 0 );
  381. }
  382. return ( 1 );
  383. }
  384. /// <summary>
  385. /// Sets the zero point of the accelerometer
  386. /// </summary>
  387. /// <param name="val">Y0 Value</param>
  388. /// <returns>Success</returns>
  389. double wm_nunchuck_set_calibration_y0(double val)
  390. {
  391. unsigned char calData = (unsigned char)val;
  392. try
  393. {
  394. GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.AccelCalibration.Y0 = calData;
  395. }
  396. catch(...)
  397. {
  398. return ( 0 );
  399. }
  400. return ( 1 );
  401. }
  402. /// <summary>
  403. /// Sets the gravity at rest of the accelerometer
  404. /// </summary>
  405. /// <param name="val">YG Value</param>
  406. /// <returns>Success</returns>
  407. double wm_nunchuck_set_calibration_yg(double val)
  408. {
  409. unsigned char calData = (unsigned char)val;
  410. try
  411. {
  412. GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.AccelCalibration.YG = calData;
  413. }
  414. catch(...)
  415. {
  416. return ( 0 );
  417. }
  418. return ( 1 );
  419. }
  420. /// <summary>
  421. /// Sets the zero point of the accelerometer
  422. /// </summary>
  423. /// <param name="val">Z0 Value</param>
  424. /// <returns>Success</returns>
  425. double wm_nunchuck_set_calibration_z0(double val)
  426. {
  427. unsigned char calData = (unsigned char)val;
  428. try
  429. {
  430. GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.AccelCalibration.Z0 = calData;
  431. }
  432. catch(...)
  433. {
  434. return ( 0 );
  435. }
  436. return ( 1 );
  437. }
  438. /// <summary>
  439. /// Sets the gravity at rest of the accelerometer
  440. /// </summary>
  441. /// <param name="val">ZG Value</param>
  442. /// <returns>Success</returns>
  443. double wm_nunchuck_set_calibration_zg(double val)
  444. {
  445. unsigned char calData = (unsigned char)val;
  446. try
  447. {
  448. GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.AccelCalibration.ZG = calData;
  449. }
  450. catch(...)
  451. {
  452. return ( 0 );
  453. }
  454. return ( 1 );
  455. }
  456. /// <summary>
  457. /// Sets the joystick X-axis calibration
  458. /// </summary>
  459. /// <param name="val">MaxX Value</param>
  460. /// <returns>Success</returns>
  461. double wm_nunchuck_set_calibration_maxx(double val)
  462. {
  463. unsigned char calData = (unsigned char)val;
  464. try
  465. {
  466. GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.MaxX = calData;
  467. }
  468. catch(...)
  469. {
  470. return ( 0 );
  471. }
  472. return ( 1 );
  473. }
  474. /// <summary>
  475. /// Sets the joystick Y-axis calibration
  476. /// </summary>
  477. /// <param name="val">MaxY Value</param>
  478. /// <returns>Success</returns>
  479. double wm_nunchuck_set_calibration_maxy(double val)
  480. {
  481. unsigned char calData = (unsigned char)val;
  482. try
  483. {
  484. GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.MaxY = calData;
  485. }
  486. catch(...)
  487. {
  488. return ( 0 );
  489. }
  490. return ( 1 );
  491. }
  492. /// <summary>
  493. /// Sets the joystick X-axis calibration
  494. /// </summary>
  495. /// <param name="val">MidX Value</param>
  496. /// <returns>Success</returns>
  497. double wm_nunchuck_set_calibration_midx(double val)
  498. {
  499. unsigned char calData = (unsigned char)val;
  500. try
  501. {
  502. GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.MidX = calData;
  503. }
  504. catch(...)
  505. {
  506. return ( 0 );
  507. }
  508. return ( 1 );
  509. }
  510. /// <summary>
  511. /// Sets the joystick Y-axis calibration
  512. /// </summary>
  513. /// <param name="val">MidY Value</param>
  514. /// <returns>Success</returns>
  515. double wm_nunchuck_set_calibration_midy(double val)
  516. {
  517. unsigned char calData = (unsigned char)val;
  518. try
  519. {
  520. GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.MidY = calData;
  521. }
  522. catch(...)
  523. {
  524. return ( 0 );
  525. }
  526. return ( 1 );
  527. }
  528. /// <summary>
  529. /// Sets the joystick X-axis calibration
  530. /// </summary>
  531. /// <param name="val">MinX Value</param>
  532. /// <returns>Success</returns>
  533. double wm_nunchuck_set_calibration_minx(double val)
  534. {
  535. unsigned char calData = (unsigned char)val;
  536. try
  537. {
  538. GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.MinX = calData;
  539. }
  540. catch(...)
  541. {
  542. return ( 0 );
  543. }
  544. return ( 1 );
  545. }
  546. /// <summary>
  547. /// Sets the joystick Y-axis calibration
  548. /// </summary>
  549. /// <param name="val">MinY Value</param>
  550. /// <returns>Success</returns>
  551. double wm_nunchuck_set_calibration_miny(double val)
  552. {
  553. unsigned char calData = (unsigned char)val;
  554. try
  555. {
  556. GiiMote::gm->wc[GiiMote::gm->wmIndex]->WiimoteState->NunchukState.CalibrationInfo.MinY = calData;
  557. }
  558. catch(...)
  559. {
  560. return ( 0 );
  561. }
  562. return ( 1 );
  563. }