Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* XDCtools Header files */
- #include <xdc/std.h>
- #include <xdc/runtime/System.h>
- #include <ti/sysbios/BIOS.h>
- #include <ti/sysbios/knl/Task.h>
- #include <ti/sysbios/knl/Clock.h>
- /* TI-RTOS Header files */
- #include <ti/drivers/I2C.h>
- #include <ti/drivers/PIN.h>
- #include <ti/drivers/pin/PINCC26XX.h>
- #include <ti/mw/display/Display.h>
- #include <ti/mw/display/DisplayExt.h>
- /* Board Header files */
- #include "Board.h"
- #include <ti/drivers/i2c/I2CCC26XX.h>
- /* jtkj Header files */
- #include "wireless/comm_lib.h"
- #include "sensors/mpu9250.h"
- /* Task Stacks */
- #define STACKSIZE 4096
- Char gametaskStack[STACKSIZE];
- Char commTaskStack[STACKSIZE];
- Char taskStack[STACKSIZE];
- /* Display */
- Display_Handle hDisplay;
- /*global variables*/
- static int i=1;
- static int k=0;
- static uint8_t rata;
- static PIN_Handle hMpuPin;
- static PIN_State MpuPinState;
- static PIN_Config MpuPinConfig[] = {
- Board_MPU_POWER | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,
- PIN_TERMINATE
- };
- // MPU9250 uses its own I2C interface
- static const I2CCC26XX_I2CPinCfg i2cMPUCfg = {
- .pinSDA = Board_I2C0_SDA1,
- .pinSCL = Board_I2C0_SCL1
- };
- /* Pin Button1 configured as power button */
- static PIN_Handle hPowerButton;
- static PIN_State sPowerButton;
- PIN_Config cPowerButton[] = {
- Board_BUTTON1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
- PIN_TERMINATE
- };
- PIN_Config cPowerWake[] = {
- Board_BUTTON1 | PIN_INPUT_EN | PIN_PULLUP | PINCC26XX_WAKEUP_NEGEDGE,
- PIN_TERMINATE
- };
- /* Pin button0 configured as input */
- static PIN_Handle hbutton0;
- static PIN_State sbutton0;
- PIN_Config cbutton0[] = {
- Board_BUTTON0 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
- PIN_TERMINATE
- };
- /* Handle power button */
- Void powerButtonFxn(PIN_Handle handle, PIN_Id pinId) {
- k++;
- }
- /* Handle button0 */
- Void button0Fxn(PIN_Handle handle, PIN_Id pinId){
- if(i<4){i++;}
- else{i=1;}
- }
- Void commTask(UArg arg0, UArg arg1) {
- char payload[16]; // viestipuskuri
- uint16_t senderAddr=4660;
- // Radio alustetaan vastaanottotilaan
- int32_t result = StartReceive6LoWPAN();
- if(result != true) {
- System_abort("Wireless receive start failed");
- }
- // Vastaanotetaan viestejä loopissa
- while (true) {
- // jos true, viesti odottaa
- if (GetRXFlag() == true) {
- // Tyhjennetään puskuri
- memset(payload,0,16);
- // Luetaan viesti puskuriin payload
- Receive6LoWPAN(&senderAddr, payload, sizeof(payload));
- // Tulostetaan vastaanotettu viesti sensortagiin
- Display_print0(hDisplay, 1, 1, payload);
- rata=(uint8_t)payload[0];
- System_printf("%x \n", rata);
- System_flush();
- }
- }}
- Void play_game(){
- System_printf("playing game");
- Display_print0(hDisplay, 3, 1, "*Play1");
- int x=0;
- uint8_t mask01=0x01,mask02=0x02,mask04=0x04,mask08=0x08,mask10=0x10,mask20=0x20,mask40=0x40,mask80=0x80;
- char reuna1,tie1,tie2,reuna2;
- Display_print0(hDisplay, 5, 1, "*Play2");
- if((rata&mask80)==mask80){
- reuna1=" O ";
- }
- else{
- reuna1=" ";
- }
- Display_print0(hDisplay, 7, 1, "*Play3");
- sprintf("%c| | | \n", reuna1);
- Display_print0(hDisplay, x, 1, reuna1);
- }
- Void gametask(UArg arg0, UArg arg1){
- I2C_Handle i2c;
- I2C_Params i2cParams;
- int j=(i-1);
- /*Create I2C for usage */
- I2C_Params_init(&i2cParams);
- i2cParams.bitRate = I2C_400kHz;
- i2c = I2C_open(Board_I2C0, &i2cParams);
- if (i2c == NULL) {
- System_abort("Error Initializing I2C\n");
- }
- /* Init Display */
- Display_Params displayParams;
- displayParams.lineClearMode = DISPLAY_CLEAR_BOTH;
- Display_Params_init(&displayParams);
- hDisplay = Display_open(Display_Type_LCD, &displayParams);
- if (hDisplay == NULL) {
- System_abort("Error initializing Display\n");
- }
- // main loop
- while (1) {
- if(i==(j+1)){
- j=i;
- switch(i){
- case 1:
- Display_print0(hDisplay, 3, 1, "*Play");
- Display_print0(hDisplay, 5, 1, " Score");
- Display_print0(hDisplay, 7, 1, " Calibrate");
- Display_print0(hDisplay, 9, 1, " Exit");
- break;
- case 2:
- Display_print0(hDisplay, 3, 1, " Play");
- Display_print0(hDisplay, 5, 1, "*Score");
- Display_print0(hDisplay, 7, 1, " Calibrate");
- Display_print0(hDisplay, 9, 1, " Exit");
- break;
- case 3:
- Display_print0(hDisplay, 3, 1, " Play");
- Display_print0(hDisplay, 5, 1, " Score");
- Display_print0(hDisplay, 7, 1, "*Calibrate");
- Display_print0(hDisplay, 9, 1, " Exit");
- break;
- case 4:
- Display_print0(hDisplay, 3, 1, " Play");
- Display_print0(hDisplay, 5, 1, " Score");
- Display_print0(hDisplay, 7, 1, " Calibrate");
- Display_print0(hDisplay, 9, 1, "*Exit");
- j=0;
- break;
- default:
- break;
- }}
- if(k==1){
- switch(i){
- case 1:
- Display_clear(hDisplay);
- play_game();
- break;
- case 2:
- Display_clear(hDisplay);
- Display_print0(hDisplay, 3, 1, "no score");
- break;
- case 3:
- Display_clear(hDisplay);
- Display_print0(hDisplay, 3, 1, "calibrating");
- break;
- case 4:
- k=0;
- Display_clear(hDisplay);
- Display_close(hDisplay);
- Task_sleep(100000 / Clock_tickPeriod);
- Display_print0(hDisplay, 3, 1, "Shutting down");
- PIN_close(hPowerButton);
- PINCC26XX_setWakeup(cPowerWake);
- Power_shutdown(NULL,0);
- PIN_close(hPowerButton);
- break;
- default:
- break;
- }
- k=0;
- }
- // Do not remove sleep-call from here!
- Task_sleep(100000 / Clock_tickPeriod);
- }}
- // SENSOR TASK
- Void sensorFxn(UArg arg0, UArg arg1) {
- // USE TWO DIFFERENT I2C INTERFACES
- I2C_Handle i2c; // INTERFACE FOR OTHER SENSORS
- I2C_Params i2cParams;
- I2C_Handle i2cMPU; // INTERFACE FOR MPU9250 SENSOR
- I2C_Params i2cMPUParams;
- float ax, ay, az, gx, gy, gz;
- double pres,temp;
- char str[80];
- I2C_Params_init(&i2cParams);
- i2cParams.bitRate = I2C_400kHz;
- I2C_Params_init(&i2cMPUParams);
- i2cMPUParams.bitRate = I2C_400kHz;
- i2cMPUParams.custom = (uintptr_t)&i2cMPUCfg;
- // MPU OPEN I2C
- i2cMPU = I2C_open(Board_I2C, &i2cMPUParams);
- if (i2cMPU == NULL) {
- System_abort("Error Initializing I2CMPU\n");
- }
- // MPU POWER ON
- PIN_setOutputValue(hMpuPin,Board_MPU_POWER, Board_MPU_POWER_ON);
- // WAIT 100MS FOR THE SENSOR TO POWER UP
- Task_sleep(100000 / Clock_tickPeriod);
- System_printf("MPU9250: Power ON\n");
- System_flush();
- // MPU9250 SETUP AND CALIBRATION
- System_printf("MPU9250: Setup and calibration...\n");
- System_flush();
- mpu9250_setup(&i2cMPU);
- System_printf("MPU9250: Setup and calibration OK\n");
- System_flush();
- // MPU CLOSE I2C
- I2C_close(i2cMPU);
- // OTHER SENSOR OPEN I2C
- i2c = I2C_open(Board_I2C, &i2cParams);
- if (i2c == NULL) {
- System_abort("Error Initializing I2C\n");
- }
- // LOOP FOREVER
- while (1) {
- // OTHER SENSORS OPEN I2C
- i2c = I2C_open(Board_I2C, &i2cParams);
- if (i2c == NULL) {
- System_abort("Error Initializing I2C\n");
- }
- // MPU OPEN I2C
- i2cMPU = I2C_open(Board_I2C, &i2cMPUParams);
- if (i2cMPU == NULL) {
- System_abort("Error Initializing I2CMPU\n");
- }
- // MPU ASK DATA
- // Accelerometer values: ax,ay,az
- // Gyroscope values: gx,gy,gz
- mpu9250_get_data(&i2cMPU, &ax, &ay, &az, &gx, &gy, &gz);
- // MPU CLOSE I2C
- I2C_close(i2cMPU);
- // WAIT 100MS
- Task_sleep(100000 / Clock_tickPeriod);
- }
- // MPU9250 POWER OFF
- // Because of loop forever, code never goes here
- PIN_setOutputValue(hMpuPin,Board_MPU_POWER, Board_MPU_POWER_OFF);
- }
- int main(void){
- Task_Handle hgametask;
- Task_Params gametaskparams;
- Task_Handle hCommTask;
- Task_Params commTaskParams;
- Task_Handle task;
- Task_Params taskParams;
- // Initialize board
- Board_initGeneral();
- Board_initI2C();
- // OPEN MPU POWER PIN
- hMpuPin = PIN_open(&MpuPinState, MpuPinConfig);
- if (hMpuPin == NULL) {
- System_abort("Pin open failed!");
- }
- /* Power Button */
- hPowerButton = PIN_open(&sPowerButton, cPowerButton);
- if(!hPowerButton) {
- System_abort("Error initializing power button shut pins\n");
- }
- if (PIN_registerIntCb(hPowerButton, &powerButtonFxn) != 0) {
- System_abort("Error registering power button callback function");
- }
- /* Move Button(button0) */
- hbutton0 = PIN_open(&sbutton0, cbutton0);
- if(!hbutton0) {
- System_abort("Error initializing move button\n");
- }
- if (PIN_registerIntCb(hbutton0, &button0Fxn) != 0) {
- System_abort("Error registering move button callback function");
- }
- /* Init Main Task */
- Task_Params_init(&gametaskparams);
- gametaskparams.stackSize = STACKSIZE;
- gametaskparams.stack = &gametaskStack;
- gametaskparams.priority=2;
- hgametask = Task_create(gametask, &gametaskparams, NULL);
- if (hgametask == NULL) {
- System_abort("Task create failed!");
- }
- /* Init Communication Task */
- Task_Params_init(&commTaskParams);
- commTaskParams.stackSize = STACKSIZE;
- commTaskParams.stack = &commTaskStack;
- commTaskParams.priority=1;
- Init6LoWPAN();
- hCommTask = Task_create(commTask, &commTaskParams, NULL);
- if (hCommTask == NULL) {
- System_abort("Task create failed!");
- }
- // Send OK to console
- System_printf("Hello world!\n");
- System_flush();
- /* Start BIOS */
- BIOS_start();
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement