⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simple_mouse.c

📁 reference about wireless design which is helpful to everyone
💻 C
字号:
#include "cc2511_app_ex_lib_headers.h"
#include "simple_hid_eb.h"
#include "RF04EB.h"
#include "lcd.h"





extern RF_PACKET __xdata * pRfTxBuffer;
extern volatile BOOL radioReadyToSend;
extern BYTE dongleAddress; //device address of the USB dongle.
extern volatile BOOL ackTimeout;

BOOL updateRssi;


const __code char smLcdInit[2][16] = {{'U', 's', 'e', ' ', 'J', 'o', 'y', ' ', 'a', 's', ' ', 'm', 'o', 'u', 's', 'e'},
                                      {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}};
char __xdata lcdRssiText[16];


RF_PACKET __xdata * radioAckPkt;

void simpleMouse(void)
{
   BOOL packetUpdated= FALSE;
   BYTE sameDirectionCount = 0;//used to get acceleration on the mouse movements
   UINT8 timer4Index;//the index the timer4 module assignes to the callback used in this function.


   radioReadyToSend = TRUE;//used to know when the radio is ready for a new packet
   radioAckPkt = rfruGetpRxAckPkt();//Need access to the ACK packets to get the RSSI value.

   //Update LCD screen
   lcdUpdateLine(LINE1, (__xdata char * ) &smLcdInit[0][0]);
   lcdUpdateLine(LINE2, (__xdata char * ) &smLcdInit[1][0]);

   timer4Index = t4mgrSetupCallback(40, rssiUpdate);//start callbacks from the timer4 module to know when to update the LCD screen.
                                                  //the timer4 module is setup to interrupt each 25 ms. afer 40 interrupt "rssiUpdate" is called.
                                                  //e.g. "rssiUpdate" is called each second.

   //loop that runs while the program is in "simple mouse" mode.
   while(getPotValue() < POT_EXIT_MICE) //check if potentiometer is turned clockwise
   {
      if(radioReadyToSend)
      {//if radio is ready to send.
         if(updateRssi & RSSI_UPDATE_READ_FROM_NEXT_ACK) //if the RSSI in the last received ACK packet should be written to LCD
         {
            updtateRssiOnLcd((INT8) radioAckPkt->data[0]);//Update RSSI on LCD
            updateRssi = 0;
         }

         packetUpdated = FALSE;
         pRfTxBuffer->length = 11; //5 byte payload and 6 byte header (required by RF module)
         pRfTxBuffer->data[0] = MICE_DATA;//we are sending mice data.
         pRfTxBuffer->data[4] = 0;
         pRfTxBuffer->destAddress = dongleAddress;
         pRfTxBuffer->flag = RF_ACK_REQUEST;

         //check if joystick is moved
         switch (getJoystickDirection())
         {
         case UP :
            if(pRfTxBuffer->data[3] > 0x80) { sameDirectionCount++; }
            else {sameDirectionCount = 0;}
            pRfTxBuffer->data[3] = 0xFF - (sameDirectionCount >> 4);
            pRfTxBuffer->data[2] = 0;
            packetUpdated = TRUE;
            break;
         case DOWN :
            if((pRfTxBuffer->data[3] < 0x80) && (pRfTxBuffer->data[3] != 0)) { sameDirectionCount++; }
            else {sameDirectionCount = 0;}
            pRfTxBuffer->data[3] = 0x01 + (sameDirectionCount >> 4);
            pRfTxBuffer->data[2] = 0;
            packetUpdated = TRUE;
            break;
         case RIGHT :
            if((pRfTxBuffer->data[2] < 0x80) && (pRfTxBuffer->data[2] != 0)) { sameDirectionCount++; }
            else {sameDirectionCount = 0;}
            pRfTxBuffer->data[2] = 0x01 + (sameDirectionCount >> 4);
            pRfTxBuffer->data[3] = 0;
            packetUpdated = TRUE;
            break;
         case LEFT :
            if(pRfTxBuffer->data[2] > 0x80) { sameDirectionCount++; }
            else {sameDirectionCount = 0;}
            pRfTxBuffer->data[2] = 0xFF - (sameDirectionCount >> 4);
            pRfTxBuffer->data[3] = 0;
            packetUpdated = TRUE;
            break;
         case CENTRED :
            pRfTxBuffer->data[2] = 0;
            pRfTxBuffer->data[3] = 0;
            break;
         }

         if((pRfTxBuffer->data[1] & 0x02) && (JOYSTICK_PRESSED() == 0)) {
            pRfTxBuffer->data[1] &= ~0x02; packetUpdated = TRUE;
         }
         else if(joystickPushed()) {
            pRfTxBuffer->data[1] |= 0x02;  packetUpdated = TRUE;
         }
         if((pRfTxBuffer->data[1] & 0x01) && (BUTTON_PRESSED() == 0)) {
            pRfTxBuffer->data[1] &= ~0x01; packetUpdated = TRUE;
         }
         else if(buttonPushed()) {
            pRfTxBuffer->data[1] |= 0x01;  packetUpdated = TRUE;
         }

         if(packetUpdated)
         {
            radioReadyToSend = FALSE;
            rfruSendDataPacket(pRfTxBuffer);//send packet.
            if(updateRssi & RSSI_UPDATE_REQUEST_PACKET) { updateRssi |= RSSI_UPDATE_READ_FROM_NEXT_ACK; }
         }
         else if(updateRssi & RSSI_UPDATE_REQUEST_PACKET)//if no packet is beeing sent, but RSSI value should be updated, send a dummy packet;
         {
            updateRssi |= RSSI_UPDATE_READ_FROM_NEXT_ACK;
            pRfTxBuffer->data[0] = DUMMY_PKT;
            radioReadyToSend = FALSE;
            rfruSendDataPacket(pRfTxBuffer);//send packet.

         }
      }
      if(ackTimeout)
      {//if a packet has been sent but no ack received, try again.
         halWait(2);
         ackTimeout = FALSE;
         rfruSendDataPacket(pRfTxBuffer);
      }
   }

   t4mgrCancelCallback(timer4Index); //cancel timer4 manager callbacks to "rssiUpdate"
   rfruStopRadio();
   radioReadyToSend = TRUE;
   ackTimeout = FALSE;
}

