//set CE low if mode == 0, else set CE high inline void setMode(uint8_t mode) { if (mode) { activeMode(); } else { standbyMode(); } }
void setup() { i2c_begin(); standbyMode(); //register settings must be made in standby mode regWrite(REG_XYZ_DATA_CFG, FULL_SCALE_RANGE_2g); hiResMode(); //this is the default setting and can be omitted. //lowResMode(); //set to low res (fast mode), must use getAccXYZ(,,,false) to retrieve readings. activeMode(); byte b; //regRead(REG_WHO_AM_I, &b); }
__interrupt void P1_ISR(void) { //HM-10 - Disable pin interrupts and re-enable UART HAL_ENTER_ISR(); P1IFG = 0; P1IF = 0; uint8 pV = P1_7; if(pV) { //active low activeMode(); } CLEAR_SLEEP_MODE(); HAL_EXIT_ISR(); }
__interrupt void P0_ISR(void) { //HM-11 - Disable pin interrupts and re-enable UART HAL_ENTER_ISR(); //clear interrupt P0IFG = 0; P0IF = 0; uint8 pV = P0_2; if(pV == 0) { activeMode(); } CLEAR_SLEEP_MODE(); HAL_EXIT_ISR(); }
/* Command sets, chosen options need to be stored in non-volatile memory - SC,<value> + set connection mode of device + <value> is a single digit number + 1 Do not auto-connect to last paired, 2 auto-connect to last paired - SN,<value> + set device name + <value> device's new name - S,R Reset the device - S,D Set device to be discoverable - S,DC Disconnect device from host */ static void processCommands(void) { //buf: Testing variables uint8 *buf = rxBuffer; buf[rxBufferIndex] = 0; if(rxBuffer[0] == 'K') { //keyboard commands if(rxBufferIndex == 3) { //printf("Keyboard key press and release\r\n"); if(rxBuffer[1] == 'U') { //Key released KBD_Report_RemoveKey(rxBuffer[2]); } else if(rxBuffer[1] == 'D') { //Key pressed KBD_Report_AddKey(rxBuffer[2]); } } else { //command as KS maybe, for Keyboard-report Send //printf("Sending report...\r\n"); KBD_Report_Update(); } } else if(rxBuffer[0] == 'M') { //mouse commands //printf("Mouse commands\r\n"); hidKbdMouseSendMouseReport(rxBuffer[1], rxBuffer[2], rxBuffer[3], rxBuffer[4]); } else if(rxBuffer[0] == 'S') { //setting commands //printf("Setting commands\r\n"); if((rxBuffer[1] == 'C') && (rxBuffer[2] == ',')) { //TO-DO: SET CONNECTION MODE //printf("Connection modes\r\n"); } else if((rxBuffer[1] == 'N') && (rxBuffer[2] == ',')) { uint8 i; uint8 deviceNewName[20]; uint8 deviceNewNameLength; uint8 deviceNewNameCRC; deviceNewNameLength = rxBufferIndex-3; if(deviceNewNameLength > 20) { printf("Name exceeds permitted length\r\n"); } else { for(i = 3; i < rxBufferIndex; i++) { deviceNewName[i-3] = rxBuffer[i]; } deviceNewName[deviceNewNameLength] = '\0'; deviceNewNameCRC = getCRC(deviceNewName, deviceNewNameLength); osal_snv_write(SNV_ID_DEVICE_NAME, 20, deviceNewName); osal_snv_write(SNV_ID_DEVICE_NAME_LENGTH, 1, &deviceNewNameLength); osal_snv_write(SNV_ID_DEVICE_NAME_CRC, 1, &deviceNewNameCRC); // printf("Name is being set, reset to set new name\r\n"); } } else if((rxBuffer[1] == ',') && (rxBuffer[2] == 'R')) { //reset the device HAL_SYSTEM_RESET(); } else if((rxBuffer[1] == ',') && (rxBuffer[2] == 'D')) { if(rxBuffer[3] == 'C') { //disconnect the device from host //printf("Disconnecting from host...\r\n"); GAPRole_TerminateConnection(); } else { //set device to be discoverable //printf("Set deveice to be discoverable\r\n"); } } else if((rxBuffer[1] == ',') && (rxBuffer[2] == 'S')) { //enable sleep mode printf("SLEEP\r\n"); sleepModeEnabled = 1; sleepMode(); } else if((rxBuffer[1] == ',') && (rxBuffer[2] == 'A')) { //disable sleep mode printf("ACTIVE\r\n"); sleepModeEnabled = 0; activeMode(); } } //after processing, reset rxBuffer and its index memset(rxBuffer, 0, 8); rxBufferIndex = 0; }