// 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 ICACHE_FLASH_ATTR read_timeout(void *arg) { os_intr_lock(); if (irparams.rawlen) { irparams.rcvstate = STATE_STOP; } os_intr_unlock(); }
// 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; }
void ICACHE_FLASH_ATTR ws2812_strip(uint8_t * buffer, uint16_t length) { uint16_t i; GPIO_OUTPUT_SET(GPIO_ID_PIN(0), 0); os_intr_lock(); for( i = 0; i < length; i++ ) { uint8_t mask = 0x80; uint8_t byte = buffer[i]; while (mask) { ( byte & mask ) ? SEND_WS_1() : SEND_WS_0(); mask >>= 1; } } os_intr_unlock(); }
// 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; }
/** Originally from: http://harizanov.com/2014/11/esp8266-powered-web-server-led-control-dht22-temperaturehumidity-sensor-reading/ Adapted from: https://github.com/adafruit/Adafruit_Python_DHT/blob/master/source/Raspberry_Pi/pi_dht_read.c LICENSE: // Copyright (c) 2014 Adafruit Industries // Author: Tony DiCola // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. */ static void ICACHE_FLASH_ATTR pollDHTCb(void * arg){ int counter = 0; int laststate = 1; int i = 0; int bits_in = 0; // int bitidx = 0; // int bits[250]; int data[100]; data[0] = data[1] = data[2] = data[3] = data[4] = 0; //disable interrupts, start of critical section os_intr_lock(); wdt_feed(); // Wake up device, 250ms of high GPIO_OUTPUT_SET(DHT_PIN, 1); delay_ms(20); // Hold low for 20ms GPIO_OUTPUT_SET(DHT_PIN, 0); delay_ms(20); // High for 40ms // GPIO_OUTPUT_SET(2, 1); GPIO_DIS_OUTPUT(DHT_PIN); os_delay_us(40); // Set pin to input with pullup // PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO2_U); // os_printf("Waiting for gpio2 to drop \n"); // wait for pin to drop? while (GPIO_INPUT_GET(DHT_PIN) == 1 && i < DHT_MAXCOUNT) { if (i >= DHT_MAXCOUNT) { goto fail; } i++; } // os_printf("Reading DHT\n"); // read data! for (i = 0; i < MAXTIMINGS; i++) { // Count high time (in approx us) counter = 0; while (GPIO_INPUT_GET(DHT_PIN) == laststate) { counter++; os_delay_us(1); if (counter == 1000) break; } laststate = GPIO_INPUT_GET(DHT_PIN); if (counter == 1000) break; // store data after 3 reads if ((i > 3) && (i % 2 == 0)) { // shove each bit into the storage bytes data[bits_in / 8] <<= 1; if (counter > BREAKTIME) { //os_printf("1"); data[bits_in / 8] |= 1; } else { //os_printf("0"); } bits_in++; } } //Re-enable interrupts, end of critical section os_intr_unlock(); if (bits_in < 40) { os_printf("Got too few bits: %d should be at least 40", bits_in); goto fail; } int checksum = (data[0] + data[1] + data[2] + data[3]) & 0xFF; os_printf("DHT: %02x %02x %02x %02x [%02x] CS: %02x", data[0], data[1],data[2],data[3],data[4],checksum); if (data[4] != checksum) { os_printf("Checksum was incorrect after %d bits. Expected %d but got %d", bits_in, data[4], checksum); goto fail; } reading.temperature = scale_temperature(data); reading.humidity = scale_humidity(data); os_printf("Temp = %d *C, Hum = %d %%\n", (int)(reading.temperature * 100), (int)(reading.humidity * 100)); reading.success = 1; return; fail: os_printf("Failed to get reading, dying\n"); reading.success = 0; }
static void ICACHE_FLASH_ATTR read_timeout(void *arg) { os_intr_lock(); irparams.rcvstate = STATE_IDLE; os_intr_unlock(); }