//Switches the I2C address of the desired sensor and removes others void Sensor::SelectSensor(byte sensorNumber) { if (sensorNumber == sC::FR) { sensGPIO.writeGPIOA(0x7F); SEN_PRINTLN("FR Selected"); } else if (sensorNumber == sC::LTR) { sensGPIO.writeGPIOA(0xBF); SEN_PRINTLN("LTR Selected"); } else if (sensorNumber == sC::LTL) { sensGPIO.writeGPIOA(0xDF); SEN_PRINTLN("LTL Selected"); } else if (sensorNumber == sC::FL) { sensGPIO.writeGPIOA(0xEF); SEN_PRINTLN("FL Selected"); } else if (sensorNumber == sC::MR) { sensGPIO.writeGPIOA(0xF7); SEN_PRINTLN("MR Selected"); } else if (sensorNumber == sC::ML) { sensGPIO.writeGPIOA(0xFB); SEN_PRINTLN("ML Selected"); } else if (sensorNumber == sC::BR) { sensGPIO.writeGPIOA(0xFD); SEN_PRINTLN("BR Selected"); } else if (sensorNumber == sC::BL) { sensGPIO.writeGPIOA(0xFE); SEN_PRINTLN("BL Selected"); } else { ERROR_PRINTLN("Sensor Select Error"); ERROR_PRINTLN("All address lines returned to default"); sensGPIO.writeGPIOA(0xFF); } }
bool MCP::turnSw(byte num) { if ((num == -1) || (num == 255)) { ERROR_PRINT("ERROR: MCP.turnSw num = "); ERROR_PRINTLN(num); return false; } return turnSw(num, !actStates.msw[num]); }
bool MCP::turnSw(byte num, bool state) { if ((num == -1) || (num == 255)) { ERROR_PRINT("ERROR: MCP.turnSw num = "); ERROR_PRINTLN(num); return false; } if (actStates.msw[num] != state) { actStates.setMsw(num, state); MCP23017::digitalWrite(appSettings.msw[num], state); mqtt->publish(appSettings.topMSW, (num+1), OUT, (state?"ON":"OFF")); } return state; }
//Reads the Raw value from whichever sensor is enabled void Sensor::ReadRaw() { if(_s == sC::SensorType::TSL) { Raw = 0; if (!tsl.getData(Raw)) { ERROR_PRINTLN("I2C ERROR"); } SEN_VPRINTLN(Raw); } else { #ifdef QTRSINUSE unsigned int *a; rc[_pin].readCalibrated(a); Raw = (uint16_t)a; #endif } }
void* ts_malloc(size_t size) { void* block; /* To prevent corruption of the heap due to concurrent access between two * threads or by an interrupt routine, this call is made atomic.*/ ATOMIC { block = malloc(size); } if(!block) { //This should be optimized away by the compiler when ERROR_OUTPUT is defined to 0. ERROR_PRINTLN("Malloc failed!"); } return block; }
//Polls all given sensors in the order specified. void Sensor::PollSensors(Sensor *sens, const sC::sensorNumber *order, const byte OrderLength){ if (_sensorInitComplete == false) { ERROR_PRINTLN("Please call initSensors() before attempting to poll"); return; } sC::sensorNumber currentSensorIndex = sC::FR; for (byte n = 0; n < OrderLength; n++) { currentSensorIndex = order[n]; SelectSensor(static_cast<byte>(currentSensorIndex)); SLASTVAL(currentSensorIndex, RVAL(currentSensorIndex)); sens[currentSensorIndex].GetReading(); SVAL(currentSensorIndex, sens[currentSensorIndex].tileWhite); } printbw(); }