inline void ledsInit() { // Setup led pins pinMode(MY_DEFAULT_RX_LED_PIN, OUTPUT); pinMode(MY_DEFAULT_TX_LED_PIN, OUTPUT); pinMode(MY_DEFAULT_ERR_LED_PIN, OUTPUT); // Set initial state of leds hwDigitalWrite(MY_DEFAULT_RX_LED_PIN, LED_OFF); hwDigitalWrite(MY_DEFAULT_TX_LED_PIN, LED_OFF); hwDigitalWrite(MY_DEFAULT_ERR_LED_PIN, LED_OFF); // initialize counters _countRx = 0; _countTx = 0; _countErr = 0; }
bool transportInit(void) { // Reset the state machine _dev.begin(MY_RS485_BAUD_RATE); _serialReset(); #if defined(MY_RS485_DE_PIN) hwPinMode(MY_RS485_DE_PIN, OUTPUT); hwDigitalWrite(MY_RS485_DE_PIN, LOW); #endif return true; }
inline void ledsProcess() { // Just return if it is not the time... // http://playground.arduino.cc/Code/TimingRollover if ((long)(hwMillis() - _blink_next_time) < 0) return; else _blink_next_time = hwMillis() + MY_DEFAULT_LED_BLINK_PERIOD; // do the actual blinking if(_countRx && _countRx != 255) { // switch led on hwDigitalWrite(MY_DEFAULT_RX_LED_PIN, LED_ON); } else if(!_countRx) { // switching off hwDigitalWrite(MY_DEFAULT_RX_LED_PIN, LED_OFF); } if(_countRx != 255) --_countRx; if(_countTx && _countTx != 255) { // switch led on hwDigitalWrite(MY_DEFAULT_TX_LED_PIN, LED_ON); } else if(!_countTx) { // switching off hwDigitalWrite(MY_DEFAULT_TX_LED_PIN, LED_OFF); } if(_countTx != 255) --_countTx; if(_countErr && _countErr != 255) { // switch led on hwDigitalWrite(MY_DEFAULT_ERR_LED_PIN, LED_ON); } else if(!_countErr) { // switching off hwDigitalWrite(MY_DEFAULT_ERR_LED_PIN, LED_OFF); } if(_countErr != 255) --_countErr; }
bool transportSend(const uint8_t to, const void* data, const uint8_t len) { const char *datap = static_cast<char const *>(data); unsigned char i; unsigned char cs = 0; unsigned char del; // This is how many times to try and transmit before failing. unsigned char timeout = 10; // Let's start out by looking for a collision. If there has been anything seen in // the last millisecond, then wait for a random time and check again. while (_serialProcess()) { del = rand() % 20; for (i = 0; i < del; i++) { delay(1); _serialProcess(); } timeout--; if (timeout == 0) { // Failed to transmit!!! return false; } } #if defined(MY_RS485_DE_PIN) hwDigitalWrite(MY_RS485_DE_PIN, HIGH); delayMicroseconds(5); #endif // Start of header by writing multiple SOH for(byte w=0; w<1; w++) { _dev.write(SOH); } _dev.write(to); // Destination address cs += to; _dev.write(_nodeId); // Source address cs += _nodeId; _dev.write(ICSC_SYS_PACK); // Command code cs += ICSC_SYS_PACK; _dev.write(len); // Length of text cs += len; _dev.write(STX); // Start of text for(i=0; i<len; i++) { _dev.write(datap[i]); // Text bytes cs += datap[i]; } _dev.write(ETX); // End of text _dev.write(cs); _dev.write(EOT); #if defined(MY_RS485_DE_PIN) #ifdef __PIC32MX__ // MPIDE has nothing yet for this. It uses the hardware buffer, which // could be up to 8 levels deep. For now, let's just delay for 8 // characters worth. delayMicroseconds((F_CPU/9600)+1); #else #if defined(ARDUINO) && ARDUINO >= 100 #if ARDUINO >= 104 // Arduino 1.0.4 and upwards does it right _dev.flush(); #else // Between 1.0.0 and 1.0.3 it almost does it - need to compensate // for the hardware buffer. Delay for 2 bytes worth of transmission. _dev.flush(); delayMicroseconds((20000000UL/9600)+1); #endif #elif defined(__linux__) _dev.flush(); #endif #endif hwDigitalWrite(MY_RS485_DE_PIN, LOW); #endif return true; }
LOCAL void RF24_ce(const bool level) { hwDigitalWrite(MY_RF24_CE_PIN, level); }