Пример #1
0
/// @brief Initialize TFT
/// @return diplay ID 9341
MEMSPACE
window *tft_init(void)
{
    TFT_RST_INIT;	// RESET PIN
    TFT_INIT;		// DATA/COMMAND
    TFT_CS_INIT;	// CHIP SELLECT
	hspi_cs_disable(TFT_CS_PIN);
	// TFT_CS_DISABLE;

	// Start with slow clock so tft_readId works
	// This is the only function that fails at less then 1.
	// tft_readId is the ONLY SPI bus command that needs this.
	// Nomal reads work fine.
	tft_spi_init(2);
    TFT_RST_ACTIVE;	
    os_delay_us(10000);
    TFT_RST_DEACTIVE;
    os_delay_us(1000);

	/* Adafruit 9341 TFT Display Initialization */
    tft_configRegister();

	/* Read the TFT ID value */
    tft_ID = tft_readId();

	// fast SPI
	tft_spi_init(1);

	/* Setup the master window */
    tft_window_init(tft, TFT_XOFF, TFT_YOFF, TFT_W, TFT_H);
    tft_setRotation(0);
    tft_fillWin(tft, tft->bg);

    return (tft);
}
Пример #2
0
int dht11_read(int gpio, int* temp, int* rh) {
  unsigned int cs, expected_cs;
  int max_cycles = 100000, thresh;
  /* Get DHT11's attention. */
  set_gpio(gpio, 0);
  os_delay_us(20 * 1000);
  /* Switch GPIO pin to input and let it stabilize. */
  read_gpio_pin(gpio);
  os_delay_us(1);
  /* Starting with high, wait for first 80us of L. */
  await_change(gpio, &max_cycles);
  thresh = await_change(gpio, &max_cycles); /* First 80 us low. */
  /* Then 80us of H. */
  thresh += await_change(gpio, &max_cycles); /* Then 80 us high. */
  if (max_cycles <= 0) return 0;
  /* We use this to calibrate our delay: take average of the two
   * and further divide by half to get number of cycles that represent 40us. */
  thresh /= 4;
  /* Now read the data. */
  cs = (*rh = dht11_read_bits(gpio, 8, thresh, &max_cycles));
  cs += dht11_read_bits(gpio, 8, thresh, &max_cycles); /* Always 0. */
  cs += (*temp = dht11_read_bits(gpio, 8, thresh, &max_cycles));
  cs += dht11_read_bits(gpio, 8, thresh, &max_cycles); /* Always 0. */
  expected_cs = dht11_read_bits(gpio, 8, thresh, &max_cycles);
  /* printf("%d %d %d==%d %d %d\r\n", *temp, *rh, cs, expected_cs, thresh,
   * max_cycles); */
  return (max_cycles > 0 && cs == expected_cs);
}
Пример #3
0
/**
 * close valve
 */
LOCAL void ICACHE_FLASH_ATTR valveClose(SleeperStateT* sleeperState)
{
  // start generator, preset valve direction to close and wait for generator voltage to stablelize
  GPIO_OUTPUT_SET(GENERATOR_GPIO,  1);
  GPIO_OUTPUT_SET(OPEN_VALVE_GPIO, 1); // 1 -> VOUT2 = H
  os_delay_us(5000); // 5 ms -> us

  // close valve by enabling H-bridge
  GPIO_OUTPUT_SET(OPERATE_VALVE_GPIO, 1);
  os_delay_us(62500); // 62.5 ms -> us

  // short circuit valve current
  GPIO_OUTPUT_SET(OPERATE_VALVE_GPIO, 0);
  os_delay_us(1000); // 1 ms -> us

  // done, go to passive state
  valveDriverShutdown();

  // update state
  sleeperState->rtcMem.valveOpen = false;
  if (now > sleeperState->rtcMem.valveOpenTime)
  {
    sleeperState->rtcMem.totalOpenDuration += (now - sleeperState->rtcMem.valveOpenTime)/1000;
  }
  ets_uart_printf("valveClose\r\n");
}
Пример #4
0
/**
 * open valve
 */
