TA的每日心情 | 开心 2018-11-19 09:39 |
---|
签到天数: 2 天 连续签到: 1 天 [LV.1]初来乍到
|
楼主 |
发表于 2016-6-23 21:49:29
|
显示全部楼层
本帖最后由 火oooo 于 2016-6-23 21:53 编辑
oled.c- //1. 包含头文件
- #include "example.h"
- #include "trochili.h"
- #include "phoenix_bsp_oled_spi.h"
- //2. 预编译开关
- #if (EVB_EXAMPLE == SPIOLED)
- //3. 三个参数
- //设定栈长宽字节、优先级字节、时间片字节的长度
- //Q:怎么设定这个参数的数值?不同的数据会有什么影响?
- /* 用户线程参数 */
- #define THREAD_OLED_STACK_BYTES (512)
- #define THREAD_OLED_PRIORITY (5)
- #define THREAD_OLED_SLICE (20)
- #define THREAD_REFRESH_STACK_BYTES (512)
- #define THREAD_REFRESH_PRIORITY (4)
- #define THREAD_REFRESH_SLICE (20)
- //4. 定义线程
- /* 用户线程定义 */
- static TThread ThreadOLED;
- //static TThread ThreadOLEDClear;
- static TThread ThreadREFRESH;
- /* 用户信号量定义 */
- static TSemaphore RefreshSemaphore;
- //5. 定义线程栈
- /* 用户线程栈 */
- static TWord ThreadOLEDStack[THREAD_OLED_STACK_BYTES/4];
- static TWord ThreadREFRESHStack[THREAD_REFRESH_STACK_BYTES/4];
- //static TWord ThreadOLEDClearStack[THREAD_OLED_STACK_BYTES/4];
- //6. 定义线程入口函数 线程的主要功能在这里实现。static void ThreadLedEntry(TArgument data)函数就在这个位置
- /* 线程OLED的线程函数 */
- static void ThreadOLEDEntry(TArgument data)
- {
- TState state;
- TError error;
- u8 t;
-
- while (eTrue)
- {
- /* OLED线程以阻塞方式获取信号量,然后显示信息*/
- state = TclObtainSemaphore(&RefreshSemaphore, TCLO_IPC_WAIT, 0, &error);
- if (state == eSuccess)
- // while (eTrue)
- {
- OLED_ShowString(0,0, " Phoenix's "); //???Phoenix's????
- OLED_ShowString(0,16,"UID: "); //Pres:??.?kPa/???
- OLED_ShowString(0,32," "); //Temp:???.?C/????
- OLED_ShowString(0,48," "); //Temp:????h/?????
- OLED_Refresh_Gram();
- EvbUart2WriteStr("wirte finished!");
- if((t++)%2)
- {
- EvbLedControl(LED1, LED_ON);
- }
- else
- {
- EvbLedControl(LED1, LED_OFF);
- }
- }
-
- /* OLED线程以阻塞方式获取信号量,然后显示信息*/
- state = TclObtainSemaphore(&RefreshSemaphore, TCLO_IPC_WAIT, 0, &error);
- if (state == eSuccess)
- // while (eTrue)
- {
- OLED_Clear(); //
- OLED_Refresh_Gram();
-
- EvbUart2WriteStr("clear finished!\n");
- EvbLedControl(LED1, LED_OFF);
- }
- }
- }
- ///* 线程OLEDClear的线程函数 */
- //static void ThreadOLEDClearEntry(TArgument data)
- //{
- // TState state;
- // TError error;
- //
- // while (eTrue)
- // {
- // /* OLEDClear线程以阻塞方式获取信号量,然后清除信息*/
- //// state = TclObtainSemaphore(&RefreshSemaphore, TCLO_IPC_WAIT, 0, &error);
- //// if (state == eSuccess)
- // while (eTrue)
- // {
- // OLED_Clear(); //
- //// OLED_Refresh_Gram();
- //
- // EvbUart2WriteStr("clear finished!\n");
- // EvbLedControl(LED1, LED_OFF);
- // }
- // }
- //}
- /* 线程REFRESH的线程函数 */
- static void ThreadREFRESHEntry(TArgument data)
- {
- TError error;
- TState state;
- while (eTrue)
- {
- /**/
- // OLED_Refresh_Gram();
- /* REFRESH线程延时1000ms */
- state = TclDelayThread(0, TCLM_MLS2TICKS(1000), &error);
- TCLM_ASSERT((state == eSuccess), "");
- TCLM_ASSERT((error == TCLE_THREAD_NONE), "");
- /* REFRESH线程释放信号量 */
- state = TclReleaseSemaphore(&RefreshSemaphore, 0, 0, &error);
- TCLM_ASSERT((state == eSuccess), "");
- TCLM_ASSERT((error == TCLE_IPC_NONE), "");
-
- // /* 激活Led设备点亮线程 */
- // state = TclActivateThread(&ThreadOLED, &error);
- // TCLM_ASSERT((state == eSuccess), "");
- // TCLM_ASSERT((error == TCLE_THREAD_NONE), "");
- //
- //// /* REFRESH线程释放信号量 */
- //// state = TclReleaseSemaphore(&RefreshSemaphore, 0, 0, &error);
- //// TCLM_ASSERT((state == eSuccess), "");
- //// TCLM_ASSERT((error == TCLE_IPC_NONE), "");
- // OLED_Refresh_Gram();
- // /* 控制线程延时1秒 */
- // state = TclDelayThread(&ThreadREFRESH, TCLM_SEC2TICKS(1), &error);
- // TCLM_ASSERT((state == eSuccess), "");
- // TCLM_ASSERT((error == TCLE_THREAD_NONE), "");
- // /* 休眠Led设备点亮线程 */
- // state = TclDeactivateThread(&ThreadOLED, &error);
- // TCLM_ASSERT((state == eSuccess), "");
- // TCLM_ASSERT((error == TCLE_THREAD_NONE), "");
- // /* 激活Led设备熄灭线程 */
- // state = TclActivateThread(&ThreadOLEDClear, &error);
- // TCLM_ASSERT((state == eSuccess), "");
- // TCLM_ASSERT((error == TCLE_THREAD_NONE), "");
- //
- //// /* REFRESH线程释放信号量 */
- //// state = TclReleaseSemaphore(&RefreshSemaphore, 0, 0, &error);
- //// TCLM_ASSERT((state == eSuccess), "");
- //// TCLM_ASSERT((error == TCLE_IPC_NONE), "");
- // OLED_Refresh_Gram();
- // /* 控制线程延时1秒 */
- // state = TclDelayThread(&ThreadREFRESH, TCLM_SEC2TICKS(1), &error);
- // TCLM_ASSERT((state == eSuccess), "");
- // TCLM_ASSERT((error == TCLE_THREAD_NONE), "");
- // /* 休眠Led设备熄灭线程 */
- // state = TclDeactivateThread(&ThreadOLEDClear, &error);
- // TCLM_ASSERT((state == eSuccess), "");
- // TCLM_ASSERT((error == TCLE_THREAD_NONE), "");
- }
- }
- //7. 定义应用入口函数 系统调用此应用,从而对线程进行处理
- /* 用户应用入口函数 */
- static void AppSetupEntry(void)
- {
- TError error;
- TState state;
-
- /* 初始化信号量 */
- state = TclInitSemaphore(&RefreshSemaphore, 0, 1, TCLP_IPC_DUMMY, &error);
- TCLM_ASSERT((state == eSuccess), "");
- TCLM_ASSERT((error == TCLE_IPC_NONE), "");
-
- /* 初始化OLED设备线程 */
- state = TclInitThread(&ThreadOLED,
- &ThreadOLEDEntry, (TArgument)0,
- ThreadOLEDStack, THREAD_OLED_STACK_BYTES,
- THREAD_OLED_PRIORITY, THREAD_OLED_SLICE,
- &error);
- TCLM_ASSERT((state == eSuccess), "");
- TCLM_ASSERT((error == TCLE_THREAD_NONE), "");
- // /* 初始化OLEDClear设备线程 */
- // state = TclInitThread(&ThreadOLEDClear,
- // &ThreadOLEDClearEntry, (TArgument)0,
- // ThreadOLEDClearStack, THREAD_OLED_STACK_BYTES,
- // THREAD_OLED_PRIORITY, THREAD_OLED_SLICE,
- // &error);
- // TCLM_ASSERT((state == eSuccess), "");
- // TCLM_ASSERT((error == TCLE_THREAD_NONE), "");
- /* 初始化REFRESH线程 */
- state = TclInitThread(&ThreadREFRESH,
- &ThreadREFRESHEntry, (TArgument)0,
- ThreadREFRESHStack, THREAD_REFRESH_STACK_BYTES,
- THREAD_REFRESH_PRIORITY, THREAD_REFRESH_SLICE,
- &error);
- TCLM_ASSERT((state == eSuccess), "");
- TCLM_ASSERT((error == TCLE_THREAD_NONE), "");
- /* 激活REFRESH线程 */
- state = TclActivateThread(&ThreadREFRESH, &error);
- TCLM_ASSERT((state == eSuccess), "");
- TCLM_ASSERT((error == TCLE_THREAD_NONE), "");
-
- /* 激活OLED线程 */
- state = TclActivateThread(&ThreadOLED, &error);
- TCLM_ASSERT((state == eSuccess), "");
- TCLM_ASSERT((error == TCLE_THREAD_NONE), "");
-
- // /* 激活OLEDClear线程 */
- // state = TclActivateThread(&ThreadOLEDClear, &error);
- // TCLM_ASSERT((state == eSuccess), "");
- // TCLM_ASSERT((error == TCLE_THREAD_NONE), "");
- EvbUart2WriteStr("\nAPP INIT finished!\n");
- }
- //8. 定义main函数
- /* 处理器BOOT之后会调用main函数,必须提供 */
- int main(void)
- {
- /* 注册各个内核函数,启动内核 */
- TclStartKernel(&AppSetupEntry,
- &CpuSetupEntry,
- &EvbSetupEntry,
- &EvbTraceEntry);
- return 1;
- }
- #endif
复制代码 还记得我们说过的8个结构吗?
1. 包含头文件
2. 预编译开关
3. 三个参数 设定栈长宽字节、优先级字节、时间片字节的长度
4. 定义线程
5. 定义线程栈
6. 定义线程入口函数 线程的主要功能在这里实现
7. 定义应用入口函数 系统调用此应用,从而对线程进行处理
8. 定义main函数
我们逐个来看。
|
|