//****************************************** // Servo pulse on multiple pins // call this from tmr about every 20ms // Lua: servo.pulse(reduce_each_by_us,reduce_first_by_us) static int lservo_pulse( lua_State* L ) { int i,d,d1; d1=servo_reduce_first_by_us; uint32_t tt[MAX_NUM_SERVOS],t1; for(i=servo_min;i<MAX_NUM_SERVOS;i++){ platform_gpio_write(servo_pin[i], 1); tt[i]=system_get_time(); os_delay_us(DELAY_SHIFT); } for(i=servo_min;i<MAX_NUM_SERVOS;i++){ t1=system_get_time(); d = servo_delay[i]+tt[i]-t1-servo_reduce_each_by_us-d1; d1=0; os_delay_us(d); platform_gpio_write(servo_pin[i], 0); tt[i]-=system_get_time(); } if(servo_stats){ lua_newtable(L); for(i=servo_min;i<MAX_NUM_SERVOS;i++){ lua_pushinteger( L, servo_pin[i] ); lua_pushinteger( L, tt[i] ); lua_settable(L, -3); } return 1; } else return 0; }
// 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; }
static void lu8g_digital_write( u8g_t *u8g, uint8_t pin_index, uint8_t value ) { uint8_t pin; pin = u8g->pin_list[pin_index]; if ( pin != U8G_PIN_NONE ) platform_gpio_write( pin, value ); }
/*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; }
// Lua: write( pin, level ) static int lgpio_write( lua_State* L ) { unsigned level; unsigned pin; pin = luaL_checkinteger( L, 1 ); //MOD_CHECK_ID( gpio, pin ); level = luaL_checkinteger( L, 2 ); if ( level!=HIGH && level!=LOW ) return luaL_error( L, "wrong arg type" ); platform_gpio_write(pin, level); 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_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; }
// 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; }
/*Lua: result = hx711.read()*/ static int ICACHE_FLASH_ATTR hx711_read(lua_State* L) { uint32_t i; int32_t data = 0; //TODO: double check init has happened first. //wakeup hx711 platform_gpio_write(clk_pin,0); //wait for data ready. or time out. //TODO: set pin inturrupt and come back to it. This may take up to 1/10 sec // or maybe just make an async version too and have both available. system_soft_wdt_feed(); //clear WDT... this may take a while. for (i = 0; i<HX711_MAX_WAIT && platform_gpio_read(data_pin)==1;i++){ asm ("nop"); } //Handle timeout error if (i>=HX711_MAX_WAIT) { return luaL_error( L, "ADC timeout!", ( unsigned )0 ); } for (i = 0; i<24 ; i++){ //clock in the 24 bits platform_gpio_write(clk_pin,1); platform_gpio_write(clk_pin,0); data = data<<1; if (platform_gpio_read(data_pin)==1) { data = i==0 ? -1 : data|1; //signextend the first bit } } //add 25th clock pulse to prevent protocol error (probably not needed // since we'll go to sleep immediately after and reset on wakeup.) platform_gpio_write(clk_pin,1); platform_gpio_write(clk_pin,0); //sleep platform_gpio_write(clk_pin,1); lua_pushinteger( L, data ); 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; }
//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; }
void transmit(int pin, int pulseLen, int nHighPulses, int nLowPulses) { platform_gpio_write(pin, 1); os_delay_us(pulseLen*nHighPulses); platform_gpio_write(pin, 0); os_delay_us(pulseLen*nLowPulses); }
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; } }
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; }