可以上传阿里云,概率随机动

This commit is contained in:
james 2024-08-05 11:24:14 +08:00
parent 65e7298351
commit 5db946f9db
3 changed files with 118 additions and 94 deletions

View File

@ -12,8 +12,11 @@
#define KEY_LEFT GET_PIN(C, 0)
#define KEY_RIGHT GET_PIN(C, 4)
extern rt_atomic_t now_direction ;
extern rt_atomic_t snake_pressed ;
extern int snake_max;
void key_up_callback(void *args)
{
rt_atomic_store(&snake_pressed, snake_max+1);
if(rt_atomic_load(&now_direction) != 2) rt_atomic_store(&now_direction, 0);
int value = rt_pin_read(KEY_UP);
LOG_I("key up value: %d\n", value);
@ -21,6 +24,7 @@ void key_up_callback(void *args)
void key_down_callback(void *args)
{
rt_atomic_store(&snake_pressed, snake_max+1);
int value = rt_pin_read(KEY_DOWN);
if(rt_atomic_load(&now_direction) != 0) rt_atomic_store(&now_direction, 2);
LOG_I("key down value: %d\n", value);
@ -28,6 +32,7 @@ void key_down_callback(void *args)
void key_left_callback(void *args)
{
rt_atomic_store(&snake_pressed, snake_max+1);
if(rt_atomic_load(&now_direction) != 3) rt_atomic_store(&now_direction, 1);
int value = rt_pin_read(KEY_LEFT);
LOG_I("key left value: %d\n", value);
@ -35,6 +40,7 @@ void key_left_callback(void *args)
void key_right_callback(void *args)
{
rt_atomic_store(&snake_pressed, snake_max+1);
if(rt_atomic_load(&now_direction) != 1) rt_atomic_store(&now_direction, 3);
int value = rt_pin_read(KEY_RIGHT);
LOG_I("key right value: %d\n", value);

View File

@ -11,7 +11,6 @@
#include <drv_lcd.h>
#include "mysnake.h"
char DEMO_PRODUCT_KEY[IOTX_PRODUCT_KEY_LEN + 1] = {0};
char DEMO_DEVICE_NAME[IOTX_DEVICE_NAME_LEN + 1] = {0};
char DEMO_DEVICE_SECRET[IOTX_DEVICE_SECRET_LEN + 1] = {0};
@ -28,6 +27,7 @@ int HAL_Snprintf(char *str, const int len, const char *fmt, ...);
// 定义接受文件内容的缓冲区
char buffer[1026] = {};
char tmp[1026];
extern int snake_len;
#define GPIO_LED_B GET_PIN(F, 11)
#define GPIO_LED_R GET_PIN(F, 12)
@ -35,7 +35,6 @@ char tmp[1026];
// AHT挂载的总线名字
#define AHT10_I2C_BUS "i2c3"
// AHT,ap3216c设备指针
aht10_device_t Dev = RT_NULL;
ap3216c_device_t dev;
@ -52,7 +51,8 @@ void mqt_init(void);
int ap3_init(void);
#define EXAMPLE_TRACE(fmt, ...) \
do { \
do \
{ \
HAL_Printf("%s|%03d :: ", __func__, __LINE__); \
HAL_Printf(fmt, ##__VA_ARGS__); \
HAL_Printf("%s", "\r\n"); \
@ -62,7 +62,8 @@ static void example_message_arrive(void *pcontext, void *pclient, iotx_mqtt_even
{
iotx_mqtt_topic_info_t *topic_info = (iotx_mqtt_topic_info_pt)msg->msg;
switch (msg->event_type) {
switch (msg->event_type)
{
case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
/* print topic name and topic message */
EXAMPLE_TRACE("Message Arrived:");
@ -96,7 +97,8 @@ static int example_subscribe(void *handle)
topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1;
topic = HAL_Malloc(topic_len);
if (topic == NULL) {
if (topic == NULL)
{
EXAMPLE_TRACE("memory not enough");
return -1;
}
@ -104,7 +106,8 @@ static int example_subscribe(void *handle)
HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME);
res = IOT_MQTT_Subscribe(handle, topic, IOTX_MQTT_QOS0, example_message_arrive, NULL);
if (res < 0) {
if (res < 0)
{
EXAMPLE_TRACE("subscribe failed");
HAL_Free(topic);
return -1;
@ -180,8 +183,8 @@ void tmp_payload(void)
// sprintf(tmp, "Temp: %.1f;Humi: %.1f;Count: %d\n", Temp, Humi,++cnt);
// rt_kprintf("\n%f %f tmp:%s\n",Humi,Temp,tmp);
// make_file();
show_lcd();
sprintf(tmp, "{\"params\":{\"temperature\":%.2f,\"humidity\":%.2f,,\"LightLux\":%.2f,\"Psdata\":%d}}", Temp, Humi,brightness,ps_data);
// show_lcd();
sprintf(tmp, "{\"params\":{\"temperature\":%.2f,\"humidity\":%.2f,\"LightLux\":%.2f,\"Psdata\":%d,\"Snakelen\":%d}}", Temp, Humi, brightness, ps_data, snake_len);
return;
}
void test_lcd()
@ -199,7 +202,6 @@ MSH_CMD_EXPORT(test_lcd, run my project);
static int example_publish(void *handle)
{
tmp_payload();
int res = 0;
const char *fmt = "/sys/%s/%s/thing/event/property/post";
@ -211,7 +213,8 @@ static int example_publish(void *handle)
// rt_kprintf("payload:%s\n",payload);
topic_len = strlen(fmt) + strlen(DEMO_PRODUCT_KEY) + strlen(DEMO_DEVICE_NAME) + 1;
topic = HAL_Malloc(topic_len);
if (topic == NULL) {
if (topic == NULL)
{
EXAMPLE_TRACE("memory not enough");
return -1;
}
@ -219,7 +222,8 @@ static int example_publish(void *handle)
HAL_Snprintf(topic, topic_len, fmt, DEMO_PRODUCT_KEY, DEMO_DEVICE_NAME);
res = IOT_MQTT_Publish_Simple(0, topic, IOTX_MQTT_QOS0, payload, strlen(payload));
if (res < 0) {
if (res < 0)
{
EXAMPLE_TRACE("publish failed, res = %d", res);
HAL_Free(topic);
return -1;
@ -247,25 +251,28 @@ static void mqtt_example_main(void *parameter)
EXAMPLE_TRACE("mqtt example");
memset(&mqtt_params, 0x0, sizeof(mqtt_params));
mqtt_params.handle_event.h_fp = example_event_handle;
pclient = IOT_MQTT_Construct(&mqtt_params);
if (NULL == pclient) {
if (NULL == pclient)
{
EXAMPLE_TRACE("MQTT construct failed");
return;
}
res = example_subscribe(pclient);
if (res < 0) {
if (res < 0)
{
IOT_MQTT_Destroy(&pclient);
return;
}
while (1) {
if (0 == loop_cnt % 20) {
while (1)
{
if (0 == loop_cnt % 20)
{
example_publish(pclient);
}
@ -277,7 +284,6 @@ static void mqtt_example_main(void *parameter)
return;
}
#define THREAD_PRIORITY 25
#define THREAD_STACK_SIZE 4096
#define THREAD_TIMESLICE 5
@ -308,7 +314,8 @@ void mqt_init(void)
rt_kprintf("MQTT Thread Create Failed!\n");
}
}
int ap3_init(void){
int ap3_init(void)
{
const char *i2c_bus_name = "i2c2";
dev = ap3216c_init(i2c_bus_name);

View File

@ -10,6 +10,9 @@
#define SNAKE_SIZE 20
#define SNAKE_MAX LCD_MAX / SNAKE_SIZE
rt_atomic_t now_direction = 3;
rt_atomic_t snake_pressed = 0;
int snake_max = SNAKE_MAX * 3;
int snake_len = 3;
// bool snake_table[SNAKE_MAX][SNAKE_MAX] = {0};
// struct My_snake
@ -32,8 +35,7 @@ void snake_entry(void *parameter)
bool food_flag = false;
char snake_dirshow[4][7] = {"upup", "left", "down", "rigt"};
char tmp[10];
int snake_head = 2, snake_tail = 0, former_head; // 蛇头,蛇尾
int snake_len = 3;
int snake_head = 2, snake_tail = 0; // 蛇头,蛇尾
snake_list[1][0] = SNAKE_MAX / 2;
snake_list[1][1] = SNAKE_MAX / 2;
snake_list[0][0] = snake_list[1][0] - 1;
@ -50,13 +52,22 @@ void snake_entry(void *parameter)
snake_food[1] = rand() % SNAKE_MAX;
snake_address(snake_food[0], snake_food[1], SNAKE_SIZE, GREEN);
int new_head_x = 0, new_head_y = 0;
int new_direction = 0;
while (1)
{
// if(snake)
// new_direction = rand() % 3;
// now_direction = (now_direction+3+new_direction)%4;//防止反向,走回头路
former_head = snake_head;
if (!snake_pressed)
{
//80%的概率保持当前方向20%的概率随机改变方向
if (rand() % 100 < 20)
{
new_direction = rand() % 3;
now_direction = (now_direction + 3 + new_direction) % 4; // 防止反向,走回头路
}
}
else
{
rt_atomic_add(&snake_pressed, -1);
}
new_head_x = (snake_list[snake_head][0] + snake_direction[now_direction][0] + SNAKE_MAX) % (SNAKE_MAX);
new_head_y = (snake_list[snake_head][1] + snake_direction[now_direction][1] + SNAKE_MAX) % (SNAKE_MAX);