bool Wiimote::Read() { Report rpt(MAX_PAYLOAD); auto const result = IORead(rpt.data()); if (result > 0 && m_channel > 0) { if (Core::g_CoreStartupParameter.iBBDumpPort > 0 && index == WIIMOTE_BALANCE_BOARD) { static sf::SocketUDP Socket; Socket.Send((char*)rpt.data(), rpt.size(), sf::IPAddress::LocalHost, Core::g_CoreStartupParameter.iBBDumpPort); } // Add it to queue rpt.resize(result); m_read_reports.Push(std::move(rpt)); return true; } else if (0 == result) { ERROR_LOG(WIIMOTE, "Wiimote::IORead failed. Disconnecting Wiimote %d.", index + 1); DisconnectInternal(); } return false; }
bool Wiimote::Read() { Report rpt(MAX_PAYLOAD); auto const result = IORead(rpt.data()); if (result > 0 && m_channel > 0) { if (SConfig::GetInstance().iBBDumpPort > 0 && m_index == WIIMOTE_BALANCE_BOARD) { static sf::UdpSocket Socket; Socket.send((char*)rpt.data(), rpt.size(), sf::IpAddress::LocalHost, SConfig::GetInstance().iBBDumpPort); } // Add it to queue rpt.resize(result); m_read_reports.Push(std::move(rpt)); return true; } else if (0 == result) { ERROR_LOG(WIIMOTE, "Wiimote::IORead failed. Disconnecting Wiimote %d.", m_index + 1); DisconnectInternal(); } return false; }
void mpuIntExtiInit(void) { static bool mpuExtiInitDone = false; if (mpuExtiInitDone || !mpuIntExtiConfig) { return; } #if defined(USE_MPU_DATA_READY_SIGNAL) && defined(USE_EXTI) IO_t mpuIntIO = IOGetByTag(mpuIntExtiConfig->tag); #ifdef ENSURE_MPU_DATA_READY_IS_LOW uint8_t status = IORead(mpuIntIO); if (status) { return; } #endif IOInit(mpuIntIO, OWNER_MPU, RESOURCE_EXTI, 0); IOConfigGPIO(mpuIntIO, IOCFG_IN_FLOATING); // TODO - maybe pullup / pulldown ? EXTIHandlerInit(&mpuIntCallbackRec, mpuIntExtiHandler); EXTIConfig(mpuIntIO, &mpuIntCallbackRec, NVIC_PRIO_MPU_INT_EXTI, EXTI_Trigger_Rising); EXTIEnable(mpuIntIO, true); #endif mpuExtiInitDone = true; }
static void i2cUnstick(IO_t scl, IO_t sda) { int i; int timeout = 100; IOHi(scl); IOHi(sda); IOConfigGPIO(scl, IOCFG_OUT_OD); IOConfigGPIO(sda, IOCFG_OUT_OD); for (i = 0; i < 8; i++) { // Wait for any clock stretching to finish while (!IORead(scl) && timeout) { delayMicroseconds(10); timeout--; } // Pull low IOLo(scl); // Set bus low delayMicroseconds(10); IOHi(scl); // Set bus high delayMicroseconds(10); } // Generate a start then stop condition IOLo(sda); // Set bus data low delayMicroseconds(10); IOLo(scl); // Set bus scl low delayMicroseconds(10); IOHi(scl); // Set bus scl high delayMicroseconds(10); IOHi(sda); // Set bus sda high }
static void hmc5883lConfigureDataReadyInterruptHandling(magDev_t* mag) { #ifdef USE_MAG_DATA_READY_SIGNAL if (mag->magIntExtiTag == IO_TAG_NONE) { return; } const IO_t magIntIO = IOGetByTag(mag->magIntExtiTag); #ifdef ENSURE_MAG_DATA_READY_IS_HIGH uint8_t status = IORead(magIntIO); if (!status) { return; } #endif #if defined (STM32F7) IOInit(magIntIO, OWNER_COMPASS_EXTI, 0); EXTIHandlerInit(&mag->exti, hmc5883_extiHandler); EXTIConfig(magIntIO, &mag->exti, NVIC_PRIO_MPU_INT_EXTI, IO_CONFIG(GPIO_MODE_INPUT,0,GPIO_NOPULL)); EXTIEnable(magIntIO, true); #else IOInit(magIntIO, OWNER_COMPASS_EXTI, 0); IOConfigGPIO(magIntIO, IOCFG_IN_FLOATING); EXTIHandlerInit(&mag->exti, hmc5883_extiHandler); EXTIConfig(magIntIO, &mag->exti, NVIC_PRIO_MAG_INT_EXTI, EXTI_Trigger_Rising); EXTIEnable(magIntIO, true); #endif #else UNUSED(mag); #endif }
static int ReadFromHandle(HANDLE& dev_handle, u8* buf) { OVERLAPPED hid_overlap_read = OVERLAPPED(); hid_overlap_read.hEvent = CreateEvent(nullptr, true, false, nullptr); const int read = IORead(dev_handle, hid_overlap_read, buf, 1); CloseHandle(hid_overlap_read.hEvent); return read; }
void InnerNet::ReadData(SESSION* pSession) { int nRet = IORead(pSession->nSock, pSession, pSession->oRecvBuf, this, INNERNET_MAX_RW_PEREVENT); if (nRet == -1) { CloseSession(pSession->nSessionID); return; } }
static void GetRecordData(RecordReader* reader, FILE* stream, void* buf, size_t amount) { #ifdef ESC_DEBUG assert(reader->dataIndex + amount <= reader->dataSize); #endif IOSetFilePos(stream, reader->dataOffset + reader->dataIndex); IORead(stream, buf, amount); reader->dataIndex += amount; }
bool usbCableIsInserted(void) { bool result = false; #ifdef USB_DETECT_PIN result = IORead(usbDetectPin) != 0; #endif return result; }
void detectHardwareRevision(void) { IO_t pin1 = IOGetByTag(IO_TAG(PB12)); IOInit(pin1, OWNER_SYSTEM, RESOURCE_INPUT, 1); IOConfigGPIO(pin1, IOCFG_IPU); IO_t pin2 = IOGetByTag(IO_TAG(PB13)); IOInit(pin2, OWNER_SYSTEM, RESOURCE_INPUT, 2); IOConfigGPIO(pin2, IOCFG_IPU); // Check hardware revision delayMicroseconds(10); // allow configuration to settle /* if both PB12 and 13 are tied to GND then it is Rev3A (mini) if only PB12 is tied to GND then it is a Rev3 (full size) */ if (!IORead(pin1)) { if (!IORead(pin2)) { hardwareRevision = BJF4_REV3A; } hardwareRevision = BJF4_REV3; } if (hardwareRevision == UNKNOWN) { hardwareRevision = BJF4_REV2; return; } /* enable the UART1 inversion PC9 TODO: once param groups are in place, inverter outputs can be moved to be simple IO outputs, and merely set them HI or LO in configuration. */ IO_t uart1invert = IOGetByTag(IO_TAG(PC9)); IOInit(uart1invert, OWNER_INVERTER, RESOURCE_OUTPUT, 2); IOConfigGPIO(uart1invert, IOCFG_AF_PP); IOLo(uart1invert); }
void hcsr04_extiHandler(extiCallbackRec_t* cb) { static timeUs_t timing_start; UNUSED(cb); if (IORead(echoIO) != 0) { timing_start = micros(); } else { const timeUs_t timing_stop = micros(); if (timing_stop > timing_start) { lastMeasurementReceivedAt = millis(); hcsr04SonarPulseTravelTime = timing_stop - timing_start; } } }
void detectHardwareRevision(void) { HWDetectPin = IOGetByTag(IO_TAG(HW_PIN)); IOInit(HWDetectPin, OWNER_SYSTEM, 0); IOConfigGPIO(HWDetectPin, IOCFG_IPU); // Check hardware revision delayMicroseconds(10); // allow configuration to settle if (IORead(HWDetectPin)) { hardwareRevision = AFF3_REV_1; } else { hardwareRevision = AFF3_REV_2; } }
bool Wiimote::Read() { Report rpt; rpt.first = new unsigned char[MAX_PAYLOAD]; rpt.second = IORead(rpt.first); if (rpt.second > 0 && m_channel > 0) { // Add it to queue m_read_reports.Push(rpt); return true; } delete rpt.first; return false; }
/** * Detect if a SD card is physically present in the memory slot. */ bool sdcard_isInserted(void) { bool result = true; #ifdef SDCARD_DETECT_PIN result = IORead(sdCardDetectPin) != 0; #ifdef SDCARD_DETECT_INVERTED result = !result; #endif #endif return result; }
trap_retval ReqFile_read( void ) { file_read_req *acc; file_read_ret *ret; int retval; acc = GetInPtr( 0 ); ret = GetOutPtr( 0 ); retval = IORead( acc->handle, GetOutPtr( sizeof( *ret ) ), acc->len ); if( retval < 0 ) { ret->err = retval; retval = 0; } else { ret->err = 0; } return( sizeof( *ret ) + retval ); }
static void hmc5883lConfigureDataReadyInterruptHandling(void) { #ifdef USE_MAG_DATA_READY_SIGNAL if (!(hmc5883Config->intTag)) { return; } intIO = IOGetByTag(hmc5883Config->intTag); #ifdef ENSURE_MAG_DATA_READY_IS_HIGH uint8_t status = IORead(intIO); if (!status) { return; } #endif EXTIHandlerInit(&hmc5883_extiCallbackRec, hmc5883_extiHandler); EXTIConfig(intIO, &hmc5883_extiCallbackRec, NVIC_PRIO_MAG_INT_EXTI, EXTI_Trigger_Rising); EXTIEnable(intIO, true); #endif }
//加载模块信息 void implementsetting::LoadModuleState(const string&host) { unsigned char txBuf[12]= {0},rxBuf[12]= {0}; unsigned char checkValue = 0; unsigned short *gpioBak = NULL; int len = 0; gpioBak = __ipGpioBak[host]; txBuf[0] = 'R'; for(int i =1; i<11; i++) { checkValue^=txBuf[i]; } txBuf[11] = checkValue; IOWrite(host, (char*)txBuf, (int)sizeof(txBuf)); len = (int)sizeof(txBuf); IORead(host, (char*)rxBuf, &len); if(rxBuf[0] != 'R')return; checkValue = 0; for(int i =1; i<11; i++) { checkValue^=rxBuf[i]; } //if(checkValue != rxBuf[11])return;//此处不要检验 for(int i =1,j=0; i<11; j++) { gpioBak[j] = rxBuf[i]; i++; gpioBak[j] += rxBuf[i]*256; i++; } }
static void i2cUnstick(IO_t scl, IO_t sda) { int i; IOHi(scl); IOHi(sda); IOConfigGPIO(scl, IOCFG_OUT_OD); IOConfigGPIO(sda, IOCFG_OUT_OD); // Clock out, with SDA high: // 7 data bits // 1 READ bit // 1 cycle for the ACK for (i = 0; i < (LEN_ADDR + LEN_RW + LEN_ACK); i++) { // Wait for any clock stretching to finish int timeout = UNSTICK_CLK_STRETCH; while (!IORead(scl) && timeout) { delayMicroseconds(UNSTICK_CLK_US); timeout--; } // Pull low IOLo(scl); // Set bus low delayMicroseconds(UNSTICK_CLK_US/2); IOHi(scl); // Set bus high delayMicroseconds(UNSTICK_CLK_US/2); } // Generate a stop condition in case there was none IOLo(scl); delayMicroseconds(UNSTICK_CLK_US/2); IOLo(sda); delayMicroseconds(UNSTICK_CLK_US/2); IOHi(scl); // Set bus scl high delayMicroseconds(UNSTICK_CLK_US/2); IOHi(sda); // Set bus sda high }
bool hcsr04Detect(rangefinderDev_t *dev, const sonarConfig_t * rangefinderHardwarePins) { bool detected = false; #ifdef STM32F10X // enable AFIO for EXTI support RCC_ClockCmd(RCC_APB2(AFIO), ENABLE); #endif #if defined(STM32F3) || defined(STM32F4) RCC_ClockCmd(RCC_APB2(SYSCFG), ENABLE); // XXX Do we need this? #endif triggerIO = IOGetByTag(rangefinderHardwarePins->triggerTag); echoIO = IOGetByTag(rangefinderHardwarePins->echoTag); if (IOGetOwner(triggerIO) != OWNER_FREE) { return false; } if (IOGetOwner(echoIO) != OWNER_FREE) { return false; } // trigger pin IOInit(triggerIO, OWNER_SONAR_TRIGGER, 0); IOConfigGPIO(triggerIO, IOCFG_OUT_PP); IOLo(triggerIO); delay(100); // echo pin IOInit(echoIO, OWNER_SONAR_ECHO, 0); IOConfigGPIO(echoIO, IOCFG_IN_FLOATING); // HC-SR04 echo line should be low by default and should return a response pulse when triggered if (IORead(echoIO) == false) { for (int i = 0; i < 5 && !detected; i++) { timeMs_t requestTime = millis(); hcsr04_start_reading(); while ((millis() - requestTime) < HCSR04_MinimumFiringIntervalMs) { if (IORead(echoIO) == true) { detected = true; break; } } } } if (detected) { // Hardware detected - configure the driver #ifdef USE_EXTI EXTIHandlerInit(&hcsr04_extiCallbackRec, hcsr04_extiHandler); EXTIConfig(echoIO, &hcsr04_extiCallbackRec, NVIC_PRIO_SONAR_EXTI, EXTI_Trigger_Rising_Falling); // TODO - priority! EXTIEnable(echoIO, true); #endif dev->delayMs = 100; dev->maxRangeCm = HCSR04_MAX_RANGE_CM; dev->detectionConeDeciDegrees = HCSR04_DETECTION_CONE_DECIDEGREES; dev->detectionConeExtendedDeciDegrees = HCSR04_DETECTION_CONE_EXTENDED_DECIDEGREES; dev->init = &hcsr04_init; dev->update = &hcsr04_update; dev->read = &hcsr04_get_distance; return true; } else { // Not detected - free resources IORelease(triggerIO); IORelease(echoIO); return false; } }
//执行命令 bool implementsetting::ExcuteCmd(const string&ip,const string&sw,int chan) { string str = __ioInfoMap[sw][chan-1]; //mingw编译不通过,改为拷贝赋值 vector<string> gpioValueStr = split(str,","); unsigned short *gpioBak = __ipGpioBak[ip]; for(int i =0; i<5; i++) { gpioBak[i]|=atol(gpioValueStr[i].c_str()); } __actionList[ip] = gpioBak; unsigned char txBuf[12] = {0}; unsigned char checkValue = 0; txBuf[0]='W'; for(int i =1,j=0; i<11; j++) { txBuf[i] = gpioBak[j]%256; checkValue^=txBuf[i]; i++; txBuf[i] = gpioBak[j]/256; checkValue^=txBuf[i]; i++; } txBuf[11] = checkValue; #ifdef __DEBUG_PRINT cout<<sw<<"<"<<ip<<">"<<str<<endl; unsigned short tmpItems; for( int i=0; i<5; i++ ) { tmpItems = *(unsigned short*)&txBuf[i*2+1]; bitset<16> bs(tmpItems); cout<<hex<<tmpItems<<"-"<<bs<<" "; if(i%4==0)cout<<endl; } //cout<<endl<<endl; for(int i=0; i<12; i++) { cout<<hex<<static_cast<int>(txBuf[i])<<" "; } cout<<endl; #endif bool funcResult = true; bool wrResult=false; int len = 12; unsigned char rxBuf[12] = {0}; for(int i=0; i<3; i++) { wrResult = IOWrite(ip,(char*)txBuf,sizeof(txBuf)); if(wrResult == false) { funcResult = false; break; } IORead(ip,(char*)rxBuf,&len); if(len == sizeof(txBuf)) { for(int j=0; j<sizeof(txBuf); j++) if(rxBuf[j] != txBuf[j]) wrResult = false; } else wrResult = false; if(wrResult == true) break; else funcResult =false; //Delay(100); } return funcResult; }
void hmc5883lInit(void) { int16_t magADC[3]; int i; int32_t xyz_total[3] = { 0, 0, 0 }; // 32 bit totals so they won't overflow. bool bret = true; // Error indicator #ifdef USE_MAG_DATA_READY_SIGNAL if (hmc5883Config && hmc5883Config->io) { intIO = IOGetByTag(hmc5883Config->io); IOInit(intIO, OWNER_SYSTEM, RESOURCE_INPUT | RESOURCE_EXTI); IOConfigGPIO(intIO, IOCFG_IN_FLOATING); } #endif delay(50); i2cWrite(HMC5883L_I2C_INSTANCE, MAG_ADDRESS, HMC58X3_R_CONFA, 0x010 + HMC_POS_BIAS); // Reg A DOR = 0x010 + MS1, MS0 set to pos bias // Note that the very first measurement after a gain change maintains the same gain as the previous setting. // The new gain setting is effective from the second measurement and on. i2cWrite(HMC5883L_I2C_INSTANCE, MAG_ADDRESS, HMC58X3_R_CONFB, 0x60); // Set the Gain to 2.5Ga (7:5->011) delay(100); hmc5883lRead(magADC); for (i = 0; i < 10; i++) { // Collect 10 samples i2cWrite(HMC5883L_I2C_INSTANCE, MAG_ADDRESS, HMC58X3_R_MODE, 1); delay(50); hmc5883lRead(magADC); // Get the raw values in case the scales have already been changed. // Since the measurements are noisy, they should be averaged rather than taking the max. xyz_total[X] += magADC[X]; xyz_total[Y] += magADC[Y]; xyz_total[Z] += magADC[Z]; // Detect saturation. if (-4096 >= MIN(magADC[X], MIN(magADC[Y], magADC[Z]))) { bret = false; break; // Breaks out of the for loop. No sense in continuing if we saturated. } LED1_TOGGLE; } // Apply the negative bias. (Same gain) i2cWrite(HMC5883L_I2C_INSTANCE, MAG_ADDRESS, HMC58X3_R_CONFA, 0x010 + HMC_NEG_BIAS); // Reg A DOR = 0x010 + MS1, MS0 set to negative bias. for (i = 0; i < 10; i++) { i2cWrite(HMC5883L_I2C_INSTANCE, MAG_ADDRESS, HMC58X3_R_MODE, 1); delay(50); hmc5883lRead(magADC); // Get the raw values in case the scales have already been changed. // Since the measurements are noisy, they should be averaged. xyz_total[X] -= magADC[X]; xyz_total[Y] -= magADC[Y]; xyz_total[Z] -= magADC[Z]; // Detect saturation. if (-4096 >= MIN(magADC[X], MIN(magADC[Y], magADC[Z]))) { bret = false; break; // Breaks out of the for loop. No sense in continuing if we saturated. } LED1_TOGGLE; } magGain[X] = fabsf(660.0f * HMC58X3_X_SELF_TEST_GAUSS * 2.0f * 10.0f / xyz_total[X]); magGain[Y] = fabsf(660.0f * HMC58X3_Y_SELF_TEST_GAUSS * 2.0f * 10.0f / xyz_total[Y]); magGain[Z] = fabsf(660.0f * HMC58X3_Z_SELF_TEST_GAUSS * 2.0f * 10.0f / xyz_total[Z]); // leave test mode i2cWrite(HMC5883L_I2C_INSTANCE, MAG_ADDRESS, HMC58X3_R_CONFA, 0x70); // Configuration Register A -- 0 11 100 00 num samples: 8 ; output rate: 15Hz ; normal measurement mode i2cWrite(HMC5883L_I2C_INSTANCE, MAG_ADDRESS, HMC58X3_R_CONFB, 0x20); // Configuration Register B -- 001 00000 configuration gain 1.3Ga i2cWrite(HMC5883L_I2C_INSTANCE, MAG_ADDRESS, HMC58X3_R_MODE, 0x00); // Mode register -- 000000 00 continuous Conversion Mode delay(100); if (!bret) { // Something went wrong so get a best guess magGain[X] = 1.0f; magGain[Y] = 1.0f; magGain[Z] = 1.0f; } #ifdef USE_MAG_DATA_READY_SIGNAL do { if (!(hmc5883Config && intIO)) break; # ifdef ENSURE_MAG_DATA_READY_IS_HIGH if (!IORead(intIO)) break; # endif EXTIHandlerInit(&hmc5883_extiCallbackRec, hmc5883_extiHandler); EXTIConfig(intIO, &hmc5883_extiCallbackRec, NVIC_PRIO_MAG_INT_EXTI, EXTI_Trigger_Rising); EXTIEnable(intIO, true); } while (0); #endif }
//执行动作 bool implementsetting::Excute(void) { if (!bLoadMap) { Message("SWITCH::Excute failed::load map failed!"); return false; } bool funcResult = true; unsigned char txBuf[12] = { 0 }, rxBuf[12] = {0}; unsigned char checkValue = 0; unsigned short *gpioBak = NULL; string ip; int replyCnt = 0; //尝试次数 map<string, bool>hostIPEnableList; vector<string> actionQueue; txBuf[0]='W'; //读入主机使能队列 /* for (map<string, stHostControl>::iterator itr = __hostCtrl.begin(); itr != __hostCtrl.end(); itr++) { hostIPEnableList[itr->second.ip] = itr->second.enable; }*/ for (map<string, string>::iterator itr = __ipmap.begin(); itr != __ipmap.end(); itr++) { hostIPEnableList[itr->second] = true; } for (map<string, unsigned short*>::iterator actionIter = __actionList.begin(); actionIter != __actionList.end(); actionIter++) { if (!hostIPEnableList[actionIter->first])continue; actionQueue.push_back(actionIter->first); } replyCnt = 0; while (true) { //下发开关指令队列 for (map<string, unsigned short*>::iterator actionIter = __actionList.begin(); actionIter != __actionList.end(); actionIter++) { ip = actionIter->first; gpioBak = actionIter->second; if (!hostIPEnableList[ip])continue; checkValue = 0; for (int i = 1, j = 0; i < 11; j++) { txBuf[i] = gpioBak[j] % 256; checkValue ^= txBuf[i]; i++; txBuf[i] = gpioBak[j] / 256; checkValue ^= txBuf[i]; i++; } txBuf[11] = checkValue; #ifdef __DEBUG_PRINT cout<<"<"<<ip<<">"; for(int i=0; i<12; i++) { cout<<uppercase<<hex<<static_cast<int>(txBuf[i])<<" "; } cout<<endl; #endif bool wrResult = false; int len = 12; for (int i = 0; i < 1; i++) { funcResult = wrResult = IOWrite(ip, (char*)txBuf, sizeof(txBuf)); if (wrResult == false)continue; funcResult = wrResult = IORead(ip, (char*)rxBuf, &len); if (wrResult == false)continue; for (int j = 0; j < sizeof(txBuf); j++) { if (rxBuf[j] != txBuf[j]) { wrResult = false; break; } } funcResult = wrResult; if (wrResult == true)break; } if (!funcResult)break; } //break; if (replyCnt > 1 || funcResult)break; replyCnt++; //断开连接 for (vector<string>::iterator itr = actionQueue.begin(); itr != actionQueue.end(); itr++) { IODisConnect(*itr); } //复位 funcResult = IOResetWithList(actionQueue); //重连 for (vector<string>::iterator itr = actionQueue.begin(); itr != actionQueue.end(); itr++) { IOConnectBegin(*itr); } bool wait = true; funcResult = true; vector<string> errHost; for (vector<string>::iterator itr = actionQueue.begin(); itr != actionQueue.end(); itr++) { if (!IOConnectEnd(*itr, wait ? 1000 : 100)) { errHost.push_back(*itr); funcResult = false; break; } wait = false; //if (!funcResult)break; //LoadModuleState(*itr); } if (!funcResult) { string info("<MatrixSwitch>:["); for (vector<string>::iterator itr = errHost.begin(); itr != errHost.end(); itr++) { for (map<string,string>::iterator mtr = __ipmap.begin(); mtr != __ipmap.end(); mtr++) { if (mtr->second == *itr)info.append(mtr->first + '@'); } info.append(*itr+'/'); } info.append("]:<Failed>"); Message(info); break; } Delay(500); } return funcResult; }