// Lua: apa102.write(data_pin, clock_pin, "string") // Byte quads in the string are interpreted as (brightness, B, G, R) values. // Only the first 5 bits of the brightness value is actually used (0-31). // This function does not corrupt your buffer. // // apa102.write(1, 3, string.char(31, 0, 255, 0)) uses GPIO5 for DATA and GPIO0 for CLOCK and sets the first LED green, with the brightness 31 (out of 0-32) // apa102.write(5, 6, string.char(255, 0, 0, 255):rep(10)) uses GPIO14 for DATA and GPIO12 for CLOCK and sets ten LED to red, with the brightness 31 (out of 0-32). // Brightness values are clamped to 0-31. static int apa102_write(lua_State* L) { uint8_t data_pin = luaL_checkinteger(L, 1); MOD_CHECK_ID(gpio, data_pin); uint32_t alt_data_pin = pin_num[data_pin]; uint8_t clock_pin = luaL_checkinteger(L, 2); MOD_CHECK_ID(gpio, clock_pin); uint32_t alt_clock_pin = pin_num[clock_pin]; size_t buf_len; const char *buf = luaL_checklstring(L, 3, &buf_len); uint32_t nbr_frames = buf_len / 4; if (nbr_frames > 100000) { return luaL_error(L, "The supplied buffer is too long, and might cause the callback watchdog to bark."); } // Initialize the output pins platform_gpio_mode(data_pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); GPIO_OUTPUT_SET(alt_data_pin, PLATFORM_GPIO_HIGH); // Set pin high platform_gpio_mode(clock_pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); GPIO_OUTPUT_SET(alt_clock_pin, PLATFORM_GPIO_LOW); // Set pin low // Send the buffers apa102_send_buffer(alt_data_pin, alt_clock_pin, (uint32_t *) buf, (uint32_t) nbr_frames); return 0; }
uint8_t u8g_com_esp8266_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { switch(msg) { case U8G_COM_MSG_STOP: break; case U8G_COM_MSG_INIT: // we assume that the SPI interface was already initialized // just care for the /CS and D/C pins lu8g_digital_write( u8g, U8G_PI_CS, PLATFORM_GPIO_HIGH ); platform_gpio_mode( u8g->pin_list[U8G_PI_CS], PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT ); platform_gpio_mode( u8g->pin_list[U8G_PI_A0], PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT ); break; case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ lu8g_digital_write( u8g, U8G_PI_A0, arg_val == 0 ? PLATFORM_GPIO_LOW : PLATFORM_GPIO_HIGH ); break; case U8G_COM_MSG_CHIP_SELECT: if (arg_val == 0) { /* disable */ lu8g_digital_write( u8g, U8G_PI_CS, PLATFORM_GPIO_HIGH ); } else { /* enable */ //u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, LOW); lu8g_digital_write( u8g, U8G_PI_CS, PLATFORM_GPIO_LOW ); } break; case U8G_COM_MSG_RESET: if ( u8g->pin_list[U8G_PI_RESET] != U8G_PIN_NONE ) lu8g_digital_write( u8g, U8G_PI_RESET, arg_val == 0 ? PLATFORM_GPIO_LOW : PLATFORM_GPIO_HIGH ); break; case U8G_COM_MSG_WRITE_BYTE: platform_spi_send( 1, 8, arg_val ); break; case U8G_COM_MSG_WRITE_SEQ: case U8G_COM_MSG_WRITE_SEQ_P: { register uint8_t *ptr = arg_ptr; while( arg_val > 0 ) { platform_spi_send( 1, 8, *ptr++ ); arg_val--; } } break; } return 1; }
/*Lua: hx711.init(clk_pin,data_pin)*/ static int hx711_init(lua_State* L) { clk_pin = luaL_checkinteger(L,1); data_pin = luaL_checkinteger(L,2); MOD_CHECK_ID( gpio, clk_pin ); MOD_CHECK_ID( gpio, data_pin ); platform_gpio_mode(clk_pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); platform_gpio_mode(data_pin, PLATFORM_GPIO_INPUT, PLATFORM_GPIO_FLOAT); platform_gpio_write(clk_pin,1);//put chip to sleep. return 0; }
uint32_t platform_i2c_setup( unsigned id, uint8_t sda, uint8_t scl, uint32_t speed ){ if (sda >= NUM_GPIO || scl >= NUM_GPIO) return 0; // platform_pwm_close(sda); // platform_pwm_close(scl); // disable gpio interrupt first platform_gpio_mode(sda, PLATFORM_GPIO_INPUT, PLATFORM_GPIO_PULLUP); // inside this func call platform_pwm_close platform_gpio_mode(scl, PLATFORM_GPIO_INPUT, PLATFORM_GPIO_PULLUP); // disable gpio interrupt first i2c_master_gpio_init(sda, scl); return PLATFORM_I2C_SPEED_SLOW; }
// ws2812.writergb(4, string.char(255, 0, 0)) uses GPIO2 and sets the first LED red. // ws2812.writergb(3, string.char(0, 0, 255):rep(10)) uses GPIO0 and sets ten LEDs blue. // ws2812.writergb(4, string.char(0, 255, 0, 255, 255, 255)) first LED green, second LED white. static int ICACHE_FLASH_ATTR ws2812_writergb(lua_State* L) { const uint8_t pin = luaL_checkinteger(L, 1); size_t length; const char *rgb = luaL_checklstring(L, 2, &length); // dont modify lua-internal lstring - make a copy instead char *buffer = (char *)c_malloc(length); c_memcpy(buffer, rgb, length); // Ignore incomplete Byte triples at the end of buffer: length -= length % 3; // Rearrange R G B values to G R B order needed by WS2812 LEDs: size_t i; for (i = 0; i < length; i += 3) { const char r = buffer[i]; const char g = buffer[i + 1]; buffer[i] = g; buffer[i + 1] = r; } // Initialize the output pin platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); platform_gpio_write(pin, 0); // Send the buffer os_intr_lock(); ws2812_write(pin, (uint8_t*) buffer, length); os_intr_unlock(); c_free(buffer); return 0; }
// Lua: mode( pin, mode, type, function ) static int lgpio_mode( lua_State* L ) { unsigned mode; unsigned pin; size_t sl; int type = GPIO_INTR_DISABLE; pin = luaL_checkinteger( L, 1 ); //MOD_CHECK_ID( gpio, pin ); mode = luaL_checkinteger( L, 2 ); if ( mode!=OUTPUT && mode!=INPUT && mode!=PLATFORM_INTERRUPT && mode!= INOUT) return luaL_error( L, "wrong arg type" ); if(pin==0 && mode==PLATFORM_INTERRUPT) return luaL_error( L, "no interrupt for D0" ); if(lua_gettop(L) > 2) { const char *str = luaL_checklstring( L, 3, &sl ); if (str == NULL) { return luaL_error( L, "wrong arg type" ); } if(sl == 2 && strcmp(str, "up") == 0){ type = GPIO_INTR_POSEDGE; }else if(sl == 4 && strcmp(str, "down") == 0){ type = GPIO_INTR_NEGEDGE; }else if(sl == 4 && strcmp(str, "both") == 0){ type = GPIO_INTR_ANYEDGE; }else if(sl == 3 && strcmp(str, "low") == 0){ type = GPIO_INTR_LOW_LEVEL; }else if(sl == 4 && strcmp(str, "high") == 0){ type = GPIO_INTR_HIGH_LEVEL; }else{ type = GPIO_INTR_DISABLE; printf("==> 6\n"); } } if (lua_gettop(L) > 3) { if (lua_type(L, 4) == LUA_TFUNCTION || lua_type(L, 4) == LUA_TLIGHTFUNCTION){ lua_pushvalue(L, 4); // copy argument (func) to the top of stack if(gpio_cb_ref[pin] != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, gpio_cb_ref[pin]); gpio_cb_ref[pin] = luaL_ref(L, LUA_REGISTRYINDEX); } } #ifdef GPIO_INTERRUPT_ENABLE gL = L; // save to local gL, for callback function if (mode!=PLATFORM_INTERRUPT){ // disable interrupt if(gpio_cb_ref[pin] != LUA_NOREF){ luaL_unref(L, LUA_REGISTRYINDEX, gpio_cb_ref[pin]); } gpio_cb_ref[pin] = LUA_NOREF; } #endif int r = platform_gpio_mode( pin, mode, type ); if( r<0 ) return luaL_error( L, "wrong pin num." ); return 0; }
// Lua: ws2812.write(pin, "string") // Byte triples in the string are interpreted as G R B values. // This function does not corrupt your buffer. // // ws2812.write(4, string.char(0, 255, 0)) uses GPIO2 and sets the first LED red. // ws2812.write(3, string.char(0, 0, 255):rep(10)) uses GPIO0 and sets ten LEDs blue. // ws2812.write(4, string.char(255, 0, 0, 255, 255, 255)) first LED green, second LED white. static int ICACHE_FLASH_ATTR ws2812_writegrb(lua_State* L) { const uint8_t pin = luaL_checkinteger(L, 1); size_t length; const char *buffer = luaL_checklstring(L, 2, &length); // Initialize the output pin platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); platform_gpio_write(pin, 0); // Send the buffer os_intr_lock(); ws2812_write(pin, (uint8_t*) buffer, length); os_intr_unlock(); return 0; }
uint8_t platform_sigma_delta_close( uint8_t pin ) { if (pin < 1 || pin > NUM_GPIO) return 0; sigma_delta_stop(); // set GPIO input mode for this pin platform_gpio_mode( pin, PLATFORM_GPIO_INPUT, PLATFORM_GPIO_PULLUP ); // CONNECT GPIO TO PIN PAD GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[pin])), (GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[pin]))) &(~GPIO_PIN_SOURCE_MASK)) | GPIO_PIN_SOURCE_SET( GPIO_AS_PIN_SOURCE )); return 1; }
uint8_t platform_sigma_delta_setup( uint8_t pin ) { if (pin < 1 || pin > NUM_GPIO) return 0; sigma_delta_setup(); // set GPIO output mode for this pin platform_gpio_mode( pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT ); platform_gpio_write( pin, PLATFORM_GPIO_LOW ); // enable sigma-delta on this pin GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[pin])), (GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[pin]))) &(~GPIO_PIN_SOURCE_MASK)) | GPIO_PIN_SOURCE_SET( SIGMA_AS_PIN_SOURCE )); return 1; }
uint32_t platform_pwm_setup( unsigned pin, uint32_t frequency, unsigned duty ) { uint32_t clock; if ( pin < NUM_PWM) { platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); // disable gpio interrupt first if(!pwm_add(pin)) return 0; // pwm_set_duty(DUTY(duty), pin); pwm_set_duty(0, pin); pwms_duty[pin] = duty; pwm_set_freq((uint16_t)frequency, pin); } else { return 0; } clock = platform_pwm_get_clock( pin ); pwm_start(); return clock; }
// Init UART1 to be able to stream WS2812 data // We use GPIO2 as output pin static void ws2812_init() { // Configure UART1 // Set baudrate of UART1 to 3200000 WRITE_PERI_REG(UART_CLKDIV(1), UART_CLK_FREQ / 3200000); // Set UART Configuration No parity / 6 DataBits / 1 StopBits / Invert TX WRITE_PERI_REG(UART_CONF0(1), UART_TXD_INV | (1 << UART_STOP_BIT_NUM_S) | (1 << UART_BIT_NUM_S)); // Pull GPIO2 down platform_gpio_mode(4, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); platform_gpio_write(4, 0); // Waits 10us to simulate a reset os_delay_us(10); // Redirect UART1 to GPIO2 // Disable GPIO2 GPIO_REG_WRITE(GPIO_ENABLE_W1TC_ADDRESS, BIT2); // Enable Function 2 for GPIO2 (U1TXD) PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK); }
// Lua: ws2812.write(pin, "string") // Byte triples in the string are interpreted as R G B values and sent to the hardware as G R B. // ws2812.write(4, string.char(255, 0, 0)) uses GPIO2 and sets the first LED red. // ws2812.write(3, string.char(0, 0, 255):rep(10)) uses GPIO0 and sets ten LEDs blue. // ws2812.write(4, string.char(0, 255, 0, 255, 255, 255)) first LED green, second LED white. static int ICACHE_FLASH_ATTR ws2812_writergb(lua_State* L) { const uint8_t pin = luaL_checkinteger(L, 1); size_t length; char *buffer = (char *)luaL_checklstring(L, 2, &length); // Cast away the constness. // Initialize the output pin: platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); platform_gpio_write(pin, 0); // Ignore incomplete Byte triples at the end of buffer: length -= length % 3; // Rearrange R G B values to G R B order needed by WS2812 LEDs: size_t i; for (i = 0; i < length; i += 3) { const char r = buffer[i]; const char g = buffer[i + 1]; buffer[i] = g; buffer[i + 1] = r; } // Do not remove these: os_delay_us(1); os_delay_us(1); // Send the buffer: os_intr_lock(); const char * const end = buffer + length; while (buffer != end) { uint8_t mask = 0x80; while (mask) { (*buffer & mask) ? send_ws_1(pin_num[pin]) : send_ws_0(pin_num[pin]); mask >>= 1; } ++buffer; } os_intr_unlock(); return 0; }
LUALIB_API int luaopen_mcu( lua_State *L ) { platform_spi_setup( 1, PLATFORM_SPI_MASTER, PLATFORM_SPI_CPOL_LOW, PLATFORM_SPI_CPHA_LOW, PLATFORM_SPI_DATABITS_8, 0); platform_gpio_mode( PIN_GPIO2, PLATFORM_GPIO_INPUT, PLATFORM_GPIO_PULLUP ); //GPIO2 => pin = 4 /* int i; for(i=0;i<4;i++){ gpio_cb_ref[i] = LUA_NOREF; } platform_gpio_init(intr_callback); //luaL_register( L, AUXLIB_MCU, mcu_map ); // Add constants MOD_REG_NUMBER( L, "PUMP_IN", OUT_PUMP_IN ); MOD_REG_NUMBER( L, "PUMP_OUT", OUT_PUMP_OUT ); MOD_REG_NUMBER( L, "PUMP1", OUT_PUMP1 ); MOD_REG_NUMBER( L, "PUMP2", OUT_PUMP2 ); MOD_REG_NUMBER( L, "PUMP3", OUT_PUMP3 ); MOD_REG_NUMBER( L, "HEATER", OUT_HEATER ); MOD_REG_NUMBER( L, "FAN", OUT_FAN ); MOD_REG_NUMBER( L, "SPRAY", OUT_SPRAY ); MOD_REG_NUMBER( L, "LCD", OUT_LCD ); MOD_REG_NUMBER( L, "LED", OUT_LED ); MOD_REG_NUMBER( L, "PWLED", OUT_PWLED ); MOD_REG_NUMBER( L, "BTN1", IN_BTN1 ); MOD_REG_NUMBER( L, "BTN2", IN_BTN2 ); MOD_REG_NUMBER( L, "BTN3", IN_BTN3 ); MOD_REG_NUMBER( L, "BTN4", IN_BTN4 ); MOD_REG_NUMBER( L, "TEMP", IN_TEMP ); MOD_REG_NUMBER( L, "HUM", IN_HUM ); MOD_REG_NUMBER( L, "PH", IN_PH ); MOD_REG_NUMBER( L, "EC", IN_EC ); MOD_REG_NUMBER( L, "WATER", IN_WATER ); MOD_REG_NUMBER( L, "CAM", IN_CAM ); return 1; */ return 0; }
// 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; }
static void ICACHE_FLASH_ATTR dht22_task(os_event_t *e) { DHT_DBG("dht22_task"); if(e->sig != SIG_DHT) return; //put dht pin on output mode with pullup platform_gpio_mode(DHT_PIN,PLATFORM_GPIO_OUTPUT,PLATFORM_GPIO_PULLUP); uint64_t data; os_memset(&data,0,sizeof(uint64_t)); //init sequence platform_gpio_write(DHT_PIN,1); delay_ms(5); platform_gpio_write(DHT_PIN,0); delay_ms(1); platform_gpio_write(DHT_PIN,1); //enable input reading platform_gpio_mode(DHT_PIN,PLATFORM_GPIO_INPUT,PLATFORM_GPIO_PULLUP); //find ACK start //wait pin to drop int timeout = 100; while(platform_gpio_read(DHT_PIN) == 1 && timeout > 0) { os_delay_us(5); timeout-=5; } if(timeout==0) { DHT_DBG("\tACK start timeout"); return; } //find ACK end //wait pin to rise timeout = 100; while(platform_gpio_read(DHT_PIN) == 0 && timeout > 0) { os_delay_us(10); timeout-=10; } if(timeout==0) { DHT_DBG("\tACK end timeout"); return; } //continue to read data while(1) { //wait pin to go low, signaling next bit timeout = 200; while(platform_gpio_read(DHT_PIN) == 1 && timeout > 0) { os_delay_us(5); timeout-=5; } if(timeout==0) { //NODE_DBG("\tdata signal timeout"); break; } //wait start of bit timeout = 200; while(platform_gpio_read(DHT_PIN) == 0 && timeout > 0) { os_delay_us(5); timeout-=5; } if(timeout==0) { //NODE_DBG("\tdata bit start timeout"); break; } //mearure pulse lenght timeout = 200; while(platform_gpio_read(DHT_PIN) == 1 && timeout > 0) { os_delay_us(5); timeout-=5; } if(timeout==0) { //NODE_DBG("\tdata bit read timeout"); break; } int pulseWidth = 200 - timeout; //shift data data <<= 1; if(pulseWidth > 40) { //bit is 1 data |=1; } else { //bit is 0 //nothing to do } } //do the math float temp_p, hum_p; uint8_t data_part[5]; data_part[4] = ((uint8_t*)&data)[0]; data_part[3] = ((uint8_t*)&data)[1]; data_part[2] = ((uint8_t*)&data)[2]; data_part[1] = ((uint8_t*)&data)[3]; data_part[0] = ((uint8_t*)&data)[4]; int checksum = (data_part[0] + data_part[1] + data_part[2] + data_part[3]) & 0xFF; if (data_part[4] == checksum) { // yay! checksum is valid hum_p = data_part[0] * 256 + data_part[1]; hum_p /= 10; temp_p = (data_part[2] & 0x7F)* 256 + data_part[3]; temp_p /= 10.0; if (data_part[2] & 0x80) temp_p *= -1; read.temp = temp_p; read.hum = hum_p; } }
//rc.send(0,267715,24,185,1) --GPIO, code, bits, pulselen, protocol static int ICACHE_FLASH_ATTR rc_send(lua_State* L) { const uint8_t pin = luaL_checkinteger(L, 1); platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT); //platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_PULLUP); //platform_gpio_mode(pin, PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_PULLDOWN); platform_gpio_write(pin, 0); long code = luaL_checklong(L, 2); //const uint8_t bits = luaL_checkinteger(L, 3); uint8_t bits = luaL_checkinteger(L, 3); const uint8_t pulseLen = luaL_checkinteger(L, 4); const uint8_t Protocol = luaL_checkinteger(L, 5); const uint8_t repeat = luaL_checkinteger(L, 6); NODE_ERR("pulseLen:%d\n",pulseLen); NODE_ERR("Protocol:%d\n",Protocol); NODE_ERR("repeat:%d\n",repeat); NODE_ERR("send:"); int c,k,nRepeat; bits = bits-1; for (c = bits; c >= 0; c--) { k = code >> c; if (k & 1) NODE_ERR("1"); else NODE_ERR("0"); } NODE_ERR("\n"); for (nRepeat=0; nRepeat<repeat; nRepeat++) { for (c = bits; c >= 0; c--) { k = code >> c; if (k & 1){ //send1 if(Protocol==1){ transmit(pin,pulseLen,3,1); }else if(Protocol==2){ transmit(pin,pulseLen,2,1); }else if(Protocol==3){ transmit(pin,pulseLen,9,6); } } else{ //send0 if(Protocol==1){ transmit(pin,pulseLen,1,3); }else if(Protocol==2){ transmit(pin,pulseLen,1,2); }else if(Protocol==3){ transmit(pin,pulseLen,4,11); } } } //sendSync(); if(Protocol==1){ transmit(pin,pulseLen,1,31); }else if(Protocol==2){ transmit(pin,pulseLen,1,10); }else if(Protocol==3){ transmit(pin,pulseLen,1,71); } } return 1; }
static int16_t ucg_com_esp8266_hw_spi(ucg_t *ucg, int16_t msg, uint16_t arg, uint8_t *data) { switch(msg) { case UCG_COM_MSG_POWER_UP: /* "data" is a pointer to ucg_com_info_t structure with the following information: */ /* ((ucg_com_info_t *)data)->serial_clk_speed value in nanoseconds */ /* ((ucg_com_info_t *)data)->parallel_clk_speed value in nanoseconds */ /* setup pins */ // we assume that the SPI interface was already initialized // just care for the /CS and D/C pins //platform_gpio_write( ucg->pin_list[0], value ); if ( ucg->pin_list[UCG_PIN_RST] != UCG_PIN_VAL_NONE ) platform_gpio_mode( ucg->pin_list[UCG_PIN_RST], PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT ); platform_gpio_mode( ucg->pin_list[UCG_PIN_CD], PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT ); if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE ) platform_gpio_mode( ucg->pin_list[UCG_PIN_CS], PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT ); break; case UCG_COM_MSG_POWER_DOWN: break; case UCG_COM_MSG_DELAY: delayMicroseconds(arg); break; case UCG_COM_MSG_CHANGE_RESET_LINE: if ( ucg->pin_list[UCG_PIN_RST] != UCG_PIN_VAL_NONE ) platform_gpio_write( ucg->pin_list[UCG_PIN_RST], arg ); break; case UCG_COM_MSG_CHANGE_CS_LINE: if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE ) platform_gpio_write( ucg->pin_list[UCG_PIN_CS], arg ); break; case UCG_COM_MSG_CHANGE_CD_LINE: platform_gpio_write( ucg->pin_list[UCG_PIN_CD], arg ); break; case UCG_COM_MSG_SEND_BYTE: platform_spi_send( 1, 8, arg ); break; case UCG_COM_MSG_REPEAT_1_BYTE: CACHED_TRANSFER(data[0], 1); break; case UCG_COM_MSG_REPEAT_2_BYTES: CACHED_TRANSFER((data[0] << 8) | data[1], 2); break; case UCG_COM_MSG_REPEAT_3_BYTES: while( arg > 0 ) { platform_spi_transaction( 1, 0, 0, 24, (data[0] << 16) | (data[1] << 8) | data[2], 0, 0, 0 ); arg--; } break; case UCG_COM_MSG_SEND_STR: CACHED_TRANSFER(*data++, 1); break; case UCG_COM_MSG_SEND_CD_DATA_SEQUENCE: while(arg > 0) { if ( *data != 0 ) { /* set the data line directly, ignore the setting from UCG_CFG_CD */ if ( *data == 1 ) { platform_gpio_write( ucg->pin_list[UCG_PIN_CD], 0 ); } else { platform_gpio_write( ucg->pin_list[UCG_PIN_CD], 1 ); } } data++; platform_spi_send( 1, 8, *data ); data++; arg--; } break; } return 1; }
int ICACHE_FLASH_ATTR dht22_read(dht_data *read){ DHT_DBG("dht22_read"); //put dht pin on output mode with pullup platform_gpio_mode(DHT_PIN,PLATFORM_GPIO_OUTPUT,PLATFORM_GPIO_PULLUP); uint64_t data; os_memset(&data,0,sizeof(uint64_t)); //init sequence platform_gpio_write(DHT_PIN,1); delay_ms(5); platform_gpio_write(DHT_PIN,0); delay_ms(1); platform_gpio_write(DHT_PIN,1); //enable input reading platform_gpio_mode(DHT_PIN,PLATFORM_GPIO_INPUT,PLATFORM_GPIO_PULLUP); //find ACK start //wait pin to drop int timeout = 100; while(platform_gpio_read(DHT_PIN) == 1 && timeout > 0) { os_delay_us(5); timeout-=5; } if(timeout==0){ DHT_DBG("\tACK start timeout"); return 0; } //find ACK end //wait pin to rise timeout = 100; while(platform_gpio_read(DHT_PIN) == 0 && timeout > 0) { os_delay_us(10); timeout-=10; } if(timeout==0){ DHT_DBG("\tACK end timeout"); return 0; } //continue to read data while(1){ //wait pin to go low, signaling next bit timeout = 200; while(platform_gpio_read(DHT_PIN) == 1 && timeout > 0) { os_delay_us(5); timeout-=5; } if(timeout==0){ //NODE_DBG("\tdata signal timeout"); break; } //wait start of bit timeout = 200; while(platform_gpio_read(DHT_PIN) == 0 && timeout > 0) { os_delay_us(5); timeout-=5; } if(timeout==0){ //NODE_DBG("\tdata bit start timeout"); break; } //mearure pulse lenght timeout = 200; while(platform_gpio_read(DHT_PIN) == 1 && timeout > 0) { os_delay_us(5); timeout-=5; } if(timeout==0){ //NODE_DBG("\tdata bit read timeout"); break; } int pulseWidth = 200 - timeout; //shift data data <<= 1; if(pulseWidth > 40) { //bit is 1 data |=1; } else{ //bit is 0 //nothing to do } } //do the math float temp_p, hum_p; uint8_t data_part[5]; data_part[4] = ((uint8_t*)&data)[0]; data_part[3] = ((uint8_t*)&data)[1]; data_part[2] = ((uint8_t*)&data)[2]; data_part[1] = ((uint8_t*)&data)[3]; data_part[0] = ((uint8_t*)&data)[4]; int checksum = (data_part[0] + data_part[1] + data_part[2] + data_part[3]) & 0xFF; if (data_part[4] == checksum) { // yay! checksum is valid hum_p = data_part[0] * 256 + data_part[1]; hum_p /= 10; temp_p = (data_part[2] & 0x7F)* 256 + data_part[3]; temp_p /= 10.0; if (data_part[2] & 0x80) temp_p *= -1; read->temp = temp_p; read->hum = hum_p; #ifdef DHT_DEBUG_ON char * buff = (char *)os_zalloc(64); c_sprintf(buff,"%f",read->temp); DHT_DBG("dht22_init temp : %s C",buff); os_memset(buff,0,64); c_sprintf(buff,"%f",read->hum); DHT_DBG("dht22_init humidity : %s %%",buff); os_free(buff); #endif } return 1; }