Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 0:ee91220d7bea
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Tue Nov 30 23:53:06 2010 +0000
@@ -0,0 +1,185 @@
+/**
+ * This is hw2_3 that shows the implementation of
+ * protothreads. The program consists of three protothreads that wait
+ * for each other to toggle a variable.
+ */
+
+/* We must always include pt.h in our protothreads code. */
+#include "pt.h"
+#include "mbed.h"
+#include "pre_work.h"
+
+/* Three flags that the three protothread functions use. */
+static int protothread1_flag, protothread2_flag, protothread3_flag;
+
+ int count=0;
+ int pre_cir_pro=0; // in case of initial trigger fault
+ int judge=1;
+ int shown=0;
+/**
+ * The first protothread function. It achieves the pc input part.
+ *
+ * The protothread function is driven by the main loop further down in
+ * the code.
+ */
+static int
+protothread1(struct pt *pt)
+{
+ PT_BEGIN(pt);
+ /* We loop forever here. */
+ while(1) {
+ /* Wait until the other protothread has set its flag. */
+ PT_WAIT_UNTIL(pt, protothread2_flag != 0 || pc.readable());//wait until pc inputs
+ // printf("Protothread 1 running\n");
+ /* We then reset the other protothread's flag, and set our own
+ flag so that the other protothread can run. */
+ protothread2_flag = 0;
+ protothread1_flag = 1;
+ char temp;
+ if (pc.readable()) {
+ temp=pc.getc();
+ if (temp=='E'&& length!=0){pc.printf("Input ends");}
+ else if (temp=='E'&& length==0){pc.printf("Error. No 'S' before 'E'");}
+ else if (temp=='S'){pc.printf("It is a new start.");length=0;count=0;}
+ else if (temp=='1'){
+ str[length]='1';length++;}
+ else if (temp=='0'){
+ str[length]='0';length++;}
+ else if (temp==' '){}
+ else senderror2();
+ if (length>40) {pc.printf("The allocated space is too small.");}
+ for (int i=0;i<length; i++)
+ { pc.putc(str[i]);}
+ pc.printf("\n");
+
+ }
+ // PT_WAIT_UNTIL(pt, pc.readable());
+ // pc.printf("I am here. Start inputing.");
+ // pc.getc();
+ // length=gtchar();
+ /* And we loop. */
+ protothread3_flag=1;
+ }
+
+ /* All protothread functions must end with PT_END() which takes a
+ pointer to a struct pt. */
+ PT_END(pt);
+}
+
+/**
+ * The second protothread function. This is almost the same as the
+ * first one. This part helps with the sensor input.
+ */
+static int
+protothread2(struct pt *pt)
+{
+ PT_BEGIN(pt);
+ while(1) {
+ /* Let the other protothread run. */
+ protothread2_flag = 1;
+ /* Wait until the other protothread has set its flag. */
+ PT_WAIT_UNTIL(pt, protothread1_flag != 0); //This part always usually has not trigger info
+ // printf("Protothread 2 running\n");
+ /* We then reset the other protothread's flag. */
+ protothread1_flag = 0;
+ if (touchSense1()&& !touchSense2()) { //T0 happens
+ myled1 = 1;
+ myled2=0;
+ pc.putc('a');
+ shown=0;
+ while(1) {if (!test1()){break;} if (touchSense2()&& touchSense1()) {break;}}
+ // if (str[count]=='0') {judge=0;}
+ // judge=1;
+ // pc.putc(str[count]);
+ if (count>=length){
+ for (int i=1;i<count;i++)
+ { str2[i-1]=str2[i];
+ pc.putc(str2[i-1]);
+ }
+ str2[count-1]='1';
+ pc.putc(str2[count-1]);}
+ else { str2[count]='1';count++;}
+ //process_char();
+ } else if (touchSense2()&& !touchSense1()) { //T1 happens
+ if (pre_cir_pro>30){
+ myled1 = 0;
+ myled2=1;
+ pc.putc('b');
+ shown=0;
+ while(1) {if (!test2()){break;} if (touchSense2()&& touchSense1()) {break;} }
+ // if (str[count]=='1') {judge=0;}
+ // judge=0;
+ // pc.putc(str[count]);
+ if (count>=length){
+ for (int i=1;i<count;i++)
+ { str2[i-1]=str2[i];
+ pc.putc(str2[i-1]);
+ }
+ str2[count-1]='0';
+ pc.putc(str2[count-1]);}
+ else { str2[count]='0';count++;}
+ }
+ } else if (touchSense2()&& touchSense1()) { //Touch error
+ myled1 = 1;
+ myled2=1;
+ senderror();
+ } else { //anything else
+ myled1=0;
+ myled2=0;
+ }
+ pre_cir_pro++;
+ /* And we loop. */
+ protothread3_flag=1;
+ }
+ PT_END(pt);
+}
+
+static int
+protothread3(struct pt *pt)
+{
+ /* A protothread function must begin with PT_BEGIN() which takes a
+ pointer to a struct pt. Match judging and output */
+ PT_BEGIN(pt);
+ /* We loop forever here. */
+ while(1) {
+ PT_WAIT_UNTIL(pt,count>=length &&count!=0 && protothread3_flag==1 );
+ // pc.printf("here");
+ for (int i=0;i<length;i++)
+ { if (str[i]!=str2[i]) {judge=0;} }
+ if (judge==1 && shown==0) {pc.printf("\nMATCH");shown=1;}
+ judge=1;
+ protothread3_flag=0;
+ // PT_WAIT_UNTIL(pt, pc.readable());
+ // pc.printf("I am here. Start inputing.");
+ // pc.getc();
+ // length=gtchar();
+ /* And we loop. */
+ }
+
+ /* All protothread functions must end with PT_END() which takes a
+ pointer to a struct pt. */
+ PT_END(pt);
+}
+
+
+/**
+ * Finally, we have the main loop. Here is where the protothreads are
+ * initialized and scheduled. Almost nothing in it.
+ */
+static struct pt pt1,pt2,pt3;
+int
+main(void)
+{
+ /* Initialize the protothread state variables with PT_INIT(). */
+ PT_INIT(&pt1);
+ PT_INIT(&pt2);
+ PT_INIT(&pt3);
+ /*
+ * Then we schedule the three protothreads by repeatedly calling their
+ * protothread functions and passing a pointer to the protothread
+ * state variables as arguments.
+ */while(1){ protothread2(&pt2);
+ protothread1(&pt1);
+ protothread3(&pt3);
+ }
+}