servo_timer_tick(void) // servo timer function { unsigned char i; os_timer_disarm(&servo_timer); // dis_arm the timer os_timer_setfn(&servo_timer, (os_timer_func_t *)servo_timer_tick, NULL); // set the timer function, dot get os_timer_func_t to force function convert os_timer_arm(&servo_timer, 20, 1); // arm the timer every 20ms and repeat if (need_sort != 0) { sort_pulse_time(); } // if only 1 servo is active if (active_servos == 1) { DIRECT_WRITE_HIGH(servo_pin_sorted[0]); os_delay_us(servo_delay_time[0]); DIRECT_WRITE_LOW(servo_pin_sorted[0]); } // if more than 1 servo is active, loop for all active servo is needed else if (active_servos > 1) { for ( i = 0 ; i < active_servos ; i++ ) { if (servo_pin_sorted[i] >= 0) {DIRECT_WRITE_HIGH(servo_pin_sorted[i]);} } for ( i = 0 ; i < active_servos ; i++ ) { if (servo_delay_time[i] > 0) {os_delay_us(servo_delay_time[i]);} if (servo_pin_sorted[i] >= 0) {DIRECT_WRITE_LOW(servo_pin_sorted[i]);} } } }
int CapacitiveSensor::SenseOneCycle(void) { noInterrupts(); DIRECT_WRITE_LOW(sReg, sBit); // sendPin Register low DIRECT_MODE_INPUT(rReg, rBit); // receivePin to input (pullups are off) DIRECT_MODE_OUTPUT(rReg, rBit); // receivePin to OUTPUT DIRECT_WRITE_LOW(rReg, rBit); // pin is now LOW AND OUTPUT delayMicroseconds(10); DIRECT_MODE_INPUT(rReg, rBit); // receivePin to input (pullups are off) DIRECT_WRITE_HIGH(sReg, sBit); // sendPin High interrupts(); while ( !DIRECT_READ(rReg, rBit) && (total < CS_Timeout_Millis) ) { // while receive pin is LOW AND total is positive value total++; } //Serial.print("SenseOneCycle(1): "); //Serial.println(total); if (total > CS_Timeout_Millis) { return -2; // total variable over timeout } // set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V noInterrupts(); DIRECT_WRITE_HIGH(rReg, rBit); DIRECT_MODE_OUTPUT(rReg, rBit); // receivePin to OUTPUT - pin is now HIGH AND OUTPUT DIRECT_WRITE_HIGH(rReg, rBit); DIRECT_MODE_INPUT(rReg, rBit); // receivePin to INPUT (pullup is off) DIRECT_WRITE_LOW(sReg, sBit); // sendPin LOW interrupts(); #ifdef FIVE_VOLT_TOLERANCE_WORKAROUND DIRECT_MODE_OUTPUT(rReg, rBit); DIRECT_WRITE_LOW(rReg, rBit); delayMicroseconds(10); DIRECT_MODE_INPUT(rReg, rBit); // receivePin to INPUT (pullup is off) #else while ( DIRECT_READ(rReg, rBit) && (total < CS_Timeout_Millis) ) { // while receive pin is HIGH AND total is less than timeout total++; } #endif //Serial.print("SenseOneCycle(2): "); //Serial.println(total); if (total >= CS_Timeout_Millis) { return -2; // total variable over timeout } else { return 1; } }
// Lua: write( pin, baudrate, string1, [string2], ..., [stringn] ) static int softuart_write( lua_State* L ) { unsigned pin, baudrate, bit_time; const char* buf; size_t len, i; int total = lua_gettop( L ), s; pin = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( gpio, pin ); baudrate = luaL_checkinteger( L, 2 ); //if( baudrate > 57600 ) // return luaL_error( L, "max baudrate is 57600" ); //bit_time = (1000000 / baudrate); bit_time = (6000000 / baudrate); if (pin >= 0) { DIRECT_WRITE_HIGH(pin); DIRECT_MODE_OUTPUT(pin); os_delay_us((bit_time<<3)/6); } for( s = 3; s <= total; s ++ ) { if( lua_type( L, s ) == LUA_TNUMBER ) { len = lua_tointeger( L, s ); if( len > 255 ) return luaL_error( L, "invalid number" ); soft_uart_putchar_c(pin,bit_time,( u8 )len); } else { luaL_checktype( L, s, LUA_TSTRING ); buf = lua_tolstring( L, s, &len ); for( i = 0; i < len; i ++ ) soft_uart_putchar_c(pin, bit_time, buf[ i ]); } } return 0; }
void OneWireSlave::sendBit(uint8_t v) { uint8_t mask = pin_bitmask; volatile uint8_t *reg asm("r30") = baseReg; cli(); DIRECT_MODE_INPUT(reg, mask); if (!waitTimeSlot() ) { errno = ONEWIRE_WRITE_TIMESLOT_TIMEOUT; sei(); return; } if (v & 1) delayMicroseconds(30); else { cli(); DIRECT_WRITE_LOW(reg, mask); DIRECT_MODE_OUTPUT(reg, mask); delayMicroseconds(30); DIRECT_WRITE_HIGH(reg, mask); sei(); } sei(); return; }
// return values: // DHTLIB_OK // DHTLIB_ERROR_TIMEOUT int dht_readSensor(uint8_t pin, uint8_t wakeupDelay) { // INIT BUFFERVAR TO RECEIVE DATA uint8_t mask = 128; uint8_t idx = 0; uint8_t i = 0; // replace digitalRead() with Direct Port Reads. // reduces footprint ~100 bytes => portability issue? // direct port read is about 3x faster // uint8_t bit = digitalPinToBitMask(pin); // uint8_t port = digitalPinToPort(pin); // volatile uint8_t *PIR = portInputRegister(port); // EMPTY BUFFER for (i = 0; i < 5; i++) dht_bytes[i] = 0; // REQUEST SAMPLE // pinMode(pin, OUTPUT); platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_PULLUP); DIRECT_MODE_OUTPUT(pin); // digitalWrite(pin, LOW); // T-be DIRECT_WRITE_LOW(pin); // delay(wakeupDelay); for (i = 0; i < wakeupDelay; i++) os_delay_us(1000); // Disable interrupts ets_intr_lock(); // digitalWrite(pin, HIGH); // T-go DIRECT_WRITE_HIGH(pin); os_delay_us(40); // pinMode(pin, INPUT); DIRECT_MODE_INPUT(pin); // GET ACKNOWLEDGE or TIMEOUT uint16_t loopCntLOW = DHTLIB_TIMEOUT; while (DIRECT_READ(pin) == LOW ) // T-rel { os_delay_us(1); if (--loopCntLOW == 0) return DHTLIB_ERROR_TIMEOUT; } uint16_t loopCntHIGH = DHTLIB_TIMEOUT; while (DIRECT_READ(pin) != LOW ) // T-reh { os_delay_us(1); if (--loopCntHIGH == 0) return DHTLIB_ERROR_TIMEOUT; } // READ THE OUTPUT - 40 BITS => 5 BYTES for (i = 40; i != 0; i--) { loopCntLOW = DHTLIB_TIMEOUT; while (DIRECT_READ(pin) == LOW ) { os_delay_us(1); if (--loopCntLOW == 0) return DHTLIB_ERROR_TIMEOUT; } uint32_t t = system_get_time(); loopCntHIGH = DHTLIB_TIMEOUT; while (DIRECT_READ(pin) != LOW ) { os_delay_us(1); if (--loopCntHIGH == 0) return DHTLIB_ERROR_TIMEOUT; } if ((system_get_time() - t) > 40) { dht_bytes[idx] |= mask; } mask >>= 1; if (mask == 0) // next byte? { mask = 128; idx++; } } // Enable interrupts ets_intr_unlock(); // pinMode(pin, OUTPUT); DIRECT_MODE_OUTPUT(pin); // digitalWrite(pin, HIGH); DIRECT_WRITE_HIGH(pin); return DHTLIB_OK; }