Example #1
0
// Lua: read( pin )
static int lgpio_read( lua_State* L )
{
  unsigned pin;
  
  pin = luaL_checkinteger( L, 1 );
  //MOD_CHECK_ID( gpio, pin );

  unsigned level = platform_gpio_read( pin );
  lua_pushinteger( L, level );
  return 1; 
}
Example #2
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;
}
Example #3
0
static void transfer(uint8_t* send, uint8_t * recv, uint8_t len)
{
    while(platform_gpio_read( PIN_GPIO2 ) == 0);

    if(recv == NULL) {
        while(len --) {
            platform_spi_send_recv(1, *send++);
        }
        return;
    }
    while(len --) {
        *recv++ = platform_spi_send_recv(1, *send++);
    }
}
Example #4
0
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;


    }


}
Example #5
0
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;
}