LOCAL void ICACHE_FLASH_ATTR valveOpen(SleeperStateT* sleeperState)
{
  // start generator, preset valve direction to open and wait for generator voltage to stablelize
  GPIO_OUTPUT_SET(GENERATOR_GPIO,  1);
  GPIO_OUTPUT_SET(OPEN_VALVE_GPIO, 0);  // 0 -> VOUT1 = H
  os_delay_us(5000); // 5 ms -> us

  // open valve by enabling H-bridge
  GPIO_OUTPUT_SET(OPERATE_VALVE_GPIO, 1);
  os_delay_us(VALVE_OPEN_PULSE_DURATION); // (250 ms) -> us

  // short circuit valve current
  GPIO_OUTPUT_SET(OPERATE_VALVE_GPIO, 0);
  os_delay_us(1000); // 1 ms -> us

  // done, go to passive state
  valveDriverShutdown();

  // update state
  if (!sleeperState->rtcMem.valveOpen)
  {
    sleeperState->rtcMem.valveOpen = true;
    sleeperState->rtcMem.totalOpenCount++;
  }
  sleeperState->rtcMem.valveOpenTime = now;
  ets_uart_printf("valveOpen\r\n");
}
Пример #5
0
// Byte triples in the buffer are interpreted as R G B values and sent to the hardware as G R B.
int ICACHE_FLASH_ATTR ws2812_writergb(uint8_t gpio, char *buffer, size_t length)
{
    // Initialize the output pin:
    pinMode(gpio, OUTPUT);
    digitalWrite(gpio, 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:
    ets_intr_lock();
    const char * const end = buffer + length;
    while (buffer != end) {
        uint8_t mask = 0x80;
        while (mask) {
            (*buffer & mask) ? send_ws_1(gpio) : send_ws_0(gpio);
            mask >>= 1;
        }
        ++buffer;
    }
    ets_intr_unlock();
}
Пример #6
0
// Byte triples in the buffer are interpreted as G R B values and sent to the hardware as G R B.
int ICACHE_FLASH_ATTR ws2812_writegrb(uint8_t gpio, char *buffer, size_t length)
{
    // Initialize the output pin:
    pinMode(gpio, OUTPUT);
    digitalWrite(gpio, 0);

    // Ignore incomplete Byte triples at the end of buffer:
    length -= length % 3;

    // Do not remove these:
    os_delay_us(1);
    os_delay_us(1);

    // Send the buffer:
    ets_intr_lock();
    const char * const end = buffer + length;
    while (buffer != end) {
        uint8_t mask = 0x80;
        while (mask) {
            (*buffer & mask) ? send_ws_1(gpio) : send_ws_0(gpio);
            mask >>= 1;
        }
        ++buffer;
    }
    ets_intr_unlock();
}
Пример #7
0
/**
 * open valve
 */
LOCAL void ICACHE_FLASH_ATTR valveOpen(SleeperStateT* sleeperState)
{
  // discharge capacitor as much as possible while powering up generator (this may also close valve if still open)
  GPIO_OUTPUT_SET(CLOSE_VALVE_GPIO, 1);

  // start generator
  GPIO_OUTPUT_SET(GENERATOR_GPIO, 1);
  os_delay_us(50000); // 50 ms -> us

  // stop discharging capacitor
  GPIO_OUTPUT_SET(CLOSE_VALVE_GPIO, 0);
  os_delay_us(20); // 20 us

  // open latching valve by charing capacitor
  GPIO_OUTPUT_SET(OPEN_VALVE_GPIO, 0);
  os_delay_us(VALVE_OPEN_PULSE_DURATION); // (250 ms) -> us

  // disable power to valve and disable generator
  GPIO_DIS_OUTPUT(OPEN_VALVE_GPIO);
  GPIO_OUTPUT_SET(GENERATOR_GPIO, 0);

  // update state
  if (!sleeperState->rtcMem.valveOpen)
  {
    sleeperState->rtcMem.valveOpen = true;
    sleeperState->rtcMem.totalOpenCount++;
  }
  sleeperState->rtcMem.valveOpenTime = now;
  ets_uart_printf("valveOpen\r\n");
}
Пример #8
0
// Perform the onewire reset function.  We will wait up to 250uS for
// the bus to come high, if it doesn't then it is broken or shorted
// and we return a 0;
// Returns 1 if a device asserted a presence pulse, 0 otherwise.
uint32 ICACHE_FLASH_ATTR onewire_reset() {
	uint32 result;
	uint8 retries = 125;

	// Disable output on the pin.
	GPIO_DIS_OUTPUT(ONEWIRE_PIN);

	// Wait for the bus to get high (which it should because of the pull-up resistor).
	do {
		if (--retries == 0) {
			return 0;
		}
		os_delay_us(2);
	} while (!GPIO_INPUT_GET(ONEWIRE_PIN));

	// Transmit the reset pulse by pulling the bus low for at least 480 us.
	GPIO_OUTPUT_SET(ONEWIRE_PIN, 0);
	os_delay_us(500);

	// Release the bus, and wait, then check for a presence pulse, which should start 15-60 us after the reset, and will last 60-240 us.
	// So 65us after the reset the bus must be high.
	GPIO_DIS_OUTPUT(ONEWIRE_PIN);
	os_delay_us(65);
	result = !GPIO_INPUT_GET(ONEWIRE_PIN);

	// After sending the reset pulse, the master (we) must wait at least another 480 us.
	os_delay_us(490);
	return result;
}
Пример #9
0
//******************************************
// 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;
}
Пример #10
0
/**
 * The main entry point in an ESP8266 application.
 * It is where the logic of ESP8266 starts.
 */
void user_init() {
  system_timer_reinit(); // use microsecond os_timer_*

  // Initialize the UART devices
  int defaultBaudRate = 9600;
#ifdef DEFAULT_CONSOLE_BAUDRATE
  defaultBaudRate = DEFAULT_CONSOLE_BAUDRATE;
#endif

  uart_init(defaultBaudRate, defaultBaudRate);

  //uart_init(BIT_RATE_9600, BIT_RATE_9600);
  os_delay_us(1000); // make sure there's a gap on uart output
  UART_SetPrintPort(1);
  system_set_os_print(1);
  os_printf("\n\n\n\n");
  os_delay_us(1000);

  // Dump the restart exception information.
  dumpRestart();
  os_printf("Heap: %d\n", system_get_free_heap_size());
  os_printf("Variables: %d @%dea = %dbytes\n", JSVAR_CACHE_SIZE, sizeof(JsVar),
      JSVAR_CACHE_SIZE * sizeof(JsVar));
  os_printf("Time sys=%u rtc=%u\n", system_get_time(), system_get_rtc_time());

  // Register the ESP8266 initialization callback.
  system_init_done_cb(initDone);

  os_timer_setfn(&mainLoopSuspendTimer, enableMainLoop, NULL);
}
Пример #11
0
/**
 * Receive byte from the I2C bus 
 * returns the byte 
 */