//Function is called from timer4 interrupt (timer4 manager) each second.
void rssiUpdate(void)
{
   updateRssi |= RSSI_UPDATE_REQUEST_PACKET;//notify the loop in simpleMice that the RSSI should be updated
}

void updtateRssiOnLcd(INT8 rssiValue) {
   rssiValue /= 2;
   rssiValue -= 72;

   lcdRssiText[0] = 'R';
   lcdRssiText[1] = 'S';
   lcdRssiText[2] = 'S';
   lcdRssiText[3] = 'I';

   if(rssiValue & 0x80)
   {
      rssiValue = 0xFF - (UINT8) rssiValue;
      if(rssiValue > 99) { lcdRssiText[4] = '-'; lcdRssiText[5] = '1'; }
      else { lcdRssiText[4] = ' ';lcdRssiText[5] = '-'; }
      lcdRssiText[6] = '0' + rssiValue/10;
      lcdRssiText[7] = '0' + rssiValue%10;
      lcdRssiText[8] = 'd';
      lcdRssiText[9] = 'B';
      lcdRssiText[10] = 'm';
      lcdRssiText[11] = '\0';
   }
   else
   {
      lcdRssiText[4] = ' ';
      lcdRssiText[5] = '0' + rssiValue/10;
      lcdRssiText[6] = '0' + rssiValue%10;
      lcdRssiText[7] = 'd';
      lcdRssiText[8] = 'B';
      lcdRssiText[9] = 'm';
      lcdRssiText[10] = '\0';
   }
   lcdUpdateLine(LINE2, lcdRssiText);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -