/** * read bytes from uart with timeout ms */ int suli_uart_read_bytes_timeout(UART_T *uart, uint8_t *buff, int len, int timeout_ms) { uint8_t *ptr = buff; uint8_t *end = ptr + len; uint32_t t = suli_millis(); while (ptr != end) { if ((suli_millis() - t) > timeout_ms) break; int c = serial_getc(uart); *ptr++ = c; } return ptr - buff; }
bool GroveTempHumPro::_read(IO_T *io) { uint8_t laststate = SULI_HIGH; uint8_t counter = 0; uint8_t j = 0, i; unsigned long currenttime; // pull the pin high and wait 250 milliseconds //digitalWrite(_pin, SULI_HIGH); //suli_pin_write(io, SULI_HIGH); //suli_delay_ms(250); currenttime = suli_millis(); if (currenttime < _lastreadtime) { // ie there was a rollover _lastreadtime = 0; } if (!firstreading && ((currenttime - _lastreadtime) < 2000)) { return true; // return last correct measurement //delay(2000 - (currenttime - _lastreadtime)); } firstreading = false; _lastreadtime = suli_millis(); data[0] = data[1] = data[2] = data[3] = data[4] = 0; // now pull it low for ~20 milliseconds //pinMode(_pin, OUTPUT); suli_pin_dir(io, SULI_OUTPUT); //digitalWrite(_pin, LOW); suli_pin_write(io, SULI_LOW); suli_delay_ms(20); //cli(); //digitalWrite(_pin, SULI_HIGH); suli_pin_write(io, SULI_HIGH); //delayMicroseconds(40); suli_delay_us(40); //pinMode(_pin, INPUT); suli_pin_dir(io, SULI_INPUT); // read in timings for (i = 0; i < MAXTIMINGS; i++) { counter = 0; //while (digitalRead(_pin) == laststate) { while (suli_pin_read(io) == laststate) { counter++; //delayMicroseconds(1); suli_delay_us(1); if (counter == 255) { break; } } //laststate = digitalRead(&_pin); laststate = suli_pin_read(io); if (counter == 255) break; // ignore first 3 transitions if ((i >= 4) && (i % 2 == 0)) { // shove each bit into the storage bytes data[j / 8] <<= 1; if (counter > _count) // data[j / 8] |= 1; j++; } } // check we read 40 bits and that the checksum matches if ((j >= 40) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))) { return true; } return false; }
/* * delay ms */ void suli_delay_ms(uint32 ms) { uint64 mark_time = suli_millis(); while(suli_millis() - mark_time < ms); }