Page 4 of 16
Page 92
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
void delay_ms(int t)
{
vTaskDelay(t / portTICK_PERIOD_MS);
}
void app_main(void)
{
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_OUTPUT);
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_INPUT);
gpio_pullup_en(4);
while (1)
{
if (gpio_get_level(4))
{
gpio_set_level(2, 1);
}
else
{
gpio_set_level(2, 0);
delay_ms(500);
}
}
}
Page 94
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
void delay_ms(int t)
{
vTaskDelay(t / portTICK_PERIOD_MS);
}
void app_main(void)
{
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_OUTPUT);
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_INPUT);
gpio_pulldown_en(4);
while (1)
{
while (gpio_get_level(4) == 0)
{
};
delay_ms(10);
while (gpio_get_level(4) == 1)
{
};
gpio_set_level(2, 1);
delay_ms(1000);
gpio_set_level(2, 0);
}
}
Page 95
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include <sys/time.h>
void delay_ms(int t)
{
vTaskDelay(t / portTICK_PERIOD_MS);
}
int64_t tick_ms(int64_t offset)
{
static struct timeval tv_now;
gettimeofday(&tv_now, NULL);
return (int64_t)tv_now.tv_sec * 1000L +
(int64_t)tv_now.tv_usec / 1000L + offset;
}
void app_main(void)
{
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_OUTPUT);
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_INPUT);
gpio_pulldown_en(4);
gpio_set_level(2, 0);
uint64_t t;
while (1)
{
while (gpio_get_level(4) == 0)
{
};
t = tick_ms(2000);
delay_ms(10);
while (gpio_get_level(4) == 1)
{
}
if (tick_ms(0) < t)
{
gpio_set_level(2, 1);
delay_ms(1000);
gpio_set_level(2, 0);
}
else
{
for (int i = 0; i < 10; i++)
{
gpio_set_level(2, 1);
delay_ms(1000);
gpio_set_level(2, 0);
delay_ms(1000);
}
}
}
}
Page 97
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include <sys/time.h>
int64_t tick_us(int64_t offset) {
static struct timeval tv_now;
gettimeofday(&tv_now, NULL);
return (int64_t)tv_now.tv_sec * 1000000L +
(int64_t)tv_now.tv_usec + offset;
}
void delay_ms(int t) {
vTaskDelay(t / portTICK_PERIOD_MS);
}
void app_main(void)
{
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_INPUT);
uint64_t t;
while (1) {
while (gpio_get_level(4) == 1) {};
while (gpio_get_level(4) == 0) {};
t = tick_us(0);
while (gpio_get_level(4) == 1) {};
t = tick_us(0) - t;
printf("%llu\n", t);
fflush(stdout);
delay_ms(1000);
}
}
Page 98
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include <sys/time.h>
int64_t tick_us(int64_t offset)
{
static struct timeval tv_now;
gettimeofday(&tv_now, NULL);
return (int64_t)tv_now.tv_sec * 1000000L +
(int64_t)tv_now.tv_usec + offset;
}
void delay_ms(int t)
{
vTaskDelay(t / portTICK_PERIOD_MS);
}
void app_main(void)
{
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_INPUT);
uint64_t t;
while (1)
{
while (gpio_get_level(4) == 1)
{
};
while (gpio_get_level(4) == 0)
{
};
t = 0;
while (gpio_get_level(4) == 1)
{
t++;
};
printf("%llu\n", t);
fflush(stdout);
delay_ms(1000);
}
Page 101
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include <sys/time.h>
int64_t tick_us(int64_t offset)
{
static struct timeval tv_now;
gettimeofday(&tv_now, NULL);
return (int64_t)tv_now.tv_sec * 1000000L +
(int64_t)tv_now.tv_usec + offset;
}
void delay_until(int64_t t)
{
static struct timeval tv_now;
do
{
gettimeofday(&tv_now, NULL);
} while (t > (int64_t)tv_now.tv_sec * 1000000L +
(int64_t)tv_now.tv_usec);
}
void app_main(void)
{
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_INPUT);
gpio_pulldown_en(4);
uint64_t t;
int s = 0;
int i;
int count = 0;
while (true)
{
i = gpio_get_level(4);
t = tick_us(100 * 1000);
switch (s)
{
case 0: // button not pushed
if (i)
{
s = 1;
count++;
printf("Button Push %d \n\r", count);
}
break;
case 1: // Button pushed
if (!i)
{
s = 0;
}
break;
default:
s = 0;
}
delay_until(t);
}
}
Page 103
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include <sys/time.h>
#include <unistd.h>
void delay_us(int t)
{
usleep(t);
}
int64_t tick_us(int64_t offset)
{
static struct timeval tv_now;
gettimeofday(&tv_now, NULL);
return (int64_t)tv_now.tv_sec * 1000000L + (int64_t)tv_now.tv_usec + offset;
}
void delay_until(int64_t t)
{
static struct timeval tv_now;
do
{
gettimeofday(&tv_now, NULL);
} while (t > (int64_t)tv_now.tv_sec * 1000000L + (int64_t)tv_now.tv_usec);
}
void app_main(void)
{
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_INPUT);
gpio_pulldown_en(4);
uint64_t t;
uint64_t tpush, twait;
int s = 0, i;
while (true)
{
i = gpio_get_level(4);
t = tick_us(0);
switch (s)
{
case 0: // button not pushed
if (i)
{
s = 1;
tpush = t;
}
break;
case 1: // Button pushed
if (!i)
{
s = 0;
if ((t - tpush) > 2000000)
{
printf("Button held \n\r");
}
else
{
printf("Button pushed \n\r");
}
fflush(stdout);
}
break;
default:
s = 0;
}
delay_until(t + 100 * 10000);
}
}
Page 105
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include <sys/time.h>
int64_t tick_us(int64_t offset)
{
static struct timeval tv_now;
gettimeofday(&tv_now, NULL);
return (int64_t)tv_now.tv_sec * 1000000L + (int64_t)tv_now.tv_usec + offset;
}
void delay_until(int64_t t)
{
static struct timeval tv_now;
do
{
gettimeofday(&tv_now, NULL);
} while (t > (int64_t)tv_now.tv_sec * 1000000L + (int64_t)tv_now.tv_usec);
}
void app_main(void)
{
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_INPUT);
gpio_pulldown_en(4);
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_OUTPUT);
gpio_reset_pin(16);
gpio_set_direction(16, GPIO_MODE_OUTPUT);
gpio_reset_pin(17);
gpio_set_direction(17, GPIO_MODE_OUTPUT);
uint64_t t;
int edge;
int buttonNow;
int s = 0;
int buttonState = gpio_get_level(4);
gpio_set_level(2, 0);
gpio_set_level(16, 1);
gpio_set_level(17, 0);
while (true)
{
t = tick_us(0);
buttonNow = gpio_get_level(4);
edge = buttonState - buttonNow;
buttonState = buttonNow;
switch (s)
{
case 0:
if (edge == 1)
{
s = 1;
gpio_set_level(2, 0);
gpio_set_level(16, 1);
gpio_set_level(17, 0);
}
break;
case 1:
if (edge == 1)
{
s = 2;
gpio_set_level(2, 0);
gpio_set_level(16, 0);
gpio_set_level(17, 1);
}
break;
case 2:
if (edge == 1)
{
s = 0;
gpio_set_level(2, 1);
gpio_set_level(16, 0);
gpio_set_level(17, 0);
}
break;
default:
s = 0;
}
delay_until(t + 100 * 10000);
}
}