C_and_CPP 板


LINE

不好意思, 我搞错了。coroutine.h 从下面网址下载。 https://www.chiark.greenend.org.uk/~sgtatham/coroutine.h 我用了 coroutine 做了类似的模拟, 因为实做 multitask 太费工夫。 这是纯 c 的方式, 完全不相依任何平台。 你先跑看看是不是你要的样子, 需要在 linux 终端机上测试。 简单来说, task2 第一次被呼叫时, 点灯, 第二次被呼叫时, 关灯。 补上画面: https://goo.gl/o21jWg #include <unistd.h> #include <cstdio> #include <cstdlib> #include <algorithm> #include <iostream> #include "coroutine.h" using namespace std; #define W 10 #define H 5 #define X(x) "\x1b[xA" #define Y(y) "\x1b[yC" void x(int x) { cout << "\x1b[" << x << "A"; } void y(int y) { cout << "\x1b["<< y << "C"; } void xy(int x, int y) { cout << "\x1b[" << x << ";" << y << "H"; } int task2(); int task1() { xy(1,1); cout << "*" << endl; task2(); usleep(100000); xy(1,1); cout << " " << endl; usleep(100000); } int task2() { scrBegin; xy(3,3); cout << "*" << endl; scrReturnV; xy(3,3); cout << " " << endl; scrLine=0; scrFinishV(-1); } int main(int argc, char *argv[]) { system("clear"); #if 1 while(1) { task1(); } #endif return 0; } ※ 引述《zzss2003 (brotherD)》之铭言: : 开发平台(Platform):C language in embedded system : 问题(Question):想让两颗LED用不同的频率闪烁 : 程式码(Code): : void task1(void) : { : J11: LED1 ON : J12: Delay 1s : J13: LED1 OFF : J14: Delay 1s : } : void task2(void) : { : J21: LED2 ON : J22: Delay 2s : J23: LED2 OFF : J24: Delay 2s : } : : 补充说明(Supplement):目前的想法是想用中断达到此功能,先执行J11,中断触发执行 : J21,中断执行完毕再回去执行J12,中断又触发在执行J22....以此类推。看似很简单的功 : 能,但要写程式时却不知道如何下手。 : 问题1: 第一个中断触发後,要怎麽样让中断副程式进入task2这个function? : 问题2: 进入task2这个function後,如何让中断副程式"只"做一行後就结束? : 问题3: 第二个中断触发後,如何让第二个中断从第一个中断结束的地方继续做? : 问题4: 如3,第二个中断结束後,要如何返回J12,而不是重新从J11开始执行? : 问题5: 这些功能是不是可以只靠"中断+if else"就达到?最多再多一个timer : 问题6: 我该如何思考问题?虽然我现在有办法把问题列出来(并不完全),但最重要的是, : 我要如何"思考"这些问题? : 谢谢各位前辈的指点与指教,小弟感激不尽。 -- 纸上得来终觉浅,绝知此事要躬行。 --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 49.214.179.124
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1501828031.A.238.html ※ 编辑: descent (113.196.174.254), 08/04/2017 17:35:39







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Boy-Girl站内搜寻

TOP