uint8 ICACHE_FLASH_ATTR
i2c_readByte(void)
{
    uint8 data = 0;
    uint8 data_bit;
    uint8 i;

    i2c_sda(1);

    for (i = 0; i < 8; i++)
    {
        os_delay_us(I2C_SLEEP_TIME);
        i2c_sck(0);
        os_delay_us(I2C_SLEEP_TIME);

        i2c_sck(1);
        os_delay_us(I2C_SLEEP_TIME);

        data_bit = i2c_read();
        os_delay_us(I2C_SLEEP_TIME);

        data_bit <<= (7 - i);
        data |= data_bit;
    }
    i2c_sck(0);
    os_delay_us(I2C_SLEEP_TIME);
    
    return data;
}
Пример #12
0
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]);}
		}
    }

}
Пример #13
0
void LCD_pulseEnable(uint8 _data){
    LCD_expanderWrite(_data | En);  // En high
    os_delay_us(1);     // enable pulse must be >450ns
    
    LCD_expanderWrite(_data & ~En); // En low
    os_delay_us(50);        // commands need > 37us to settle
}
Пример #14
0
void ICACHE_FLASH_ATTR io_powerkeep_release() {
	powerholdCount--;
	os_printf("Powerdown hold count: %d\n", powerholdCount);
	if (powerholdCount<=0) {
		//Okay, we're done; release power.
		os_printf("Powering down.\n");
		UART_WaitTxFifoEmpty(0, 5000);
		//This should disable PWM, which is needed to set the nightlight.
		io_rgbled_set_color(0,0,0);
		os_delay_us(10000);
		//Set nightlight to on or off.
		//ToDo: Read the enable value from flash config area. Is now hardcoded to on.
		enableNightlight(0);
		//Make GPIO0 high, to lower the chance of the ESP booting up in programming mode next time.
		gpio_output_set((1<<0), 0, (1<<0), 0);
#if NEW_BUTTON
		//Software fix for hardware problem... sometimes GPIO12 (=USB power detect) will mysteriously 
		//keep leaking power into the LDO enable input. Force low for a while to stop this from happening.
		gpio_output_set(0, (1<<12), (1<<12), 0);
		os_delay_us(1000);
		gpio_output_set(0, (1<<12), 0, (1<<12));
#endif
		//Kill power.
		gpio_output_set(0, (1<<HOLD_PIN), (1<<HOLD_PIN), 0);
		//We should be dead now. If not, getting killed by the wdt may be a good idea anyway.
		while(1);
	}
}
Пример #15
0
void ICACHE_FLASH_ATTR dmx_task(os_event_t *events) {
	int i;
	
	if(twinkl_has_changes()) {
		INFO("Updating DMX channels\n");
		twinkl_render(dmx_channels);
	}

	//INFO("Sending DMX channels\n");

	//Space for break
	PIN_FUNC_SELECT(pin_mux[dmx_tx_pin], FUNC_GPIO2);	
	gpio_output_set(0, BIT2, BIT2, 0); 
	os_delay_us(125);
	
	//Mark After Break
	gpio_output_set(BIT2, 0, BIT2, 0);
	os_delay_us(50);

	//Looks the wrong way round, but reduces jitter somehow
	//Do not touch.
	PIN_FUNC_SELECT(pin_mux[dmx_tx_pin], FUNC_U1TXD_BK);	
	uart_tx_one_char(1, 0);

	for(i = 0; i < TWINKL_CHANNEL_COUNT; i++) {
		uart_tx_one_char(1, dmx_channels[i]);
	}

	//INFO("Done sending DMX channels\n");
	if(udp_server != NULL) {
		os_timer_arm(&dmx_update_timer, dmx_refresh_delay, 0);
	}
}
Пример #16
0
/**
 * close valve
 */
