// Application entry point void appMain(void) { // setup our radio packet buffer radioPacketBuffer = (RadioPacketBuffer_t *) &realBuf; // turn on radio listening radioOn(); for (;;) { if (isRadioPacketError()) { // error occured last time the packet was received PRINTF("radio receive failed, error code %u\n", (uint16_t) -radioPacketBuffer->receivedLength); } else if (isRadioPacketReceived()) { // a packet is received from the radio PRINTF("received %u byte packet\n", (uint16_t) radioPacketBuffer->receivedLength); // dump the contents of the buffer debugHexdump(radioPacketBuffer->buffer, radioPacketBuffer->receivedLength); } // reset our radio buffer in any case radioBufferReset(); // do nothing for a while msleep(1000); } }
// for debugging // static int amb8420GetAll(void) { bool ok; uint8_t crc; Handle_t handle; const uint8_t position = 0; const uint8_t txLen = 0x80; // Wait for device to become ready AMB8420_WAIT_FOR_RTS_READY(ok); if (!ok) goto end; // ATOMIC_START_TIMESAVE(handle); AMB8420_SERIAL_CAPTURE(ok); commandInProgress = AMB8420_CMD_GET_REQ; serialSendByte(AMB8420_UART_ID, AMB8420_START_DELIMITER); serialSendByte(AMB8420_UART_ID, AMB8420_CMD_GET_REQ); serialSendByte(AMB8420_UART_ID, 2); serialSendByte(AMB8420_UART_ID, position); serialSendByte(AMB8420_UART_ID, txLen); crc = AMB8420_START_DELIMITER ^ AMB8420_CMD_GET_REQ ^ 2 ^ position ^ txLen; serialSendByte(AMB8420_UART_ID, crc); AMB8420_SERIAL_FREE(); // ATOMIC_END_TIMESAVE(handle); // wait for reply // if someone grabs access to serial during this waiting, // then it will fail, but that's ok, as the caller will retry. INTERRUPT_ENABLED_START(handle); AMB8420_BUSYWAIT_UNTIL(!commandInProgress, TIMER_100_MS, ok); INTERRUPT_ENABLED_END(handle); #if RADIO_DEBUG PRINTF("recvLength=%u\n", recvLength); if (recvLength > 2 && rxBuffer[0] == position && rxBuffer[1] == txLen) { debugHexdump(rxBuffer + 2, recvLength - 2); } #endif // Wait for device to become ready AMB8420_WAIT_FOR_RTS_READY(ok); end: return ok ? 0 : -EBUSY; }
void midiSendLong(unsigned char *buf, unsigned long len) { MIDIHDR midiHdr; HANDLE hBuffer; hBuffer = GlobalAlloc(GHND, len); if (!hBuffer) { logPrintf(LOG_ERROR, "error allocating buffer for sysex\n"); return; } midiHdr.lpData = (LPBYTE)GlobalLock(hBuffer); if (midiHdr.lpData) { midiHdr.dwBufferLength = len; midiHdr.dwFlags = 0; debugPrintf(2, "midiSendLong: \n"); debugHexdump(2, buf, len); UINT err = midiOutPrepareHeader(outHandle, &midiHdr, sizeof(MIDIHDR)); if (!err) { memcpy(midiHdr.lpData, buf, len); err = midiOutLongMsg(outHandle, &midiHdr, sizeof(MIDIHDR)); if (err) { char errBuf[256]; midiOutGetErrorText(err, errBuf, sizeof(errBuf)); logPrintf(LOG_ERROR, "error sending long message: %s\n", errBuf); } while (MIDIERR_STILLPLAYING == midiOutUnprepareHeader(outHandle, &midiHdr, sizeof(MIDIHDR))) { ; } debugPrintf(2, "midiSendLong finished\n"); } } GlobalUnlock(hBuffer); GlobalFree(hBuffer); }
static int amb8420Get1b(uint8_t position, uint8_t *value) { bool ok; uint8_t crc; Handle_t handle; uint8_t rxLen; uint8_t rxPosition; bool hasRxAnswer = false; // const uint8_t position = 0; const uint8_t txLen = 0x1; // Wait for device to become ready AMB8420_WAIT_FOR_RTS_READY(ok); if (!ok) goto end; // ATOMIC_START_TIMESAVE(handle); AMB8420_SERIAL_CAPTURE(ok); commandInProgress = AMB8420_CMD_GET_REQ; serialSendByte(AMB8420_UART_ID, AMB8420_START_DELIMITER); serialSendByte(AMB8420_UART_ID, AMB8420_CMD_GET_REQ); serialSendByte(AMB8420_UART_ID, 2); serialSendByte(AMB8420_UART_ID, position); serialSendByte(AMB8420_UART_ID, txLen); crc = AMB8420_START_DELIMITER ^ AMB8420_CMD_GET_REQ ^ 2 ^ position ^ txLen; serialSendByte(AMB8420_UART_ID, crc); AMB8420_SERIAL_FREE(); // ATOMIC_END_TIMESAVE(handle); // wait for reply // if someone grabs access to serial during this waiting, // then it will fail, but that's ok, as the caller will retry. INTERRUPT_ENABLED_START(handle); AMB8420_BUSYWAIT_UNTIL(!commandInProgress, TIMER_100_MS, ok); INTERRUPT_ENABLED_END(handle); #if RADIO_DEBUG PRINTF("recvLength=%u\n", recvLength); PRINTF("%02x %02x %02x\n", rxBuffer[0], rxBuffer[1], rxBuffer[2]); #endif if (recvLength != 3) { #if RADIO_DEBUG if (recvLength > 3) { debugHexdump(rxBuffer + 2, recvLength - 2); } #endif goto end; } rxPosition = rxBuffer[0]; rxLen = rxBuffer[1]; if (rxPosition != position || rxLen != 1) { goto end; } hasRxAnswer = true; *value = rxBuffer[2]; // Wait for device to become ready AMB8420_WAIT_FOR_RTS_READY(ok); end: if (!ok) return -EBUSY; if (!hasRxAnswer) return -EIO; return 0; }
// -------------------------------------------- // -------------------------------------------- void onRadioRecv(void) { static bool flRxProcessing=false; if(flRxProcessing){ #ifdef PRINT_PACKETS PRINTF("RX Locked\n"); #endif return; } flRxProcessing=true; // There is a chance for a small race condition #ifdef PRINT_PACKETS uint32_t rxTime = getTimeMs(); #endif int16_t rxLen; rssi_t rssi; lqi_t lqi; rxIdx++; if( rxIdx < 0 ) rxIdx=0; led1Toggle(); // rxLen = radioRecv( &(DB_REC(db)), DB_REC_SIZE(db)); // DB_REC(db).recLen = rxLen; rxLen = radioRecv(&radioBuffer, sizeof(radioBuffer)); rssi = radioGetLastRSSI(); lqi = radioGetLastLQI(); #ifdef PRINT_PACKETS PRINTF("%d\t%d\t%d\t%d\t%ld\t", (int)rxIdx, (int)rxLen, (int)rssi, (int)lqi, (long)rxTime); #endif #ifdef PRINT_PACKETS if (rxLen < 0) { PRINTF("RX failed\n"); } else if (rxLen > 0 ) { debugHexdump((uint8_t *) &radioBuffer, rxLen); // debugHexdump((uint8_t *) &(DB_REC(db)), rxLen); } #endif if (rxLen < 0) { led2Toggle(); flRxProcessing=false; return; } if( ! MSG_SIGNATURE_OK(radioBuffer) ) { flRxProcessing = false; return; } // Anticipated payload types. MSG_NEW_PAYLOAD_PTR(radioBuffer, phaser_ping_t, test_data_p); MSG_NEW_PAYLOAD_PTR(radioBuffer, phaser_control_t, ctrl_data_p); MSG_NEW_PAYLOAD_PTR(radioBuffer, msg_text_data_t, msg_text_p); MSG_NEW_PAYLOAD_PTR(radioBuffer, test_config_t, test_config_p); int act = MSG_ACT_CLEAR; bool flOK=true; switch( radioBuffer.id ){ case PH_MSG_Test: MSG_CHECK_FOR_PAYLOAD(radioBuffer, phaser_ping_t, flOK=false ); if( !flOK ){ PRINTF("BadChk\n"); break; } // Check if new experiment iteration started. if(lastExpIdx != test_data_p->expIdx && curExp){ sendTestResults(); } processTestMsg(test_data_p, rssi, lqi); break; case PH_MSG_Angle: if(curExp) sendTestResults(); if( flRestart ){ // Best time to resend the restart message after the angle change send_ctrl_msg(MSG_ACT_RESTART); flRestart = false; } break; case PH_MSG_Control: MSG_CHECK_FOR_PAYLOAD(radioBuffer, phaser_control_t, break); if(curExp) sendTestResults(); act = ctrl_data_p->action; if(act == MSG_ACT_START ){ flRestart = false; // Clear restart command attempt } printAction(act); break; case PH_MSG_Text: MSG_CHECK_FOR_PAYLOAD(radioBuffer, msg_text_data_t, break ); PRINTF(msg_text_p->text); PRINTF("\n"); break; case PH_MSG_Config: MSG_CHECK_FOR_PAYLOAD(radioBuffer, test_config_t, break ); PRINTF("Config received:\n"); // TODO: parse the config and print print_test_config(test_config_p); } flRxProcessing=false; }