Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.55 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 i=1;
  34. static int k=0;
  35. static uint8_t rata;
  36.  
  37.  
  38. static PIN_Handle hMpuPin;
  39. static PIN_State MpuPinState;
  40. static PIN_Config MpuPinConfig[] = {
  41. Board_MPU_POWER | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,
  42. PIN_TERMINATE
  43. };
  44. // MPU9250 uses its own I2C interface
  45. static const I2CCC26XX_I2CPinCfg i2cMPUCfg = {
  46. .pinSDA = Board_I2C0_SDA1,
  47. .pinSCL = Board_I2C0_SCL1
  48. };
  49.  
  50. /* Pin Button1 configured as power button */
  51. static PIN_Handle hPowerButton;
  52. static PIN_State sPowerButton;
  53. PIN_Config cPowerButton[] = {
  54. Board_BUTTON1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
  55. PIN_TERMINATE
  56. };
  57. PIN_Config cPowerWake[] = {
  58. Board_BUTTON1 | PIN_INPUT_EN | PIN_PULLUP | PINCC26XX_WAKEUP_NEGEDGE,
  59. PIN_TERMINATE
  60. };
  61.  
  62. /* Pin button0 configured as input */
  63. static PIN_Handle hbutton0;
  64. static PIN_State sbutton0;
  65. PIN_Config cbutton0[] = {
  66. Board_BUTTON0 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
  67. PIN_TERMINATE
  68. };
  69.  
  70. /* Handle power button */
  71. Void powerButtonFxn(PIN_Handle handle, PIN_Id pinId) {
  72. k++;
  73. }
  74.  
  75.  
  76. /* Handle button0 */
  77. Void button0Fxn(PIN_Handle handle, PIN_Id pinId){
  78. if(i<4){i++;}
  79. else{i=1;}
  80. }
  81.  
  82.  
  83. Void commTask(UArg arg0, UArg arg1) {
  84.  
  85. char payload[16]; // viestipuskuri
  86. uint16_t senderAddr=4660;
  87.  
  88. // Radio alustetaan vastaanottotilaan
  89. int32_t result = StartReceive6LoWPAN();
  90. if(result != true) {
  91. System_abort("Wireless receive start failed");
  92. }
  93.  
  94. // Vastaanotetaan viestejä loopissa
  95. while (true) {
  96. // jos true, viesti odottaa
  97. if (GetRXFlag() == true) {
  98.  
  99. // Tyhjennetään puskuri
  100. memset(payload,0,16);
  101. // Luetaan viesti puskuriin payload
  102. Receive6LoWPAN(&senderAddr, payload, sizeof(payload));
  103. // Tulostetaan vastaanotettu viesti sensortagiin
  104. Display_print0(hDisplay, 1, 1, payload);
  105. rata=(uint8_t)payload[0];
  106. System_printf("%x \n", rata);
  107. System_flush();
  108.  
  109. }
  110. }}
  111. Void play_game(){
  112. System_printf("playing game");
  113. Display_print0(hDisplay, 3, 1, "*Play1");
  114. int x=0;
  115. uint8_t mask01=0x01,mask02=0x02,mask04=0x04,mask08=0x08,mask10=0x10,mask20=0x20,mask40=0x40,mask80=0x80;
  116. char reuna1,tie1,tie2,reuna2;
  117.  
  118. Display_print0(hDisplay, 5, 1, "*Play2");
  119. if((rata&mask80)==mask80){
  120. reuna1=" O ";
  121. }
  122. else{
  123. reuna1=" ";
  124. }
  125. Display_print0(hDisplay, 7, 1, "*Play3");
  126. sprintf("%c| | | \n", reuna1);
  127. Display_print0(hDisplay, x, 1, reuna1);
  128. }
  129.  
  130. Void gametask(UArg arg0, UArg arg1){
  131.  
  132. I2C_Handle i2c;
  133. I2C_Params i2cParams;
  134.  
  135. int j=(i-1);
  136.  
  137. /*Create I2C for usage */
  138. I2C_Params_init(&i2cParams);
  139. i2cParams.bitRate = I2C_400kHz;
  140. i2c = I2C_open(Board_I2C0, &i2cParams);
  141. if (i2c == NULL) {
  142. System_abort("Error Initializing I2C\n");
  143. }
  144.  
  145. /* Init Display */
  146. Display_Params displayParams;
  147. displayParams.lineClearMode = DISPLAY_CLEAR_BOTH;
  148. Display_Params_init(&displayParams);
  149.  
  150. hDisplay = Display_open(Display_Type_LCD, &displayParams);
  151. if (hDisplay == NULL) {
  152. System_abort("Error initializing Display\n");
  153. }
  154.  
  155. // main loop
  156. while (1) {
  157.  
  158. if(i==(j+1)){
  159. j=i;
  160. switch(i){
  161. case 1:
  162. Display_print0(hDisplay, 3, 1, "*Play");
  163. Display_print0(hDisplay, 5, 1, " Score");
  164. Display_print0(hDisplay, 7, 1, " Calibrate");
  165. Display_print0(hDisplay, 9, 1, " Exit");
  166. break;
  167. case 2:
  168. Display_print0(hDisplay, 3, 1, " Play");
  169. Display_print0(hDisplay, 5, 1, "*Score");
  170. Display_print0(hDisplay, 7, 1, " Calibrate");
  171. Display_print0(hDisplay, 9, 1, " Exit");
  172. break;
  173. case 3:
  174. Display_print0(hDisplay, 3, 1, " Play");
  175. Display_print0(hDisplay, 5, 1, " Score");
  176. Display_print0(hDisplay, 7, 1, "*Calibrate");
  177. Display_print0(hDisplay, 9, 1, " Exit");
  178. break;
  179. case 4:
  180. Display_print0(hDisplay, 3, 1, " Play");
  181. Display_print0(hDisplay, 5, 1, " Score");
  182. Display_print0(hDisplay, 7, 1, " Calibrate");
  183. Display_print0(hDisplay, 9, 1, "*Exit");
  184. j=0;
  185. break;
  186. default:
  187. break;
  188. }}
  189. if(k==1){
  190. switch(i){
  191. case 1:
  192. Display_clear(hDisplay);
  193. play_game();
  194. break;
  195. case 2:
  196. Display_clear(hDisplay);
  197. Display_print0(hDisplay, 3, 1, "no score");
  198. break;
  199. case 3:
  200. Display_clear(hDisplay);
  201. Display_print0(hDisplay, 3, 1, "calibrating");
  202. break;
  203. case 4:
  204. k=0;
  205. Display_clear(hDisplay);
  206. Display_close(hDisplay);
  207. Task_sleep(100000 / Clock_tickPeriod);
  208. Display_print0(hDisplay, 3, 1, "Shutting down");
  209. PIN_close(hPowerButton);
  210.  
  211. PINCC26XX_setWakeup(cPowerWake);
  212. Power_shutdown(NULL,0);
  213. PIN_close(hPowerButton);
  214. break;
  215. default:
  216. break;
  217. }
  218. k=0;
  219. }
  220.  
  221. // Do not remove sleep-call from here!
  222. Task_sleep(100000 / Clock_tickPeriod);
  223. }}
  224.  
  225. // SENSOR TASK
  226. Void sensorFxn(UArg arg0, UArg arg1) {
  227.  
  228. // USE TWO DIFFERENT I2C INTERFACES
  229.  
  230. I2C_Handle i2c; // INTERFACE FOR OTHER SENSORS
  231. I2C_Params i2cParams;
  232. I2C_Handle i2cMPU; // INTERFACE FOR MPU9250 SENSOR
  233. I2C_Params i2cMPUParams;
  234.  
  235. float ax, ay, az, gx, gy, gz;
  236. double pres,temp;
  237. char str[80];
  238.  
  239. I2C_Params_init(&i2cParams);
  240. i2cParams.bitRate = I2C_400kHz;
  241.  
  242. I2C_Params_init(&i2cMPUParams);
  243. i2cMPUParams.bitRate = I2C_400kHz;
  244. i2cMPUParams.custom = (uintptr_t)&i2cMPUCfg;
  245.  
  246. // MPU OPEN I2C
  247.  
  248. i2cMPU = I2C_open(Board_I2C, &i2cMPUParams);
  249. if (i2cMPU == NULL) {
  250. System_abort("Error Initializing I2CMPU\n");
  251. }
  252.  
  253. // MPU POWER ON
  254.  
  255. PIN_setOutputValue(hMpuPin,Board_MPU_POWER, Board_MPU_POWER_ON);
  256.  
  257. // WAIT 100MS FOR THE SENSOR TO POWER UP
  258. Task_sleep(100000 / Clock_tickPeriod);
  259. System_printf("MPU9250: Power ON\n");
  260. System_flush();
  261.  
  262. // MPU9250 SETUP AND CALIBRATION
  263.  
  264. System_printf("MPU9250: Setup and calibration...\n");
  265. System_flush();
  266.  
  267. mpu9250_setup(&i2cMPU);
  268.  
  269. System_printf("MPU9250: Setup and calibration OK\n");
  270. System_flush();
  271.  
  272. // MPU CLOSE I2C
  273.  
  274. I2C_close(i2cMPU);
  275.  
  276. // OTHER SENSOR OPEN I2C
  277.  
  278. i2c = I2C_open(Board_I2C, &i2cParams);
  279. if (i2c == NULL) {
  280. System_abort("Error Initializing I2C\n");
  281. }
  282.  
  283. // LOOP FOREVER
  284. while (1) {
  285.  
  286. // OTHER SENSORS OPEN I2C
  287.  
  288. i2c = I2C_open(Board_I2C, &i2cParams);
  289. if (i2c == NULL) {
  290. System_abort("Error Initializing I2C\n");
  291. }
  292.  
  293. // MPU OPEN I2C
  294.  
  295. i2cMPU = I2C_open(Board_I2C, &i2cMPUParams);
  296. if (i2cMPU == NULL) {
  297. System_abort("Error Initializing I2CMPU\n");
  298. }
  299.  
  300. // MPU ASK DATA
  301. // Accelerometer values: ax,ay,az
  302. // Gyroscope values: gx,gy,gz
  303.  
  304. mpu9250_get_data(&i2cMPU, &ax, &ay, &az, &gx, &gy, &gz);
  305.  
  306. // MPU CLOSE I2C
  307.  
  308. I2C_close(i2cMPU);
  309.  
  310. // WAIT 100MS
  311. Task_sleep(100000 / Clock_tickPeriod);
  312. }
  313.  
  314. // MPU9250 POWER OFF
  315. // Because of loop forever, code never goes here
  316. PIN_setOutputValue(hMpuPin,Board_MPU_POWER, Board_MPU_POWER_OFF);
  317. }
  318.  
  319.  
  320. int main(void){
  321.  
  322. Task_Handle hgametask;
  323. Task_Params gametaskparams;
  324. Task_Handle hCommTask;
  325. Task_Params commTaskParams;
  326. Task_Handle task;
  327. Task_Params taskParams;
  328.  
  329. // Initialize board
  330. Board_initGeneral();
  331. Board_initI2C();
  332.  
  333. // OPEN MPU POWER PIN
  334.  
  335. hMpuPin = PIN_open(&MpuPinState, MpuPinConfig);
  336. if (hMpuPin == NULL) {
  337. System_abort("Pin open failed!");
  338. }
  339.  
  340. /* Power Button */
  341. hPowerButton = PIN_open(&sPowerButton, cPowerButton);
  342. if(!hPowerButton) {
  343. System_abort("Error initializing power button shut pins\n");
  344. }
  345. if (PIN_registerIntCb(hPowerButton, &powerButtonFxn) != 0) {
  346. System_abort("Error registering power button callback function");
  347. }
  348.  
  349.  
  350. /* Move Button(button0) */
  351. hbutton0 = PIN_open(&sbutton0, cbutton0);
  352. if(!hbutton0) {
  353. System_abort("Error initializing move button\n");
  354. }
  355. if (PIN_registerIntCb(hbutton0, &button0Fxn) != 0) {
  356. System_abort("Error registering move button callback function");
  357. }
  358.  
  359.  
  360. /* Init Main Task */
  361. Task_Params_init(&gametaskparams);
  362. gametaskparams.stackSize = STACKSIZE;
  363. gametaskparams.stack = &gametaskStack;
  364. gametaskparams.priority=2;
  365.  
  366. hgametask = Task_create(gametask, &gametaskparams, NULL);
  367. if (hgametask == NULL) {
  368. System_abort("Task create failed!");
  369. }
  370.  
  371.  
  372. /* Init Communication Task */
  373. Task_Params_init(&commTaskParams);
  374. commTaskParams.stackSize = STACKSIZE;
  375. commTaskParams.stack = &commTaskStack;
  376. commTaskParams.priority=1;
  377.  
  378. Init6LoWPAN();
  379.  
  380. hCommTask = Task_create(commTask, &commTaskParams, NULL);
  381. if (hCommTask == NULL) {
  382. System_abort("Task create failed!");
  383. }
  384.  
  385.  
  386. // Send OK to console
  387. System_printf("Hello world!\n");
  388. System_flush();
  389.  
  390. /* Start BIOS */
  391. BIOS_start();
  392.  
  393. return (0);
  394. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement