uint32_t SystemInit(void) { uint32_t result; uint32_t unixTime; SetRebootFlag(); LedInit(); //DisplayStart(); RTC_WDT_Init(); InitNetwork(); GateInit(); InitBuff(); BattADC_Init(); /* Enable global interrupts */ CyGlobalIntEnable; /*sync real time*/ unixTime = DS1307_GetUnixTime(); // unixTime = 1485184625 ; if(unixTime > 0) { RTC_SetUnixTime(unixTime); result = DataBaseStart(); if(result == DB_NO_ERROR) { Display("System init..."); result = NO_ERROR; } else { Display("Please insert SD"); result = ERROR; SendFinStatus(FIN_NO_READY); MyDelay(300); } } else { Display("Error sync time"); result = ERROR; SendFinStatus(FIN_NO_READY); MyDelay(500); } return result; }
/******************************************************************************* * Function Name: AppCallBack ******************************************************************************** * * Summary: * This is an event callback function to receive events from the CYBLE Component. * * Parameters: * uint8 event: Event from the CYBLE component. * void* eventParams: A structure instance for corresponding event type. The * list of event structure is described in the component * datasheet. * * Return: * None * *******************************************************************************/ void AppCallBack(uint32 event, void* eventParam) { CYBLE_API_RESULT_T apiResult; CYBLE_GATTS_WRITE_REQ_PARAM_T *wrReqParam; switch (event) { /********************************************************** * General Events ***********************************************************/ case CYBLE_EVT_STACK_ON: /* This event is received when component is Started */ /* Enter into discoverable mode so that remote can search it. */ apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST); if(apiResult != CYBLE_ERROR_OK) { //ShowError(); } printf("CYBLE_EVT_STACK_ON\n"); break; case CYBLE_EVT_TIMEOUT: printf("CYBLE_EVT_TIMEOUT\n"); break; case CYBLE_EVT_HARDWARE_ERROR: /* This event indicates that some internal HW error has occurred. */ //ShowError(); break; /********************************************************** * GAP Events ***********************************************************/ case CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP: printf("CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP\n"); if(CYBLE_STATE_DISCONNECTED == CyBle_GetState()) { /* Fast and slow advertising period complete, go to low power * mode (Hibernate mode) and wait for an external * user event to wake up the device again */ // Advertising_LED_Write(LED_OFF); // Disconnect_LED_Write(LED_ON); // SW2_ClearInterrupt(); // Wakeup_Interrupt_ClearPending(); // Wakeup_Interrupt_Start(); CySysPmHibernate(); } break; case CYBLE_EVT_GAP_DEVICE_CONNECTED: printf("CYBLE_EVT_GAP_DEVICE_CONNECTED\n"); // Disconnect_LED_Write(LED_OFF); // Advertising_LED_Write(LED_OFF); break; case CYBLE_EVT_GAP_DEVICE_DISCONNECTED: /* Put the device to discoverable mode so that remote can search it. */ printf("CYBLE_EVT_GAP_DEVICE_DISCONNECTED\n"); apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST); if(apiResult != CYBLE_ERROR_OK) { //ShowError(); } break; case CYBLE_EVT_GAPC_CONNECTION_UPDATE_COMPLETE: break; /********************************************************** * GATT Events ***********************************************************/ case CYBLE_EVT_GATT_CONNECT_IND: printf("CYBLE_EVT_GATT_CONNECT_IND\n"); break; case CYBLE_EVT_GATT_DISCONNECT_IND: printf("CYBLE_EVT_GATT_DISCONNECT_IND\n"); // Reset the rotation notification _rotationCCCDValue[0] = 0; _voltageCCCDValue[0] = 0; _drawCCCDValue[0] = 0; break; case CYBLE_EVT_GATTS_WRITE_REQ: printf("CYBLE_EVT_GATTS_WRITE_REQ\n"); wrReqParam = (CYBLE_GATTS_WRITE_REQ_PARAM_T *) eventParam; /* Write request for time/date */ if(wrReqParam->handleValPair.attrHandle == CYBLE_POVDISPLAY_TIME_CHAR_HANDLE) { /* only update the value and write the response if the requested write is allowed */ if(CYBLE_GATT_ERR_NONE == CyBle_GattsWriteAttributeValue(&wrReqParam->handleValPair, 0, &cyBle_connHandle, CYBLE_GATT_DB_PEER_INITIATED)) { uint64_t datetime = wrReqParam->handleValPair.value.val[0]; datetime <<= 8; datetime |= wrReqParam->handleValPair.value.val[1]; datetime <<= 8; datetime |= wrReqParam->handleValPair.value.val[2]; datetime <<= 8; datetime |= wrReqParam->handleValPair.value.val[3]; printf("Date time EPOCH: %08x\n", (unsigned int)(datetime >> 32)); printf("Date time EPOCH: %08x\n", (unsigned int)datetime); RTC_SetUnixTime(datetime); } } /* Write request for time/date */ if(wrReqParam->handleValPair.attrHandle == CYBLE_POVDISPLAY_FILTERGAIN_CHAR_HANDLE) { /* only update the value and write the response if the requested write is allowed */ if(CYBLE_GATT_ERR_NONE == CyBle_GattsWriteAttributeValue(&wrReqParam->handleValPair, 0, &cyBle_connHandle, CYBLE_GATT_DB_PEER_INITIATED)) { _rotationFilterGain = wrReqParam->handleValPair.value.val[0]; _rotationFilterGain <<= 8; _rotationFilterGain |= wrReqParam->handleValPair.value.val[1]; _rotationFilterGain <<= 8; _rotationFilterGain |= wrReqParam->handleValPair.value.val[2]; _rotationFilterGain <<= 8; _rotationFilterGain |= wrReqParam->handleValPair.value.val[3]; } } if(wrReqParam->handleValPair.attrHandle == CYBLE_POVDISPLAY_DRAWOFFSET_CHAR_HANDLE) { /* only update the value and write the response if the requested write is allowed */ if(CYBLE_GATT_ERR_NONE == CyBle_GattsWriteAttributeValue(&wrReqParam->handleValPair, 0, &cyBle_connHandle, CYBLE_GATT_DB_PEER_INITIATED)) { _drawOffset = wrReqParam->handleValPair.value.val[0]; _drawOffset <<= 8; _drawOffset |= wrReqParam->handleValPair.value.val[1]; } } if(wrReqParam->handleValPair.attrHandle == CYBLE_POVDISPLAY_ROTATIONSPEED_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE) { /* only update the value and write the response if the requested write is allowed */ if(wrReqParam->handleValPair.value.val[CYBLE_POVDISPLAY_ROTATIONSPEED_CHARACTERISTIC_USER_DESCRIPTION_DESC_INDEX] == 1) { _rotationCCCDValue[0] = 1; } else { _rotationCCCDValue[0] = 0; } /* Update CCCD handle with notification status data*/ _rotationNotificationCCCDHandle.attrHandle = CYBLE_POVDISPLAY_ROTATIONSPEED_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE; _rotationNotificationCCCDHandle.value.val = _rotationCCCDValue; _rotationNotificationCCCDHandle.value.len = 2; /* Report data to BLE component for sending data when read by Central device */ CyBle_GattsWriteAttributeValue(&_rotationNotificationCCCDHandle, 0, &cyBle_connHandle, CYBLE_GATT_DB_PEER_INITIATED); } if(wrReqParam->handleValPair.attrHandle == CYBLE_POVDISPLAY_VOLTAGE_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE) { /* only update the value and write the response if the requested write is allowed */ if(wrReqParam->handleValPair.value.val[CYBLE_POVDISPLAY_VOLTAGE_CHARACTERISTIC_USER_DESCRIPTION_DESC_INDEX] == 1) { _voltageCCCDValue[0] = 1; } else { _voltageCCCDValue[0] = 0; } /* Update CCCD handle with notification status data*/ _voltageNotificationCCCDHandle.attrHandle = CYBLE_POVDISPLAY_VOLTAGE_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE; _voltageNotificationCCCDHandle.value.val = _voltageCCCDValue; _voltageNotificationCCCDHandle.value.len = 2; /* Report data to BLE component for sending data when read by Central device */ CyBle_GattsWriteAttributeValue(&_voltageNotificationCCCDHandle, 0, &cyBle_connHandle, CYBLE_GATT_DB_PEER_INITIATED); } if(wrReqParam->handleValPair.attrHandle == CYBLE_POVDISPLAY_DRAWTIME_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE) { /* only update the value and write the response if the requested write is allowed */ if(wrReqParam->handleValPair.value.val[CYBLE_POVDISPLAY_DRAWTIME_CHARACTERISTIC_USER_DESCRIPTION_DESC_INDEX] == 1) { _drawCCCDValue[0] = 1; } else { _drawCCCDValue[0] = 0; } /* Update CCCD handle with notification status data*/ _drawNotificationCCCDHandle.attrHandle = CYBLE_POVDISPLAY_DRAWTIME_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE; _drawNotificationCCCDHandle.value.val = _drawCCCDValue; _drawNotificationCCCDHandle.value.len = 2; /* Report data to BLE component for sending data when read by Central device */ CyBle_GattsWriteAttributeValue(&_drawNotificationCCCDHandle, 0, &cyBle_connHandle, CYBLE_GATT_DB_PEER_INITIATED); } CyBle_GattsWriteRsp(cyBle_connHandle); break; /********************************************************** * Other Events ***********************************************************/ case CYBLE_EVT_STACK_BUSY_STATUS: /* This event is generated when the internal stack buffer is full and no more * data can be accepted or the stack has buffer available and can accept data. * This event is used by application to prevent pushing lot of data to stack. */ /* Extract the present stack status */ _busyStatus = * (uint8*)eventParam; break; default: break; }