LOCAL void ICACHE_FLASH_ATTR valveClose(SleeperStateT* sleeperState)
{
  // start generator
  GPIO_OUTPUT_SET(GENERATOR_GPIO, 1);
  os_delay_us(1000); // 1 ms -> us

  // recharge capacitor while bypassing valve
  GPIO_OUTPUT_SET(CAPACITOR_GPIO, 0);
  os_delay_us(50000); // 50 ms -> us

  // stop capacitor charging and disable generator
  GPIO_DIS_OUTPUT(CAPACITOR_GPIO);
  os_delay_us(20); // 20 us
  GPIO_OUTPUT_SET(GENERATOR_GPIO, 0);

  // close latching valve by discharging capacitor
  GPIO_OUTPUT_SET(CLOSE_VALVE_GPIO, 1);
  os_delay_us(62500); // 62.5 ms -> us

  // continue discharging capacitor until os shutdown

  // update state
  sleeperState->rtcMem.valveOpen = false;
  if (now > sleeperState->rtcMem.valveOpenTime)
  {
    sleeperState->rtcMem.totalOpenDuration += (now - sleeperState->rtcMem.valveOpenTime)/1000;
  }
  ets_uart_printf("valveClose\r\n");
}
Пример #17
0
DHI2C_STATUS ICACHE_FLASH_ATTR pcf8574_hd44780_write(int sda, int scl, const char *text, unsigned int len) {
	DHI2C_STATUS status;
	int i;
	const static char init_data[] = {
		0b00101100, // function set
		0b00001100, // display on
		0b00000001, // cursor clear
		0b00000110  // entry mode set
	};

	// clear enable
	if((status = pcf8574_set(sda, scl, ~((char)(PIN_E | PIN_RW)))) != DHI2C_OK)
		return status;

	// initialization
	for(i = 0; i < 3; i++) {
		if((status = pcf8574_hd44780_write_half(sda, scl, 0b0011, 1)) != DHI2C_OK)
			return status;
		os_delay_us(5000);
	}
	if((status = pcf8574_hd44780_write_half(sda, scl, 0b0010, 1)) != DHI2C_OK)
		return status;
	os_delay_us(100);

	// configure
	for(i = 0; i < sizeof(init_data); i++) {
		if((status = pcf8574_hd44780_write_byte(sda, scl, init_data[i], 1)) != DHI2C_OK)
			return status;
		os_delay_us(2000);
	}

	int line = 0;
	int ch = 0;
	// write text to display RAM
	for(i = 0; i < len; i++) {
		int nla = text[i] == '\n';
		int nlc = (i + 1 < len) ? (text[i] == '\\' && text[i + 1] == 'n') : 0;
		if(ch == 20 || nla || nlc) {
			line++;
			if(ch == 20 && (nla || nlc))
				line++;
			if(line > 3)
				break;
			if((status = pcf8574_hd44780_set_line(sda, scl, line)) != DHI2C_OK)
				return status;
			ch = 0;
			if(nlc)
				i++;
			if(nla || nlc)
				continue;
		}
		if((status = pcf8574_hd44780_write_byte(sda, scl, text[i], 0)) != DHI2C_OK)
			return status;
		ch++;
	}

	return DHI2C_OK;
}
Пример #18
0
/**
 * I2C Stop signal 
 */
void ICACHE_FLASH_ATTR
i2c_stop(void)
{
    os_delay_us(I2C_SLEEP_TIME);
    i2c_sck(1);
    os_delay_us(I2C_SLEEP_TIME);
    i2c_sda(1);
    os_delay_us(I2C_SLEEP_TIME);
}
Пример #19
0
//
// Read a bit. Port and bit is used to cut lookup time and provide
// more certain timing.
//
uint32 ICACHE_FLASH_ATTR onewire_read_bit(void) {
	uint32 r;
	GPIO_OUTPUT_SET(ONEWIRE_PIN, 0);
	os_delay_us(3);
	GPIO_DIS_OUTPUT(ONEWIRE_PIN);
	os_delay_us(10);
	r = GPIO_INPUT_GET(ONEWIRE_PIN);
	os_delay_us(53);
	return r;
}
Пример #20
0
/******************************************************************************
 * FunctionName : user_mvh3004_burst_read
 * Description  : burst read mvh3004's internal data
 * Parameters   : uint8 addr - mvh3004's address
 *                uint8 *pData - data point to put read data
 *                uint16 len - read length
 * Returns      : bool - true or false
*******************************************************************************/
LOCAL bool ICACHE_FLASH_ATTR
peri_iaq_single_burst_read(uint8 addr, uint8 *pData, uint16 len)
{
    uint8 ack;
    uint16 i;

    i2c_start();
    i2c_writeByte(addr);
    ack = i2c_check_ack();
    PRINTF("the first ack is:%d\n",ack);
    if (ack==0) {
        os_printf("addr1 not ack when tx write cmd \n");
        i2c_stop();
        return false;
    }


      i2c_writeByte(0x52);
      ack = i2c_check_ack();
      PRINTF("the second ack is:%d\n",ack);

      if (ack==0) {
          os_printf("not ack when write 0x52 \n");
          i2c_stop();
          return false;
      }

    i2c_start();
    i2c_writeByte(addr + 1);
    ack = i2c_check_ack();
    PRINTF("the third ack is:%d\n",ack);
    if (ack==0) {
        os_printf("addr2 not ack when tx write cmd \n");
        i2c_stop();
        return false;
    }
    os_delay_us(1);
    for (i = 0; i < len; i++) {
        pData[i] = i2c_readByte();
        if(i==3)
            i2c_send_ack(0);
        else
        	i2c_send_ack(1);


        os_delay_us(1);
    	PRINTF("bytes_readed:%d\n", pData[i]);
    }

    i2c_stop();


    return true;
}
Пример #21
0
int supla_ds18b20_read_bit(void)
{
    int r;
    GPIO_OUTPUT_SET( supla_w1_pin, 0 );
    os_delay_us(3);
    GPIO_DIS_OUTPUT( supla_w1_pin );
    os_delay_us(10);
    r = GPIO_INPUT_GET( supla_w1_pin );
    os_delay_us(53);
    return r;
}
Пример #22
0
//=============================================================================
void ICACHE_FLASH_ATTR saveConfigs(void) {
	int result = -1;
	os_delay_us(100000);
	result = spi_flash_erase_sector(PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE);
	result = -1;
	os_delay_us(100000);
	result = spi_flash_write(
			(PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE,
			(uint32 *) &configs, sizeof(u_CONFIG));

	ets_uart_printf("Write W = %d\r\n", result);
}
Пример #23
0
//-----------------------------------------------------------------------------
// Command Data
//-----------------------------------------------------------------------------
void ICACHE_FLASH_ATTR
mjpwm_send_command(uint8_t pin_di, uint8_t pin_dcki, mjpwm_cmd_t command)
{
	uint8_t i;
	uint8_t command_data = *(uint8_t *) (&command);
	mjpwm_commands[pin_dcki] = command;

	// ets_intr_lock();
	// TStop > 12us.
	os_delay_us(12);
	// Send 12 DI pulse, after 6 pulse's falling edge store duty data, and 12
	// pulse's rising edge convert to command mode.
	mjpwm_di_pulse(pin_di, 12);
	// Delay >12us, begin send CMD data
	os_delay_us(12);
	// Send CMD data

	for (i = 0; i < 4; i++) {
		// DCK = 0;
		MJPWM_DIRECT_WRITE_LOW(pin_dcki);
		if (command_data & 0x80) {
			// DI = 1;
			MJPWM_DIRECT_WRITE_HIGH(pin_di);
		} else {
			// DI = 0;
			MJPWM_DIRECT_WRITE_LOW(pin_di);
		}
		// DCK = 1;
		MJPWM_DIRECT_WRITE_HIGH(pin_dcki);
		command_data = command_data << 1;
		if (command_data & 0x80) {
			// DI = 1;
			MJPWM_DIRECT_WRITE_HIGH(pin_di);
		} else {
			// DI = 0;
			MJPWM_DIRECT_WRITE_LOW(pin_di);
		}
		// DCK = 0;
		MJPWM_DIRECT_WRITE_LOW(pin_dcki);
		// DI = 0;
		MJPWM_DIRECT_WRITE_LOW(pin_di);
		command_data = command_data << 1;
	}

	// TStart > 12us. Delay 12 us.
	os_delay_us(12);
	// Send 16 DI pulse,at 14 pulse's falling edge store CMD data, and
	// at 16 pulse's falling edge convert to duty mode.
	mjpwm_di_pulse(pin_di, 16);
	// TStop > 12us.
	os_delay_us(12);
	// ets_intr_unlock();
}
Пример #24
0
//user_init is the user entry point of the Espressif SDK
void ICACHE_FLASH_ATTR user_init()
{
	//Initialize the uart0 and uart1 in 115200 bitrate
	uart_init(115200, 115200);

	wifi_enter_sta();

	os_delay_us(100000);
	os_delay_us(100000);

	system_init_done_cb(sntp_test);
}
Пример #25
0
// Read sensor data and calculate values
int ICACHE_FLASH_ATTR msReadSensor( struct msdata* d )
 {
  int32_t dt;
  int64_t off, sens;
  #ifdef MS5637_ENABLE_COMPENSATION
  int64_t t2, off2, sens2;
  #endif
  // Read D1: uncompensated digital pressure value
  if(i2cWriteCmd(MS5637_ADDR, MS5637_CONVERT_D1|MS5637_OSR_PRES, I2C_SEND_STOP)!=0) return -1;
  os_delay_us(540);
  os_delay_us(540<<MS5637_OS_PRES);
  d->d1 = (uint32_t) i2cReadRegister24(MS5637_ADDR, MS5637_ADC_READ, I2C_SEND_STOP);
  // Read D2: digital temperature value
  if(i2cWriteCmd(MS5637_ADDR, MS5637_CONVERT_D2|MS5637_OSR_TEMP, I2C_SEND_STOP)!=0) return -1;
  os_delay_us(540);
  os_delay_us(540<<MS5637_OS_TEMP);
  d->d2 = (uint32_t) i2cReadRegister24(MS5637_ADDR, MS5637_ADC_READ, I2C_SEND_STOP);
  //os_printf("MS5637 read: D1=%u, D2=%u\n", d->d1, d->d2);
  // Calculate temperature
  dt = d->d2 - (int32_t) d->c[5] * (1L<<8);
  d->t = 2000 + (int64_t) dt * (int64_t) d->c[6] / (1LL<<23);
  #ifdef MS5637_ENABLE_COMPENSATION
  if(d->t<2000)
   {
    t2 = 3 * ((int64_t) dt * (int64_t) dt) / (1LL<<33); // Low temperature
   } else {
    t2 = 5 * ((int64_t) dt * (int64_t) dt) / (1LL<<38); // High temperature
   }
  d->t -= t2;
  #endif
  // Calculate pressure
  off = (int64_t) d->c[2] * (1LL<<17) + (int64_t) dt * (int64_t) d->c[4] / (1LL<<6);
  sens = (int64_t) d->c[1] * (1LL<<16) + (int64_t) dt * (int64_t) d->c[3] / (1LL<<7);
  #ifdef MS5637_ENABLE_COMPENSATION
  if(d->t<2000)
   {
    // Low temperature
    off2 = 61 * (int64_t) (d->t-2000) * (int64_t) (d->t-2000) / (1LL<<4);
    sens2 = 29 * (int64_t) (d->t-2000) * (int64_t) (d->t-2000) / (1LL<<4);
    if(d->t<-1500)
     {
      // Very low temperature
      off2 += 17 * (int64_t) (d->t+1500) * (int64_t) (d->t+1500);
      sens2 += 9 * (int64_t) (d->t+1500) * (int64_t) (d->t+1500);
     }
    off -= off2;
    sens -= sens2;
   }
  #endif
  d->p = ((uint64_t) d->d1 * sens / (1LL<<21) - off) / (1LL << 15);
  os_printf("MS5637: t=%d, p=%d\n", d->t, d->p);
  return 0;
 }
Пример #26
0
//
// Write a bit. Port and bit is used to cut lookup time and provide
// more certain timing.
//
void ICACHE_FLASH_ATTR onewire_write_bit(uint32 v) {
	GPIO_OUTPUT_SET(ONEWIRE_PIN, 0);
	if (v) {
		os_delay_us(10);
		GPIO_DIS_OUTPUT(ONEWIRE_PIN); // Drive output high using the pull-up
		os_delay_us(55);
	} else {
		os_delay_us(65);
		GPIO_DIS_OUTPUT(ONEWIRE_PIN); // Drive output high using the pull-up
		os_delay_us(5);
	}
}
Пример #27
0
void supla_ds18b20_write_bit( int v )
{
    GPIO_OUTPUT_SET( supla_w1_pin, 0 );
    if( v ) {
        os_delay_us(10);
        GPIO_OUTPUT_SET( supla_w1_pin, 1 );
        os_delay_us(55);
    } else {
        os_delay_us(65);
        GPIO_OUTPUT_SET( supla_w1_pin, 1 );
        os_delay_us(5);
    }
}
Пример #28
0
void supla_ds18b20_reset(void)
{
    uint8_t retries = 125;
    GPIO_DIS_OUTPUT( supla_w1_pin );
    do {
        if (--retries == 0) return;
        os_delay_us(2);
    } while ( !GPIO_INPUT_GET( supla_w1_pin ));

    GPIO_OUTPUT_SET( supla_w1_pin, 0 );
    os_delay_us(480);
    GPIO_DIS_OUTPUT( supla_w1_pin );
    os_delay_us(480);
}
Пример #29
0
//Call this with PWM disabled.
static void ICACHE_FLASH_ATTR enableNightlight(int ena) {
	int x;
	//Kill PWM
	RTC_REG_WRITE(FRC1_CTRL_ADDRESS, 0);
	TM1_EDGE_INT_DISABLE();
	if (ena) {
		gpio_output_set((1<<NIGHTLIGHT_ON_PIN), (1<<NIGHTLIGHT_OFF_PIN), (1<<NIGHTLIGHT_ON_PIN)|(1<<NIGHTLIGHT_OFF_PIN), 0);
		os_delay_us(100);
	} else {
		gpio_output_set((1<<NIGHTLIGHT_OFF_PIN), (1<<NIGHTLIGHT_ON_PIN), (1<<NIGHTLIGHT_ON_PIN)|(1<<NIGHTLIGHT_OFF_PIN), 0);
		os_delay_us(10000);
	}
	gpio_output_set(0, (1<<NIGHTLIGHT_OFF_PIN)|(1<<NIGHTLIGHT_ON_PIN), (1<<NIGHTLIGHT_ON_PIN)|(1<<NIGHTLIGHT_OFF_PIN), 0);
}
Пример #30
0
void setupwebpage_init(void)
{
int reset_count = 0;

while (!GPIO_INPUT_GET(LED_GPIO_4)) // Check to see if web page setup button is held low on powerup
	{
	INFO("*");
	++reset_count;
	os_delay_us(500000); // Half second tick
	if (reset_count > 6)
		{
		for (int i = 0; i < 5; i++) { os_delay_us(100000); digitalWrite(LED_GPIO_13, 1); os_delay_us(100000); digitalWrite(LED_GPIO_13, 0);}
		break;
		}
	}


if (reset_count < 6) // OK button not held down at power up, setup MQTT etc
	{

	wifiInit(STATION_MODE); // Only connect to the local network

	if (sysCfg.enable_webpage_control)
		{
		INFO("Setting up for normal MQTT Operation\r");
		builtInUrls[0].cgiArg = "/led.tpl";
		httpdInit(builtInUrls, 80);
		}

	INFO("ESP8266 in STA mode configured.\r\n");

	// Setup a timer to initialise the mqtt system 5 seconds after a wifi connection is established
	os_timer_disarm(&MQTTlogontimer);
	os_timer_setfn(&MQTTlogontimer, MQTTLogonTimerCb, NULL);
	os_timer_arm(&MQTTlogontimer, 5000, 1);
	allow_mqtt_init = 1; // Allow the MQTT system to connect
	}
else
	{
	//struct softap_config apConfig;
	INFO("Setting up for Web Page Configuration\r");

	httpdInit(builtInUrls, 80);
	wifiInit(STATIONAP_MODE); // Connect to the local network

	//wifi_softap_get_config(&apConfig);
	}

}