123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- #ifdef _WIN32
- #include <windows.h>
- #else
- #include <unistd.h>
- #include <sys/time.h>
- #endif
- #include <stdio.h>
- #include "pt.h"
- struct timer { int start, interval; };
- static int timer_expired(struct timer *t);
- static void timer_set(struct timer *t, int usecs);
- static struct timer codelock_timer, input_timer;
- static const char code[4] = {'1', '4', '2', '3'};
- static struct pt codelock_pt, input_pt;
- static char key, key_pressed_flag;
- static void
- press_key(char k)
- {
- printf("--- Key '%c' pressed\n", k);
- key = k;
- key_pressed_flag = 1;
- }
- static int
- key_pressed(void)
- {
- if(key_pressed_flag != 0) {
- key_pressed_flag = 0;
- return 1;
- }
- return 0;
- }
- static
- PT_THREAD(codelock_thread(struct pt *pt))
- {
-
- static int keys;
-
- PT_BEGIN(pt);
-
- while(1) {
-
-
- for(keys = 0; keys < sizeof(code); ++keys) {
-
- if(keys == 0) {
-
- PT_WAIT_UNTIL(pt, key_pressed());
- } else {
-
-
- timer_set(&codelock_timer, 1000);
-
- PT_WAIT_UNTIL(pt, key_pressed() || timer_expired(&codelock_timer));
-
- if(timer_expired(&codelock_timer)) {
- printf("Code lock timer expired.\n");
-
-
- break;
- }
- }
-
- if(key != code[keys]) {
- printf("Incorrect key '%c' found\n", key);
-
- break;
- } else {
- printf("Correct key '%c' found\n", key);
- }
- }
-
- if(keys == sizeof(code)) {
- printf("Correct code entered, waiting for 500 ms before unlocking.\n");
-
- timer_set(&codelock_timer, 500);
- PT_WAIT_UNTIL(pt, key_pressed() || timer_expired(&codelock_timer));
-
- if(!timer_expired(&codelock_timer)) {
- printf("Key pressed during final wait, code lock locked again.\n");
- } else {
-
- printf("Code lock unlocked.\n");
- PT_EXIT(pt);
- }
- }
- }
-
- PT_END(pt);
- }
- static
- PT_THREAD(input_thread(struct pt *pt))
- {
- PT_BEGIN(pt);
- printf("Waiting 1 second before entering first key.\n");
-
- timer_set(&input_timer, 1000);
- PT_WAIT_UNTIL(pt, timer_expired(&input_timer));
- press_key('1');
-
- timer_set(&input_timer, 100);
- PT_WAIT_UNTIL(pt, timer_expired(&input_timer));
-
- press_key('2');
- timer_set(&input_timer, 100);
- PT_WAIT_UNTIL(pt, timer_expired(&input_timer));
-
- press_key('3');
- timer_set(&input_timer, 2000);
- PT_WAIT_UNTIL(pt, timer_expired(&input_timer));
-
- press_key('1');
- timer_set(&input_timer, 200);
- PT_WAIT_UNTIL(pt, timer_expired(&input_timer));
-
- press_key('4');
- timer_set(&input_timer, 200);
- PT_WAIT_UNTIL(pt, timer_expired(&input_timer));
-
- press_key('2');
-
- timer_set(&input_timer, 2000);
- PT_WAIT_UNTIL(pt, timer_expired(&input_timer));
-
- press_key('3');
- timer_set(&input_timer, 200);
- PT_WAIT_UNTIL(pt, timer_expired(&input_timer));
-
- press_key('1');
- timer_set(&input_timer, 200);
- PT_WAIT_UNTIL(pt, timer_expired(&input_timer));
-
- press_key('4');
- timer_set(&input_timer, 200);
- PT_WAIT_UNTIL(pt, timer_expired(&input_timer));
-
- press_key('2');
-
- timer_set(&input_timer, 100);
- PT_WAIT_UNTIL(pt, timer_expired(&input_timer));
-
- press_key('3');
- timer_set(&input_timer, 100);
- PT_WAIT_UNTIL(pt, timer_expired(&input_timer));
-
- press_key('4');
- timer_set(&input_timer, 1500);
- PT_WAIT_UNTIL(pt, timer_expired(&input_timer));
-
- press_key('1');
- timer_set(&input_timer, 300);
- PT_WAIT_UNTIL(pt, timer_expired(&input_timer));
-
- press_key('4');
- timer_set(&input_timer, 400);
- PT_WAIT_UNTIL(pt, timer_expired(&input_timer));
-
- press_key('2');
- timer_set(&input_timer, 500);
- PT_WAIT_UNTIL(pt, timer_expired(&input_timer));
-
- press_key('3');
- timer_set(&input_timer, 2000);
- PT_WAIT_UNTIL(pt, timer_expired(&input_timer));
-
- PT_END(pt);
- }
- int
- main(void)
- {
-
- PT_INIT(&input_pt);
- PT_INIT(&codelock_pt);
-
- while(PT_SCHEDULE(codelock_thread(&codelock_pt))) {
- PT_SCHEDULE(input_thread(&input_pt));
-
-
- #ifdef _WIN32
- Sleep(0);
- #else
- usleep(10);
- #endif
- }
- return 0;
- }
- #ifdef _WIN32
- static int clock_time(void)
- { return (int)GetTickCount(); }
- #else
- static int clock_time(void)
- {
- struct timeval tv;
- struct timezone tz;
- gettimeofday(&tv, &tz);
- return tv.tv_sec * 1000 + tv.tv_usec / 1000;
- }
- #endif
- static int timer_expired(struct timer *t)
- { return (int)(clock_time() - t->start) >= (int)t->interval; }
- static void timer_set(struct timer *t, int interval)
- { t->interval = interval; t->start = clock_time(); }
|