From b5093f2a508521ff828a0b2db7098a8af69d8a88 Mon Sep 17 00:00:00 2001 From: james <1943357252@qq.com> Date: Thu, 25 Jul 2024 22:21:07 +0800 Subject: [PATCH] led to day3 --- Day3/SConscript | 18 ++++++ Day3/led.c | 114 ++++++++++++++++++++++++++++++++++++++ applications/main.c | 131 +------------------------------------------- 3 files changed, 134 insertions(+), 129 deletions(-) create mode 100644 Day3/SConscript create mode 100644 Day3/led.c diff --git a/Day3/SConscript b/Day3/SConscript new file mode 100644 index 0000000..03feb20 --- /dev/null +++ b/Day3/SConscript @@ -0,0 +1,18 @@ +from building import * +import os + +cwd = GetCurrentDir() +src = Glob('*.c') +CPPPATH = [cwd] + +if GetDepend(['PKG_USING_RTDUINO']) and not GetDepend(['RTDUINO_NO_SETUP_LOOP']): + src += ['arduino_main.cpp'] + +group = DefineGroup('Applications', src, depend = [''], CPPPATH = CPPPATH) + +list = os.listdir(cwd) +for item in list: + if os.path.isfile(os.path.join(cwd, item, 'SConscript')): + group = group + SConscript(os.path.join(item, 'SConscript')) + +Return('group') diff --git a/Day3/led.c b/Day3/led.c new file mode 100644 index 0000000..fa8e4cd --- /dev/null +++ b/Day3/led.c @@ -0,0 +1,114 @@ +#include +#include +#include +#ifndef RT_USING_NANO +#include +#endif /* RT_USING_NANO */ + +#define GPIO_LED_B GET_PIN(F, 11) +#define GPIO_LED_R GET_PIN(F, 12) + +#define THREAD_PRIORITY 25 +#define THREAD_STACK_SIZE 1024 +#define THREAD_TIMESLICE 5 + +#define PIN_KEY0 GET_PIN(C,0) + +#define KEY_PRESS_DOWN(key) (rt_pin_read((key))) == PIN_LOW) +#define KEY_PRESSED PIN_LOW + +static rt_thread_t tid1 = RT_NULL; +static rt_thread_t tid2 = RT_NULL; + +static void key_name_entry(void *parameter); +static void led_name_entry(void *parameter); +static rt_sem_t dynamic_sem = RT_NULL; + +int led(void) +{ + rt_pin_mode(GPIO_LED_R, PIN_MODE_OUTPUT); + rt_pin_mode(PIN_KEY0, PIN_MODE_INPUT_PULLUP); + + dynamic_sem = rt_sem_create("dsem", 0, RT_IPC_FLAG_PRIO); + if (dynamic_sem == RT_NULL) + { + rt_kprintf("create dynamic semaphore failed.\n"); + return -1; + } + else + { + rt_kprintf("create done. dynamic semaphore value = 0.\n"); + } + + tid1 = rt_thread_create("key_thread", + key_name_entry, RT_NULL, + THREAD_STACK_SIZE, + THREAD_PRIORITY, THREAD_TIMESLICE); + if (tid1 != RT_NULL) + { + rt_thread_startup(tid1); + } + + tid2 = rt_thread_create("led_thread", + led_name_entry, RT_NULL, + THREAD_STACK_SIZE, + THREAD_PRIORITY, THREAD_TIMESLICE); + if (tid2 != RT_NULL) + { + rt_thread_startup(tid2); + } + + // while (1) + // { + // rt_pin_write(GPIO_LED_R, PIN_HIGH); + // rt_thread_mdelay(500); + // rt_pin_write(GPIO_LED_R, PIN_LOW); + // rt_thread_mdelay(500); + // } +} +MSH_CMD_EXPORT(led, led control); + +static void key_name_entry(void *parameter) +{ + rt_uint32_t count=0; + while (1) + { + if(rt_pin_read(PIN_KEY0) == KEY_PRESSED) + { + rt_thread_mdelay(10); + if(rt_pin_read(PIN_KEY0) ==KEY_PRESSED) + { + rt_kprintf("key0 is pressed (%d) \n", count++); + // rt_sem_release(dynamic_sem); + } + else + { + rt_sem_release(dynamic_sem); + } + } + else + { + rt_sem_release(dynamic_sem); + } + rt_thread_mdelay(10); + } + +} + +static void led_name_entry(void *parameter) +{ + rt_uint32_t count=0; + rt_uint32_t result=0; + while (1) + { + rt_pin_write(GPIO_LED_R, PIN_HIGH); + result = rt_sem_take(dynamic_sem, RT_WAITING_FOREVER); + if (result == RT_EOK) + { + // rt_kprintf("LED LOW\n"); + rt_pin_write(GPIO_LED_R, PIN_LOW); + } + + rt_thread_mdelay(10); + } +} diff --git a/applications/main.c b/applications/main.c index f4b27b6..298fde4 100644 --- a/applications/main.c +++ b/applications/main.c @@ -1,134 +1,7 @@ -/* - * Copyright (c) 2023, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-07-06 Supperthomas first version - * 2023-12-03 Meco Man support nano version - */ - -#include #include -#include -#ifndef RT_USING_NANO -#include -#endif /* RT_USING_NANO */ - -#define GPIO_LED_B GET_PIN(F, 11) -#define GPIO_LED_R GET_PIN(F, 12) - -#define THREAD_PRIORITY 25 -#define THREAD_STACK_SIZE 1024 -#define THREAD_TIMESLICE 5 - -#define PIN_KEY0 GET_PIN(C,0) - -static rt_thread_t tid1 = RT_NULL; -static rt_thread_t tid2 = RT_NULL; - -static void key_name_entry(void *parameter); -static void led_name_entry(void *parameter); -static rt_sem_t dynamic_sem = RT_NULL; - -int main(void) +int main(void) { - rt_pin_mode(GPIO_LED_R, PIN_MODE_OUTPUT); - rt_pin_mode(PIN_KEY0, PIN_MODE_INPUT_PULLUP); - - dynamic_sem = rt_sem_create("dsem", 0, RT_IPC_FLAG_PRIO); - if (dynamic_sem == RT_NULL) - { - rt_kprintf("create dynamic semaphore failed.\n"); - return -1; - } - else - { - rt_kprintf("create done. dynamic semaphore value = 0.\n"); - } - - tid1 = rt_thread_create("key_thread", - key_name_entry, RT_NULL, - THREAD_STACK_SIZE, - THREAD_PRIORITY, THREAD_TIMESLICE); - if (tid1 != RT_NULL) - { - rt_thread_startup(tid1); - } - - tid2 = rt_thread_create("led_thread", - led_name_entry, RT_NULL, - THREAD_STACK_SIZE, - THREAD_PRIORITY, THREAD_TIMESLICE); - if (tid2 != RT_NULL) - { - rt_thread_startup(tid2); - } - - // while (1) - // { - // rt_pin_write(GPIO_LED_R, PIN_HIGH); - // rt_thread_mdelay(500); - // rt_pin_write(GPIO_LED_R, PIN_LOW); - // rt_thread_mdelay(500); - // } -} -static void key_name_entry(void *parameter) -{ - rt_uint32_t count=0; - while (1) - { - if(rt_pin_read(PIN_KEY0) == PIN_LOW) - { - rt_thread_mdelay(100); - if(rt_pin_read(PIN_KEY0) == PIN_LOW) - { - rt_kprintf("key0 is pressed (%d) ", count++); - // rt_sem_release(dynamic_sem); - } - else - { - rt_sem_release(dynamic_sem); - } - } - else - { - rt_sem_release(dynamic_sem); - } - rt_thread_mdelay(10); - } + return 0; } -static void led_name_entry(void *parameter) -{ - rt_uint32_t count=0; - rt_uint32_t result=0; - while (1) - { - rt_pin_write(GPIO_LED_R, PIN_HIGH); - result = rt_sem_take(dynamic_sem, RT_WAITING_FOREVER); - if (result == RT_EOK) - { - // rt_kprintf("LED LOW\n"); - rt_pin_write(GPIO_LED_R, PIN_LOW); - } - - rt_thread_mdelay(10); - } -} -// #include -// #include "hello.h" - -// int main(void) -// { -// while(1) -// { -// Print_Hello_World(); -// rt_thread_mdelay(500); -// } - -// return 0; -// } -