int main(void) { int fileHandleGPIO_LED; int fileHandleGPIO_5; int fileHandleGPIO_6; int fileHandleGPIO_7; int i=0; puts("Starting LED blink GP_LED - gpio-3 on Galileo board."); fileHandleGPIO_LED = openGPIO(GP_LED, GPIO_DIRECTION_OUT); fileHandleGPIO_5 = openGPIO(GP_5, GPIO_DIRECTION_OUT); fileHandleGPIO_6 = openGPIO(GP_6, GPIO_DIRECTION_OUT); fileHandleGPIO_7 = openGPIO(GP_7, GPIO_DIRECTION_OUT); if(ERROR == fileHandleGPIO_LED) { return(-1); } for(i=0; i< 10; i++) { //LED ON writeGPIO(fileHandleGPIO_LED, 1); writeGPIO(fileHandleGPIO_5, 1); writeGPIO(fileHandleGPIO_6, 1); writeGPIO(fileHandleGPIO_7, 1); sleep(BLINK_TIME_SEC); //LED OFF writeGPIO(fileHandleGPIO_LED, 0); writeGPIO(fileHandleGPIO_5, 0); writeGPIO(fileHandleGPIO_6, 0); writeGPIO(fileHandleGPIO_7, 0); sleep(BLINK_TIME_SEC); } closeGPIO(GP_LED, fileHandleGPIO_LED); closeGPIO(GP_5, fileHandleGPIO_5); closeGPIO(GP_6, fileHandleGPIO_6); closeGPIO(GP_7, fileHandleGPIO_7); puts("Finished LED blink GP_LED - gpio-3 on Galileo board."); return 0; }
void TICC1100::stopListening() { try { if(_listenThread.joinable()) { _stopCallbackThread = true; _listenThread.join(); } _stopCallbackThread = false; if(_fileDescriptor->descriptor != -1) closeDevice(); closeGPIO(1); _stopped = true; IPhysicalInterface::stopListening(); } catch(const std::exception& ex) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(BaseLib::Exception& ex) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(...) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__); } }
void TICC1100::initDevice() { try { openDevice(); if(!_fileDescriptor || _fileDescriptor->descriptor == -1) return; initChip(); _out.printDebug("Debug: CC1100: Setting GPIO direction"); setGPIODirection(1, GPIODirection::IN); _out.printDebug("Debug: CC1100: Setting GPIO edge"); setGPIOEdge(1, GPIOEdge::BOTH); openGPIO(1, true); if(!_gpioDescriptors[1] || _gpioDescriptors[1]->descriptor == -1) throw(BaseLib::Exception("Couldn't listen to rf device, because the gpio pointer is not valid: " + _settings->device)); if(gpioDefined(2)) //Enable high gain mode { openGPIO(2, false); if(!getGPIO(2)) setGPIO(2, true); closeGPIO(2); } } catch(const std::exception& ex) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(BaseLib::Exception& ex) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(...) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__); } }
void reset() { printf("Begin Reset\n"); writeGPIO(fileHandleGPIO_4, 0); //LSB writeGPIO(fileHandleGPIO_5, 0); writeGPIO(fileHandleGPIO_6, 0); writeGPIO(fileHandleGPIO_7, 0); //MSB sleep(1); writeGPIO(fileHandleGPIO_S, 0); sleep(1); printf("Recieving Reset Confirmation\n"); //TEST!!!!! FOR RESETTING THE VALUES TO ZERO BEFORE SWITCH writeGPIO(fileHandleGPIO_4, 0); //LSB writeGPIO(fileHandleGPIO_5, 0); writeGPIO(fileHandleGPIO_6, 0); writeGPIO(fileHandleGPIO_7, 0); //MSB closeGPIO(GP_4,fileHandleGPIO_4); closeGPIO(GP_5,fileHandleGPIO_5); closeGPIO(GP_6,fileHandleGPIO_6); closeGPIO(GP_7,fileHandleGPIO_7); fileHandleGPIO_4 = openGPIO(GP_4, GPIO_DIRECTION_IN); fileHandleGPIO_5 = openGPIO(GP_5, GPIO_DIRECTION_IN); fileHandleGPIO_6 = openGPIO(GP_6, GPIO_DIRECTION_IN); fileHandleGPIO_7 = openGPIO(GP_7, GPIO_DIRECTION_IN); sleep(1); if(ReadAndConvertVals() == MSG_ACK) { printf("Reset Successful\n"); } else { printf("Reset Unsuccessful\n"); } }
void COC::startListening() { try { _socket = GD::bl->serialDeviceManager.get(_settings->device); if(!_socket) _socket = GD::bl->serialDeviceManager.create(_settings->device, 38400, O_RDWR | O_NOCTTY | O_NDELAY, true, 45); if(!_socket) return; _socket->addEventHandler(this); _socket->openDevice(); if(gpioDefined(2)) { openGPIO(2, false); if(!getGPIO(2)) setGPIO(2, true); closeGPIO(2); } if(gpioDefined(1)) { openGPIO(1, false); if(!getGPIO(1)) { setGPIO(1, false); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); setGPIO(1, true); std::this_thread::sleep_for(std::chrono::milliseconds(2000)); } closeGPIO(1); } writeToDevice(stackPrefix + "X21\n" + stackPrefix + "Zr\n"); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); IPhysicalInterface::startListening(); } catch(const std::exception& ex) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(BaseLib::Exception& ex) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(...) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__); } }
int main() { fileHandleGPIO_S = openGPIO(Strobe, GPIO_DIRECTION_OUT); while(1) { fileHandleGPIO_4 = openGPIO(GP_4, GPIO_DIRECTION_OUT); fileHandleGPIO_5 = openGPIO(GP_5, GPIO_DIRECTION_OUT); fileHandleGPIO_6 = openGPIO(GP_6, GPIO_DIRECTION_OUT); fileHandleGPIO_7 = openGPIO(GP_7, GPIO_DIRECTION_OUT); writeGPIO(fileHandleGPIO_S, 1); char input[10]; printf("Enter Command:\n"); scanf("%s",&input); if(strcmp(input,"ping") == 0) { ping(); } else if(strcmp(input,"reset") == 0) { reset(); } else if(strcmp(input,"get") == 0) { get(); } else { printf("Invalid Command \n"); } system("./test"); closeGPIO(GP_4, fileHandleGPIO_4); closeGPIO(GP_5, fileHandleGPIO_5); closeGPIO(GP_6, fileHandleGPIO_6); closeGPIO(GP_7, fileHandleGPIO_7); sleep(1); } return 0; }
void ping() { printf("Begin Ping\n"); writeGPIO(fileHandleGPIO_4, 1); //LSB writeGPIO(fileHandleGPIO_5, 0); writeGPIO(fileHandleGPIO_6, 0); writeGPIO(fileHandleGPIO_7, 0); //MSB sleep(1); writeGPIO(fileHandleGPIO_S, 0); sleep(1); printf("Recieving Ping\n"); writeGPIO(fileHandleGPIO_4, 0); //LSB writeGPIO(fileHandleGPIO_5, 0); writeGPIO(fileHandleGPIO_6, 0); writeGPIO(fileHandleGPIO_7, 0); //MSB closeGPIO(GP_4,fileHandleGPIO_4); closeGPIO(GP_5,fileHandleGPIO_5); closeGPIO(GP_6,fileHandleGPIO_6); closeGPIO(GP_7,fileHandleGPIO_7); fileHandleGPIO_4 = openGPIO(GP_4, GPIO_DIRECTION_IN); fileHandleGPIO_5 = openGPIO(GP_5, GPIO_DIRECTION_IN); fileHandleGPIO_6 = openGPIO(GP_6, GPIO_DIRECTION_IN); fileHandleGPIO_7 = openGPIO(GP_7, GPIO_DIRECTION_IN); sleep(1); if(ReadAndConvertVals() == MSG_ACK) { printf("Ping Successful\n"); } else { printf("Ping Unsuccessful\n"); } }
bool pollGPIO(){ char result; initGPIO(); fseek(gpio_file, 0, 0); fread(&result, sizeof(char), 1, gpio_file); closeGPIO(); if(result == '0'){ return 0; } else { return 1; } }
TICC1100::~TICC1100() { try { _stopCallbackThread = true; if(_listenThread.joinable()) _listenThread.join(); closeDevice(); closeGPIO(1); } catch(const std::exception& ex) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(BaseLib::Exception& ex) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(...) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__); } }
int main(void) { int fileHandleGPIO_LED; int fileHandleGPIO_PROXY; int i=0; puts("Starting proximity reader on Galileo board."); fileHandleGPIO_PROXY = openGPIO(GP_PROXY, GPIO_DIRECTION_IN); if(ERROR == fileHandleGPIO_PROXY) { puts("Unable to open toggle Proximity port #8"); return(-1); } fileHandleGPIO_LED = openGPIO(GP_LED, GPIO_DIRECTION_OUT); if(ERROR == fileHandleGPIO_LED) { puts("Unable to open toggle LED port #13"); return(-1); } //Switch off the LED before starting. writeGPIO(fileHandleGPIO_LED, 0); //set PWM parameters openPWM(GP_PWM); setPWMPeriod(1000000,GP_PWM); enablePWM(1,GP_PWM); setPWMDutyCycle(0,GP_PWM); //Start an infinite loop to keep polling for proximity info int proxyValue = 0; while(1==1) { proxyValue = readGPIO(fileHandleGPIO_PROXY,GP_PROXY); if(proxyValue == 1) { if(duty_cycle == 500000) { duty_cycle = 200000; writeGPIO(fileHandleGPIO_LED, 0); } else { duty_cycle = 500000; writeGPIO(fileHandleGPIO_LED, 1); } setPWMDutyCycle(duty_cycle,GP_PWM); } else { duty_cycle = 50000; setPWMDutyCycle(0,GP_PWM); writeGPIO(fileHandleGPIO_LED, 0); } usleep(1000*400); } closeGPIO(GP_LED, fileHandleGPIO_LED); closeGPIO(GP_PROXY, fileHandleGPIO_PROXY); closePWM(GP_PWM); puts("Finished BURGLER ALARM on Galileo board."); return 0; }
void get() { int GetVal = 0; printf("Sending Get Command\n"); writeGPIO(fileHandleGPIO_4, 0); //LSB writeGPIO(fileHandleGPIO_5, 1); writeGPIO(fileHandleGPIO_6, 0); writeGPIO(fileHandleGPIO_7, 0); //MSB sleep(1); writeGPIO(fileHandleGPIO_S, 0);//PIC reads command sleep(1); writeGPIO(fileHandleGPIO_4, 0); //LSB writeGPIO(fileHandleGPIO_5, 0); writeGPIO(fileHandleGPIO_6, 0); writeGPIO(fileHandleGPIO_7, 0); //MSB closeGPIO(GP_4,fileHandleGPIO_4); closeGPIO(GP_5,fileHandleGPIO_5); closeGPIO(GP_6,fileHandleGPIO_6); closeGPIO(GP_7,fileHandleGPIO_7); sleep(1); writeGPIO(fileHandleGPIO_S, 1); sleep(1);//Wait for pic to output fileHandleGPIO_4 = openGPIO(GP_4, GPIO_DIRECTION_IN); fileHandleGPIO_5 = openGPIO(GP_5, GPIO_DIRECTION_IN); fileHandleGPIO_6 = openGPIO(GP_6, GPIO_DIRECTION_IN); fileHandleGPIO_7 = openGPIO(GP_7, GPIO_DIRECTION_IN); sleep(1); printf("Recieving First Nibble\n"); GetVal = ReadAndConvertVals(); closeGPIO(GP_4,fileHandleGPIO_4); closeGPIO(GP_5,fileHandleGPIO_5); closeGPIO(GP_6,fileHandleGPIO_6); closeGPIO(GP_7,fileHandleGPIO_7); sleep(1); writeGPIO(fileHandleGPIO_S, 0); sleep(1); fileHandleGPIO_4 = openGPIO(GP_4, GPIO_DIRECTION_IN); fileHandleGPIO_5 = openGPIO(GP_5, GPIO_DIRECTION_IN); fileHandleGPIO_6 = openGPIO(GP_6, GPIO_DIRECTION_IN); fileHandleGPIO_7 = openGPIO(GP_7, GPIO_DIRECTION_IN); sleep(1); printf("Recieving Second Nibble\n"); GetVal += ReadAndConvertVals2(); closeGPIO(GP_4,fileHandleGPIO_4); closeGPIO(GP_5,fileHandleGPIO_5); closeGPIO(GP_6,fileHandleGPIO_6); closeGPIO(GP_7,fileHandleGPIO_7); writeGPIO(fileHandleGPIO_S, 1); sleep(1); fileHandleGPIO_4 = openGPIO(GP_4, GPIO_DIRECTION_IN); fileHandleGPIO_5 = openGPIO(GP_5, GPIO_DIRECTION_IN); fileHandleGPIO_6 = openGPIO(GP_6, GPIO_DIRECTION_IN); fileHandleGPIO_7 = openGPIO(GP_7, GPIO_DIRECTION_IN); sleep(1); printf("Recieving Last Crumb\n"); GetVal += ReadAndConvertVals3(); closeGPIO(GP_4,fileHandleGPIO_4); closeGPIO(GP_5,fileHandleGPIO_5); closeGPIO(GP_6,fileHandleGPIO_6); closeGPIO(GP_7,fileHandleGPIO_7); sleep(1); writeGPIO(fileHandleGPIO_S, 0); sleep(1); fileHandleGPIO_4 = openGPIO(GP_4, GPIO_DIRECTION_IN); fileHandleGPIO_5 = openGPIO(GP_5, GPIO_DIRECTION_IN); fileHandleGPIO_6 = openGPIO(GP_6, GPIO_DIRECTION_IN); fileHandleGPIO_7 = openGPIO(GP_7, GPIO_DIRECTION_IN); double volts; volts = (GetVal / 1023.0) * 5.0; if(ReadAndConvertVals() == 14) { printf("ADC Value Acquisition Complete\n"); printf("ADC Value = %d\n", GetVal); printf("Volts: %lf\n", volts); } else { printf("ADC Value Acquisition Failed\n"); } return; }
void TICC1100::mainThread() { try { int32_t pollResult; int32_t bytesRead; std::vector<char> readBuffer({'0'}); while(!_stopCallbackThread && _fileDescriptor->descriptor > -1 && _gpioDescriptors[1]->descriptor > -1) { if(_stopped) { std::this_thread::sleep_for(std::chrono::milliseconds(200)); continue; } pollfd pollstruct { (int)_gpioDescriptors[1]->descriptor, (short)(POLLPRI | POLLERR), (short)0 }; pollResult = poll(&pollstruct, 1, 100); /*if(pollstruct.revents & POLLERR) { _out.printWarning("Warning: Error polling GPIO. Reopening..."); closeGPIO(); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); openGPIO(_settings->gpio1); }*/ if(pollResult > 0) { if(lseek(_gpioDescriptors[1]->descriptor, 0, SEEK_SET) == -1) throw BaseLib::Exception("Could not poll gpio: " + std::string(strerror(errno))); bytesRead = read(_gpioDescriptors[1]->descriptor, &readBuffer[0], 1); if(!bytesRead) continue; if(readBuffer.at(0) == 0x30) { if(!_sending) _txMutex.try_lock(); //We are receiving, don't send now continue; //Packet is being received. Wait for GDO high } if(_sending) endSending(); else { //sendCommandStrobe(CommandStrobes::Enum::SIDLE); std::shared_ptr<MAXPacket> packet; if(crcOK()) { uint8_t firstByte = readRegister(Registers::Enum::FIFO); std::vector<uint8_t> packetBytes = readRegisters(Registers::Enum::FIFO, firstByte + 1); //Read packet + RSSI packetBytes[0] = firstByte; if(packetBytes.size() >= 9) packet.reset(new MAXPacket(packetBytes, true, BaseLib::HelperFunctions::getTime())); else _out.printWarning("Warning: Too small packet received: " + BaseLib::HelperFunctions::getHexString(packetBytes)); } else _out.printDebug("Debug: MAX! packet received, but CRC failed."); if(!_sendingPending) { sendCommandStrobe(CommandStrobes::Enum::SFRX); sendCommandStrobe(CommandStrobes::Enum::SRX); } if(packet) { if(_firstPacket) _firstPacket = false; else raisePacketReceived(packet); } } _txMutex.unlock(); //Packet sent or received, now we can send again } else if(pollResult < 0) { _txMutex.unlock(); _out.printError("Error: Could not poll gpio: " + std::string(strerror(errno)) + ". Reopening..."); closeGPIO(1); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); openGPIO(1, true); } //pollResult == 0 is timeout } } catch(const std::exception& ex) { _txMutex.unlock(); _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(BaseLib::Exception& ex) { _txMutex.unlock(); _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(...) { _txMutex.unlock(); _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__); } try { if(!_stopCallbackThread && (_fileDescriptor->descriptor == -1 || _gpioDescriptors[1]->descriptor == -1)) { _out.printError("Connection to TI CC1101 closed inexpectedly... Trying to reconnect..."); _stopCallbackThread = true; //Set to true, so that sendPacket aborts _txMutex.unlock(); //Make sure _txMutex is unlocked std::thread thread(&TICC1100::startListening, this); thread.detach(); } else _txMutex.unlock(); //Make sure _txMutex is unlocked } catch(const std::exception& ex) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(BaseLib::Exception& ex) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__, ex.what()); } catch(...) { _out.printEx(__FILE__, __LINE__, __PRETTY_FUNCTION__); } }