Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.22 KB | None | 0 0
  1. /* XDCtools Header files */
  2. #include <xdc/std.h>
  3. #include <xdc/runtime/System.h>
  4. #include <ti/sysbios/BIOS.h>
  5. #include <ti/sysbios/knl/Task.h>
  6. #include <ti/sysbios/knl/Clock.h>
  7.  
  8. /* TI-RTOS Header files */
  9. #include <ti/drivers/I2C.h>
  10. #include <ti/drivers/PIN.h>
  11. #include <ti/drivers/pin/PINCC26XX.h>
  12. #include <ti/mw/display/Display.h>
  13. #include <ti/mw/display/DisplayExt.h>
  14.  
  15. /* Board Header files */
  16. #include "Board.h"
  17. #include <ti/drivers/i2c/I2CCC26XX.h>
  18.  
  19. /* jtkj Header files */
  20. #include "wireless/comm_lib.h"
  21. #include "sensors/mpu9250.h"
  22.  
  23. /* Task Stacks */
  24. #define STACKSIZE 4096
  25. Char gametaskStack[STACKSIZE];
  26. Char commTaskStack[STACKSIZE];
  27. Char taskStack[STACKSIZE];
  28.  
  29. /* Display */
  30. Display_Handle hDisplay;
  31.  
  32. /*global variables*/
  33. static int car=0;
  34. static int jump=0;
  35. static int game=0;
  36. static int i=1;
  37. static int k=0;
  38. static int j=0;
  39. static uint8_t rata;
  40. static float ax, ay, az, gx, gy, gz;
  41.  
  42. /*Gyro config*/
  43. static I2C_Handle i2cMPU;
  44. static I2C_Params i2cMPUParams;
  45.  
  46.  
  47. static PIN_Handle hMpuPin;
  48. static PIN_State MpuPinState;
  49. static PIN_Config MpuPinConfig[] = {
  50. Board_MPU_POWER | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,
  51. PIN_TERMINATE
  52. };
  53. // MPU9250 uses its own I2C interface
  54. static const I2CCC26XX_I2CPinCfg i2cMPUCfg = {
  55. .pinSDA = Board_I2C0_SDA1,
  56. .pinSCL = Board_I2C0_SCL1
  57. };
  58.  
  59. /* Pin Button1 configured as power button */
  60. static PIN_Handle hPowerButton;
  61. static PIN_State sPowerButton;
  62. PIN_Config cPowerButton[] = {
  63. Board_BUTTON1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
  64. PIN_TERMINATE
  65. };
  66. PIN_Config cPowerWake[] = {
  67. Board_BUTTON1 | PIN_INPUT_EN | PIN_PULLUP | PINCC26XX_WAKEUP_NEGEDGE,
  68. PIN_TERMINATE
  69. };
  70.  
  71. /* Pin button0 configured as input */
  72. static PIN_Handle hbutton0;
  73. static PIN_State sbutton0;
  74. PIN_Config cbutton0[] = {
  75. Board_BUTTON0 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
  76. PIN_TERMINATE
  77. };
  78.  
  79. /* Handle power button */
  80. Void powerButtonFxn(PIN_Handle handle, PIN_Id pinId) {
  81. k++;
  82. }
  83.  
  84.  
  85. /* Handle button0 */
  86. Void button0Fxn(PIN_Handle handle, PIN_Id pinId){
  87. if(i<4){i++;}
  88. else{i=1;}
  89. }
  90.  
  91.  
  92. Void commTask(UArg arg0, UArg arg1) {
  93. char payload[16]; // viestipuskuri
  94. uint16_t senderAddr=4660;
  95.  
  96. // Radio alustetaan vastaanottotilaan
  97. int32_t result = StartReceive6LoWPAN();
  98. if(result != true) {
  99. System_abort("Wireless receive start failed");
  100. }
  101.  
  102. // Vastaanotetaan viestejä loopissa
  103. while (true) {
  104. // jos true, viesti odottaa
  105. if (GetRXFlag() == true) {
  106.  
  107. // Tyhjennetään puskuri
  108. memset(payload,0,16);
  109. // Luetaan viesti puskuriin payload
  110. Receive6LoWPAN(&senderAddr, payload, sizeof(payload));
  111. // Tulostetaan vastaanotettu viesti
  112.  
  113. rata=(uint8_t)payload[0];
  114.  
  115. Display_print0(hDisplay, 11, 1, payload);
  116. if(rata<=0xFF){
  117. j++;
  118. System_printf("%x \n", rata);
  119. System_printf("%x \n", payload[0]);
  120. System_flush();
  121. }
  122.  
  123. }
  124. // Lähetetään serverille viesti jos nappia on painettu
  125. if((k==1)&&(game==1)){
  126. sprintf(payload,"Git Gud!", IEEE80154_MY_ADDR);
  127. Send6LoWPAN(IEEE80154_SERVER_ADDR, payload, strlen(payload));
  128. k=0;
  129. System_printf("\nMessage sent!\n");
  130. // Lähetyksen jälkeen taas vastaanottotilaan
  131. StartReceive6LoWPAN();
  132. }
  133. }}
  134. int play_game(){
  135. System_printf("playing game\n");
  136. int tulos=0;
  137. int loppu=0;
  138. int autonpaikka;
  139. int x=0;
  140. int z=j-1;
  141. uint8_t mask01=0x01,mask02=0x02,mask04=0x04,mask08=0x08,mask10=0x10,mask20=0x20,mask40=0x40,mask80=0x80;
  142. char reuna1[4],tie1[6],tie2[6],reuna2[4],track0[20]=" || | || ",track1[20]=" || | || ",track2[20]=" || | || ",track3[20]=" || | || ",track4[20]=" || | || ",track5[20]=" || | || ",track6[20]=" || | || ",track7[20]=" || | || ",track8[20]=" || | || ",track9[20]=" || | || ";
  143. game=1;
  144.  
  145. while(1){
  146. if(j>z){
  147. if(x>9){
  148. x=0;
  149. }
  150. z++;
  151.  
  152. /*Radan vasen reuna*/
  153. if((rata&mask40)==mask40){
  154.  
  155. if((rata&mask80)==mask80){
  156. sprintf(reuna1,"N$");}
  157. else{
  158. sprintf(reuna1,"NN");}
  159. }
  160. else{
  161.  
  162. if((rata&mask80)==mask80){
  163. sprintf(reuna1," $");}
  164. else{
  165. sprintf(reuna1," ");}
  166. }
  167.  
  168. /*Radan oikea reuna*/
  169. if((rata&mask02)==mask02){
  170.  
  171. if((rata&mask01)==mask01){
  172. sprintf(reuna2,"$N");}
  173. else{
  174. sprintf(reuna2,"NN");}
  175. }
  176. else{
  177.  
  178. if((rata&mask01)==mask01){
  179. sprintf(reuna2,"$ ");}
  180. else{
  181. sprintf(reuna2," ");}
  182. }
  183.  
  184. /*Rata vasen*/
  185. if((rata&mask10)==mask10){
  186.  
  187. if((rata&mask20)==mask20){
  188. sprintf(tie1,"XNX");}
  189. else{
  190. sprintf(tie1,"XXX");}
  191. }
  192. else{
  193.  
  194. if((rata&mask20)==mask20){
  195. sprintf(tie1,"NNN");}
  196. else{
  197. sprintf(tie1," ");}
  198. }
  199. /*Rata oikea*/
  200. if((rata&mask08)==mask08){
  201.  
  202. if((rata&mask04)==mask04){
  203. sprintf(tie2,"XNX");}
  204. else{
  205. sprintf(tie2,"XXX");}
  206. }
  207. else{
  208.  
  209. if((rata&mask04)==mask04){
  210. sprintf(tie2,"NNN");}
  211. else{
  212. sprintf(tie2," ");}
  213. }
  214. System_printf("%d",x);
  215. switch(x){
  216. case 0:
  217. sprintf(track0,"%s||%s|%s||%s\n", reuna1, tie1, tie2, reuna2);
  218.  
  219. Display_print0(hDisplay, 0, 1, track0);
  220. Display_print0(hDisplay, 1, 1, track1);
  221. Display_print0(hDisplay, 2, 1, track2);
  222. Display_print0(hDisplay, 3, 1, track3);
  223. Display_print0(hDisplay, 4, 1, track4);
  224. Display_print0(hDisplay, 5, 1, track5);
  225. Display_print0(hDisplay, 6, 1, track6);
  226. Display_print0(hDisplay, 7, 1, track7);
  227. Display_print0(hDisplay, 8, 1, track8);
  228. Display_print0(hDisplay, 9, 1, track9);
  229. if((car==0)&&(jump==0)){
  230. Display_print0(hDisplay, 10, 9, "CAR");
  231. autonpaikka=2;
  232. }
  233. else if((car==1)&&(jump==0)){
  234. Display_print0(hDisplay, 10, 5, "CAR");
  235. autonpaikka=1;
  236. }
  237. else if((car==0)&&(jump==1)){
  238. Display_print0(hDisplay, 10, 0, "CAR");
  239. autonpaikka=0;
  240. }
  241. else if((car==1)&&(jump==1)){
  242. Display_print0(hDisplay, 10, 14, "CAR");
  243. autonpaikka=4;
  244. }
  245. else{
  246. Display_print0(hDisplay, 10, 5, "NoCAR");
  247. }
  248. if((autonpaikka==1)&&(track9[6]==("X"))){
  249. loppu=1;
  250. }
  251.  
  252. break;
  253. case 1:
  254. sprintf(track9,"%s||%s|%s||%s\n", reuna1, tie1, tie2, reuna2);
  255. Display_print0(hDisplay, 0, 1, track9);
  256. Display_print0(hDisplay, 1, 1, track0);
  257. Display_print0(hDisplay, 2, 1, track1);
  258. Display_print0(hDisplay, 3, 1, track2);
  259. Display_print0(hDisplay, 4, 1, track3);
  260. Display_print0(hDisplay, 5, 1, track4);
  261. Display_print0(hDisplay, 6, 1, track5);
  262. Display_print0(hDisplay, 7, 1, track6);
  263. Display_print0(hDisplay, 8, 1, track7);
  264. Display_print0(hDisplay, 9, 1, track8);
  265. if((car==0)&&(jump==0)){
  266. Display_print0(hDisplay, 10, 9, "CAR");
  267. }
  268. else if((car==1)&&(jump==0)){
  269. Display_print0(hDisplay, 10, 5, "CAR");
  270. }
  271. else if((car==0)&&(jump==1)){
  272. Display_print0(hDisplay, 10, 0, "CAR");
  273. }
  274. else if((car==1)&&(jump==1)){
  275. Display_print0(hDisplay, 10, 14, "CAR");
  276. }
  277. else{
  278. Display_print0(hDisplay, 10, 6, "NoCAR");
  279. }
  280. break;
  281. case 2:
  282. sprintf(track8,"%s||%s|%s||%s\n", reuna1, tie1, tie2, reuna2);
  283. Display_print0(hDisplay, 0, 1, track8);
  284. Display_print0(hDisplay, 1, 1, track9);
  285. Display_print0(hDisplay, 2, 1, track0);
  286. Display_print0(hDisplay, 3, 1, track1);
  287. Display_print0(hDisplay, 4, 1, track2);
  288. Display_print0(hDisplay, 5, 1, track3);
  289. Display_print0(hDisplay, 6, 1, track4);
  290. Display_print0(hDisplay, 7, 1, track5);
  291. Display_print0(hDisplay, 8, 1, track6);
  292. Display_print0(hDisplay, 9, 1, track7);
  293. if((car==0)&&(jump==0)){
  294. Display_print0(hDisplay, 10, 9, "CAR");
  295. }
  296. else if((car==1)&&(jump==0)){
  297. Display_print0(hDisplay, 10, 5, "CAR");
  298. }
  299. else if((car==0)&&(jump==1)){
  300. Display_print0(hDisplay, 10, 0, "CAR");
  301. }
  302. else if((car==1)&&(jump==1)){
  303. Display_print0(hDisplay, 10, 14, "CAR");
  304. }
  305. else{
  306. Display_print0(hDisplay, 10, 6, "NoCAR");
  307. }
  308. break;
  309. case 3:
  310. sprintf(track7,"%s||%s|%s||%s\n", reuna1, tie1, tie2, reuna2);
  311. Display_print0(hDisplay, 0, 1, track7);
  312. Display_print0(hDisplay, 1, 1, track8);
  313. Display_print0(hDisplay, 2, 1, track9);
  314. Display_print0(hDisplay, 3, 1, track0);
  315. Display_print0(hDisplay, 4, 1, track1);
  316. Display_print0(hDisplay, 5, 1, track2);
  317. Display_print0(hDisplay, 6, 1, track3);
  318. Display_print0(hDisplay, 7, 1, track4);
  319. Display_print0(hDisplay, 8, 1, track5);
  320. Display_print0(hDisplay, 9, 1, track6);
  321. if((car==0)&&(jump==0)){
  322. Display_print0(hDisplay, 10, 9, "CAR");
  323. }
  324. else if((car==1)&&(jump==0)){
  325. Display_print0(hDisplay, 10, 5, "CAR");
  326. }
  327. else if((car==0)&&(jump==1)){
  328. Display_print0(hDisplay, 10, 0, "CAR");
  329. }
  330. else if((car==1)&&(jump==1)){
  331. Display_print0(hDisplay, 10, 14, "CAR");
  332. }
  333. else{
  334. Display_print0(hDisplay, 10, 6, "NoCAR");
  335. }
  336. break;
  337. case 4:
  338. sprintf(track6,"%s||%s|%s||%s\n", reuna1, tie1, tie2, reuna2);
  339. Display_print0(hDisplay, 0, 1, track6);
  340. Display_print0(hDisplay, 1, 1, track7);
  341. Display_print0(hDisplay, 2, 1, track8);
  342. Display_print0(hDisplay, 3, 1, track9);
  343. Display_print0(hDisplay, 4, 1, track0);
  344. Display_print0(hDisplay, 5, 1, track1);
  345. Display_print0(hDisplay, 6, 1, track2);
  346. Display_print0(hDisplay, 7, 1, track3);
  347. Display_print0(hDisplay, 8, 1, track4);
  348. Display_print0(hDisplay, 9, 1, track5);
  349. if((car==0)&&(jump==0)){
  350. Display_print0(hDisplay, 10, 9, "CAR");
  351. }
  352. else if((car==1)&&(jump==0)){
  353. Display_print0(hDisplay, 10, 5, "CAR");
  354. }
  355. else if((car==0)&&(jump==1)){
  356. Display_print0(hDisplay, 10, 0, "CAR");
  357. }
  358. else if((car==1)&&(jump==1)){
  359. Display_print0(hDisplay, 10, 14, "CAR");
  360. }
  361. else{
  362. Display_print0(hDisplay, 10, 5, "NoCAR");
  363. }
  364. break;
  365. case 5:
  366. sprintf(track5,"%s||%s|%s||%s\n", reuna1, tie1, tie2, reuna2);
  367. Display_print0(hDisplay, 0, 1, track5);
  368. Display_print0(hDisplay, 1, 1, track6);
  369. Display_print0(hDisplay, 2, 1, track7);
  370. Display_print0(hDisplay, 3, 1, track8);
  371. Display_print0(hDisplay, 4, 1, track9);
  372. Display_print0(hDisplay, 5, 1, track0);
  373. Display_print0(hDisplay, 6, 1, track1);
  374. Display_print0(hDisplay, 7, 1, track2);
  375. Display_print0(hDisplay, 8, 1, track3);
  376. Display_print0(hDisplay, 9, 1, track4);
  377. if((car==0)&&(jump==0)){
  378. Display_print0(hDisplay, 10, 9, "CAR");
  379. }
  380. else if((car==1)&&(jump==0)){
  381. Display_print0(hDisplay, 10, 5, "CAR");
  382. }
  383. else if((car==0)&&(jump==1)){
  384. Display_print0(hDisplay, 10, 0, "CAR");
  385. }
  386. else if((car==1)&&(jump==1)){
  387. Display_print0(hDisplay, 10, 14, "CAR");
  388. }
  389. else{
  390. Display_print0(hDisplay, 10, 5, "NoCAR");
  391. }
  392. break;
  393. case 6:
  394. sprintf(track4,"%s||%s|%s||%s\n", reuna1, tie1, tie2, reuna2);
  395. Display_print0(hDisplay, 0, 1, track4);
  396. Display_print0(hDisplay, 1, 1, track5);
  397. Display_print0(hDisplay, 2, 1, track6);
  398. Display_print0(hDisplay, 3, 1, track7);
  399. Display_print0(hDisplay, 4, 1, track8);
  400. Display_print0(hDisplay, 5, 1, track9);
  401. Display_print0(hDisplay, 6, 1, track0);
  402. Display_print0(hDisplay, 7, 1, track1);
  403. Display_print0(hDisplay, 8, 1, track2);
  404. Display_print0(hDisplay, 9, 1, track3);
  405. if((car==0)&&(jump==0)){
  406. Display_print0(hDisplay, 10, 9, "CAR");
  407. }
  408. else if((car==1)&&(jump==0)){
  409. Display_print0(hDisplay, 10, 5, "CAR");
  410. }
  411. else if((car==0)&&(jump==1)){
  412. Display_print0(hDisplay, 10, 0, "CAR");
  413. }
  414. else if((car==1)&&(jump==1)){
  415. Display_print0(hDisplay, 10, 14, "CAR");
  416. }
  417. else{
  418. Display_print0(hDisplay, 10, 5, "NoCAR");
  419. }
  420. break;
  421. case 7:
  422. sprintf(track3,"%s||%s|%s||%s\n", reuna1, tie1, tie2, reuna2);
  423. Display_print0(hDisplay, 0, 1, track3);
  424. Display_print0(hDisplay, 1, 1, track4);
  425. Display_print0(hDisplay, 2, 1, track5);
  426. Display_print0(hDisplay, 3, 1, track6);
  427. Display_print0(hDisplay, 4, 1, track7);
  428. Display_print0(hDisplay, 5, 1, track8);
  429. Display_print0(hDisplay, 6, 1, track9);
  430. Display_print0(hDisplay, 7, 1, track0);
  431. Display_print0(hDisplay, 8, 1, track1);
  432. Display_print0(hDisplay, 9, 1, track2);
  433. if((car==0)&&(jump==0)){
  434. Display_print0(hDisplay, 10, 9, "CAR");
  435. }
  436. else if((car==1)&&(jump==0)){
  437. Display_print0(hDisplay, 10, 5, "CAR");
  438. }
  439. else if((car==0)&&(jump==1)){
  440. Display_print0(hDisplay, 10, 0, "CAR");
  441. }
  442. else if((car==1)&&(jump==1)){
  443. Display_print0(hDisplay, 10, 14, "CAR");
  444. }
  445. else{
  446. Display_print0(hDisplay, 10, 5, "NoCAR");
  447. }
  448. break;
  449. case 8:
  450. sprintf(track2,"%s||%s|%s||%s\n", reuna1, tie1, tie2, reuna2);
  451. Display_print0(hDisplay, 0, 1, track2);
  452. Display_print0(hDisplay, 1, 1, track3);
  453. Display_print0(hDisplay, 2, 1, track4);
  454. Display_print0(hDisplay, 3, 1, track5);
  455. Display_print0(hDisplay, 4, 1, track6);
  456. Display_print0(hDisplay, 5, 1, track7);
  457. Display_print0(hDisplay, 6, 1, track8);
  458. Display_print0(hDisplay, 7, 1, track9);
  459. Display_print0(hDisplay, 8, 1, track0);
  460. Display_print0(hDisplay, 9, 1, track1);
  461. if((car==0)&&(jump==0)){
  462. Display_print0(hDisplay, 10, 9, "CAR");
  463. }
  464. else if((car==1)&&(jump==0)){
  465. Display_print0(hDisplay, 10, 5, "CAR");
  466. }
  467. else if((car==0)&&(jump==1)){
  468. Display_print0(hDisplay, 10, 0, "CAR");
  469. }
  470. else if((car==1)&&(jump==1)){
  471. Display_print0(hDisplay, 10, 14, "CAR");
  472. }
  473. else{
  474. Display_print0(hDisplay, 10, 5, "NoCAR");
  475. }
  476. break;
  477. case 9:
  478. sprintf(track1,"%s||%s|%s||%s\n", reuna1, tie1, tie2, reuna2);
  479. Display_print0(hDisplay, 0, 1, track1);
  480. Display_print0(hDisplay, 1, 1, track2);
  481. Display_print0(hDisplay, 2, 1, track3);
  482. Display_print0(hDisplay, 3, 1, track4);
  483. Display_print0(hDisplay, 4, 1, track5);
  484. Display_print0(hDisplay, 5, 1, track6);
  485. Display_print0(hDisplay, 6, 1, track7);
  486. Display_print0(hDisplay, 7, 1, track8);
  487. Display_print0(hDisplay, 8, 1, track9);
  488. Display_print0(hDisplay, 9, 1, track0);
  489. if((car==0)&&(jump==0)){
  490. Display_print0(hDisplay, 10, 9, "CAR");
  491. }
  492. else if((car==1)&&(jump==0)){
  493. Display_print0(hDisplay, 10, 5, "CAR");
  494. }
  495. else if((car==0)&&(jump==1)){
  496. Display_print0(hDisplay, 10, 0, "CAR");
  497. }
  498. else if((car==1)&&(jump==1)){
  499. Display_print0(hDisplay, 10, 14, "CAR");
  500. }
  501. else{
  502. Display_print0(hDisplay, 10, 5, "NoCAR");
  503. }
  504. break;
  505.  
  506.  
  507. default:
  508. break;
  509.  
  510. }
  511. x++;
  512. }
  513. if(loppu!=0){
  514. return(tulos);
  515. }
  516. Task_sleep(100000 / Clock_tickPeriod);
  517. }
  518. }
  519.  
  520. Void gametask(UArg arg0, UArg arg1){
  521.  
  522. I2C_Handle i2c;
  523. I2C_Params i2cParams;
  524.  
  525. int y=(i-1),tulos;
  526.  
  527. /*Create I2C for usage */
  528. I2C_Params_init(&i2cParams);
  529. i2cParams.bitRate = I2C_400kHz;
  530. i2c = I2C_open(Board_I2C0, &i2cParams);
  531. if (i2c == NULL) {
  532. System_abort("Error Initializing I2C\n");
  533. }
  534.  
  535. /* Init Display */
  536. Display_Params displayParams;
  537. displayParams.lineClearMode = DISPLAY_CLEAR_BOTH;
  538. Display_Params_init(&displayParams);
  539.  
  540. hDisplay = Display_open(Display_Type_LCD, &displayParams);
  541. if (hDisplay == NULL) {
  542. System_abort("Error initializing Display\n");
  543. }
  544. Display_clear(hDisplay);
  545. Display_print0(hDisplay, 2, 1, "GENERIC");
  546. Display_print0(hDisplay, 3, 1, "CAR");
  547. Display_print0(hDisplay, 4, 1, "GAME");
  548. Display_print0(hDisplay, 9, 1, "IGN 10/10");
  549. Display_print0(hDisplay, 10, 1, "masterpiece!?!?");
  550.  
  551. Task_sleep(3000000 / Clock_tickPeriod);
  552. Display_clear(hDisplay);
  553.  
  554. // main loop
  555. while (1) {
  556.  
  557. if(i==(y+1)){
  558. y=i;
  559. switch(i){
  560. case 1:
  561. Display_print0(hDisplay, 0, 12, "move");
  562. Display_print0(hDisplay, 2, 1, "*Play*");
  563. Display_print0(hDisplay, 4, 1, " Score");
  564. Display_print0(hDisplay, 6, 1, " Calibrate");
  565. Display_print0(hDisplay, 8, 1, " Exit");
  566. Display_print0(hDisplay, 10, 10, "select");
  567. break;
  568. case 2:
  569. Display_print0(hDisplay, 0, 12, "move");
  570. Display_print0(hDisplay, 2, 1, " Play");
  571. Display_print0(hDisplay, 4, 1, "*Score*");
  572. Display_print0(hDisplay, 6, 1, " Calibrate");
  573. Display_print0(hDisplay, 8, 1, " Exit");
  574. Display_print0(hDisplay, 10, 10, "select");
  575. break;
  576. case 3:
  577. Display_print0(hDisplay, 0, 12, "move");
  578. Display_print0(hDisplay, 2, 1, " Play");
  579. Display_print0(hDisplay, 4, 1, " Score");
  580. Display_print0(hDisplay, 6, 1, "*Calibrate*");
  581. Display_print0(hDisplay, 8, 1, " Exit");
  582. Display_print0(hDisplay, 10, 10, "select");
  583. break;
  584. case 4:
  585. Display_print0(hDisplay, 0, 12, "move");
  586. Display_print0(hDisplay, 2, 1, " Play");
  587. Display_print0(hDisplay, 4, 1, " Score");
  588. Display_print0(hDisplay, 6, 1, " Calibrate");
  589. Display_print0(hDisplay, 8, 1, "*Exit*");
  590. Display_print0(hDisplay, 10, 10, "select");
  591. y=0;
  592. break;
  593. default:
  594. break;
  595. }}
  596. if(k==1){
  597. switch(i){
  598. case 1:
  599. Display_clear(hDisplay);
  600. tulos=play_game();
  601. Display_clear(hDisplay);
  602. Display_print0(hDisplay, 3, 1, "Game over");
  603. break;
  604. case 2:
  605. Display_clear(hDisplay);
  606. Display_print0(hDisplay, 3, 1, "no score");
  607. break;
  608. case 3:
  609. Display_clear(hDisplay);
  610. Display_print0(hDisplay, 3, 1, "calibrating");
  611. break;
  612. case 4:
  613. k=0;
  614. Display_clear(hDisplay);
  615. Display_close(hDisplay);
  616. Task_sleep(100000 / Clock_tickPeriod);
  617. Display_print0(hDisplay, 3, 1, "Shutting down");
  618. PIN_close(hPowerButton);
  619.  
  620. PINCC26XX_setWakeup(cPowerWake);
  621. Power_shutdown(NULL,0);
  622. PIN_close(hPowerButton);
  623. break;
  624. default:
  625. break;
  626. }
  627. k=0;
  628. }
  629.  
  630. // Do not remove sleep-call from here!
  631. Task_sleep(100000 / Clock_tickPeriod);
  632. }}
  633.  
  634. // SENSOR TASK
  635. Void sensorFxn(UArg arg0, UArg arg1) {
  636.  
  637. // USE TWO DIFFERENT I2C INTERFACES
  638.  
  639. I2C_Handle i2c; // INTERFACE FOR OTHER SENSORS
  640. I2C_Params i2cParams;
  641. I2C_Handle i2cMPU; // INTERFACE FOR MPU9250 SENSOR
  642. I2C_Params i2cMPUParams;
  643.  
  644. float ax, ay, az, gx, gy, gz;
  645. double pres,temp;
  646. char str[80];
  647.  
  648. I2C_Params_init(&i2cParams);
  649. i2cParams.bitRate = I2C_400kHz;
  650.  
  651. I2C_Params_init(&i2cMPUParams);
  652. i2cMPUParams.bitRate = I2C_400kHz;
  653. i2cMPUParams.custom = (uintptr_t)&i2cMPUCfg;
  654.  
  655. // MPU OPEN I2C
  656.  
  657. i2cMPU = I2C_open(Board_I2C, &i2cMPUParams);
  658. if (i2cMPU == NULL) {
  659. System_abort("Error Initializing I2CMPU\n");
  660. }
  661.  
  662. // MPU POWER ON
  663.  
  664. PIN_setOutputValue(hMpuPin,Board_MPU_POWER, Board_MPU_POWER_ON);
  665.  
  666. // WAIT 100MS FOR THE SENSOR TO POWER UP
  667. Task_sleep(100000 / Clock_tickPeriod);
  668. System_printf("MPU9250: Power ON\n");
  669. System_flush();
  670.  
  671. // MPU9250 SETUP AND CALIBRATION
  672.  
  673. System_printf("MPU9250: Setup and calibration...\n");
  674. System_flush();
  675.  
  676. mpu9250_setup(&i2cMPU);
  677.  
  678. System_printf("MPU9250: Setup and calibration OK\n");
  679. System_flush();
  680.  
  681. // MPU CLOSE I2C
  682.  
  683. I2C_close(i2cMPU);
  684.  
  685. // OTHER SENSOR OPEN I2C
  686.  
  687. i2c = I2C_open(Board_I2C, &i2cParams);
  688. if (i2c == NULL) {
  689. System_abort("Error Initializing I2C\n");
  690. }
  691.  
  692. // LOOP FOREVER
  693. while (1) {
  694.  
  695. // OTHER SENSORS OPEN I2C
  696.  
  697. i2c = I2C_open(Board_I2C, &i2cParams);
  698. if (i2c == NULL) {
  699. System_abort("Error Initializing I2C\n");
  700. }
  701.  
  702. // MPU OPEN I2C
  703.  
  704. i2cMPU = I2C_open(Board_I2C, &i2cMPUParams);
  705. if (i2cMPU == NULL) {
  706. System_abort("Error Initializing I2CMPU\n");
  707. }
  708.  
  709. // MPU ASK DATA
  710. // Accelerometer values: ax,ay,az
  711. // Gyroscope values: gx,gy,gz
  712.  
  713. mpu9250_get_data(&i2cMPU, &ax, &ay, &az, &gx, &gy, &gz);
  714.  
  715. // MPU CLOSE I2C
  716.  
  717. I2C_close(i2cMPU);
  718.  
  719. // WAIT 100MS
  720. Task_sleep(100000 / Clock_tickPeriod);
  721. }
  722.  
  723. // MPU9250 POWER OFF
  724. // Because of loop forever, code never goes here
  725. PIN_setOutputValue(hMpuPin,Board_MPU_POWER, Board_MPU_POWER_OFF);
  726. }
  727.  
  728. float Calibrate() {
  729. System_printf("Calibrating");
  730. System_flush();
  731. mpu9250_get_data(&i2cMPU, &ax, &ay, &az, &gx, &gy, &gz);
  732. System_printf("tilt to left");
  733. System_flush();
  734. }
  735.  
  736. int main(void){
  737.  
  738. Task_Handle hgametask;
  739. Task_Params gametaskparams;
  740. Task_Handle hCommTask;
  741. Task_Params commTaskParams;
  742. Task_Handle task;
  743. Task_Params taskParams;
  744.  
  745. // Initialize board
  746. Board_initGeneral();
  747. Board_initI2C();
  748.  
  749. // OPEN MPU POWER PIN
  750.  
  751. hMpuPin = PIN_open(&MpuPinState, MpuPinConfig);
  752. if (hMpuPin == NULL) {
  753. System_abort("Pin open failed!");
  754. }
  755.  
  756. /* Power Button */
  757. hPowerButton = PIN_open(&sPowerButton, cPowerButton);
  758. if(!hPowerButton) {
  759. System_abort("Error initializing power button shut pins\n");
  760. }
  761. if (PIN_registerIntCb(hPowerButton, &powerButtonFxn) != 0) {
  762. System_abort("Error registering power button callback function");
  763. }
  764.  
  765.  
  766. /* Move Button(button0) */
  767. hbutton0 = PIN_open(&sbutton0, cbutton0);
  768. if(!hbutton0) {
  769. System_abort("Error initializing move button\n");
  770. }
  771. if (PIN_registerIntCb(hbutton0, &button0Fxn) != 0) {
  772. System_abort("Error registering move button callback function");
  773. }
  774.  
  775.  
  776. /* Init Main Task */
  777. Task_Params_init(&gametaskparams);
  778. gametaskparams.stackSize = STACKSIZE;
  779. gametaskparams.stack = &gametaskStack;
  780. gametaskparams.priority=2;
  781.  
  782. hgametask = Task_create(gametask, &gametaskparams, NULL);
  783. if (hgametask == NULL) {
  784. System_abort("Task create failed!");
  785. }
  786.  
  787.  
  788. /* Init Communication Task */
  789. Task_Params_init(&commTaskParams);
  790. commTaskParams.stackSize = STACKSIZE;
  791. commTaskParams.stack = &commTaskStack;
  792. commTaskParams.priority=1;
  793.  
  794. Init6LoWPAN();
  795.  
  796. hCommTask = Task_create(commTask, &commTaskParams, NULL);
  797. if (hCommTask == NULL) {
  798. System_abort("Task create failed!");
  799. }
  800. /*Init Calibration Task*/
  801. /*Task_Params_init(&taskParams);
  802. taskParams.stackSize = STACKSIZE;
  803. taskParams.stack = &taskStack;
  804. task = Task_create((Task_FuncPtr)sensorFxn, &taskParams, NULL);
  805. if (task == NULL) {
  806. System_abort("Task create failed!");
  807. }*/
  808.  
  809. // Send OK to console
  810. System_printf("Hello world!\n");
  811. System_flush();
  812.  
  813. /* Start BIOS */
  814. BIOS_start();
  815.  
  816. return (0);
  817. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement