__init int engine_init(void){ int ret; printk (KERN_INFO ENGINE_DRIVER_NAME ": init\n") ; gpiochip = gpiochip_find("bcm2708_gpio", is_right_chip); if (!gpiochip) return -ENODEV; if (gpio_request(A1_PIN, ENGINE_DRIVER_NAME "A1_PIN")) { printk(KERN_ALERT ENGINE_DRIVER_NAME ": cant claim gpio pin %d\n", A1_PIN); ret = -ENODEV; goto exitA1; } if (gpio_request(A2_PIN, ENGINE_DRIVER_NAME "A2_PIN")) { printk(KERN_ALERT ENGINE_DRIVER_NAME ": cant claim gpio pin %d\n", A2_PIN); ret = -ENODEV; goto exitA2; } if (gpio_request(B1_PIN, ENGINE_DRIVER_NAME "B1_PIN")) { printk(KERN_ALERT ENGINE_DRIVER_NAME ": cant claim gpio pin %d\n", B1_PIN); ret = -ENODEV; goto exitB1; } if (gpio_request(B2_PIN, ENGINE_DRIVER_NAME "B2_PIN")) { printk(KERN_ALERT ENGINE_DRIVER_NAME ": cant claim gpio pin %d\n", B2_PIN); ret = -ENODEV; goto exitB2; } printk(KERN_INFO ENGINE_DRIVER_NAME ": got all pins.\n"); gpiochip->direction_output(gpiochip, A1_PIN, 1); gpiochip->set(gpiochip, A1_PIN, 1); gpiochip->direction_output(gpiochip, A2_PIN, 1); gpiochip->set(gpiochip, A2_PIN, 1); gpiochip->direction_output(gpiochip, B1_PIN, 1); gpiochip->set(gpiochip, B1_PIN, 1); gpiochip->direction_output(gpiochip, B1_PIN, 1); gpiochip->set(gpiochip, B2_PIN, 1); alloc_chrdev_region(&deviceMajMin, 0, 1, DEVICE_NAME); printk (KERN_INFO ENGINE_DRIVER_NAME ": major: %d\n", MAJOR(deviceMajMin)); class = class_create(THIS_MODULE, DEVICE_NAME); cdev_init(&cdev, &fops); cdev.owner = THIS_MODULE; cdev_add(&cdev, deviceMajMin, 1); device_create(class, NULL, deviceMajMin, NULL, DEVICE_NAME); hrtimer_init(&step_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); step_timer.function = &hrtimer_routine; run_timer(steptime); return 0; exitB2: gpio_free(B1_PIN); exitB1: gpio_free(A2_PIN); exitA2: gpio_free(A1_PIN); exitA1: return ret; }
bool TcpServer::_StartUpWin() { #ifdef WIN32 struct timeval tv = {1, 000000}; while(m_bRun) { m_tmp_rset = m_rset; m_tmp_wset = m_wset; int ready = select(m_maxfd+1, &m_tmp_rset, &m_tmp_wset, NULL, &tv); if(ready == -1) { int err = WSAGetLastError(); if (err != EINTR) { return false; } } else if (ready == 0) { run_timer(); continue; } else { run_timer(); if (FD_ISSET(m_listen_fd, &m_tmp_rset)) { handle_accept(); --ready; } for (int i=0; ready > 0 && i< m_maxfd; i++) { TcpHandler* p_socket = fds[i]; if(p_socket == NULL) { continue; } if(FD_ISSET(p_socket->GetFd(), &m_tmp_rset)) { --ready; if (p_socket->handle_read() == -1) { handle_close(p_socket); continue; } } if(FD_ISSET(p_socket->GetFd(), &m_tmp_wset)) { --ready; if (p_socket->handle_output() == -1) { handle_close(p_socket); } } } } } #endif // WIN32 return true; }
/* this is the plugin entry point */ enum plugin_status plugin_start(const void* parameter) { int i; bool done; int nr; (void)parameter; settings.nr_timers = 1; settings.total_time = 10; settings.round_time = 10; /* now go ahead and have fun! */ rb->splash(HZ, "Chess Clock"); rb->lcd_clear_display(); i=0; while (1) { int res; switch (i) { case 0: res=chessclock_set_int("Number of players", &settings.nr_timers, 1, 1, MAX_PLAYERS, 0); break; case 1: res=chessclock_set_int("Total time", &settings.total_time, 10, 10, MAX_TIME, FLAGS_SET_INT_SECONDS); settings.round_time=settings.total_time; break; case 2: res=chessclock_set_int("Max round time", &settings.round_time, 10, 10, settings.round_time, FLAGS_SET_INT_SECONDS); break; default: res=-2; /* done */ break; } if (res==CHCL_USB) { return PLUGIN_USB_CONNECTED; } else if (res==CHCL_CANCEL) { i--; if (i<0) { return PLUGIN_OK; } } else if (res==CHCL_OK) { i++; } else if (res==-2) { /* done */ break; } } for (i=0; i<settings.nr_timers; i++) { timer_holder[i].total_time=settings.total_time; timer_holder[i].used_time=0; timer_holder[i].hidden=false; } chesspause=true; /* We start paused */ nr=0; do { int ret=0; done=true; i = nr; do { if (!timer_holder[i].hidden) { nr = i; done = false; break; } if (++i == settings.nr_timers) i = 0; } while (i != nr); if (done) { return PLUGIN_OK; } ret = run_timer(nr); switch (ret) { case CHCL_CANCEL: /* exit */ done=true; break; case CHCL_USB: return PLUGIN_USB_CONNECTED; case CHCL_NEXT: nr++; if (nr>=settings.nr_timers) nr=0; break; case CHCL_PREV: do { nr--; if (nr<0) nr=settings.nr_timers-1; } while (timer_holder[nr].hidden); break; } } while (!done); return PLUGIN_OK; }
/* play one letters round */ void letters_round(void) { static int turn = 0; int i, j, k; int *player_order; /* stages: pick letters 30 second timer (let dictionary corner think during this time?) reveal word lengths reveal words assign scores dictionary corner words */ printf(" Round %d: Letters round\n", round); printf("It is %s's turn to choose letters.\n", player[turn].name); /* read letter choices and generate letters */ for(i = 0; i < num_letters; i++) get_letter(i); /* let people think */ run_timer(); /* ask each player for their word length */ for(i = 0; i < players; i++) { printf("%s, how long is your word? ", player[(i + turn) % players].name); player[(i + turn) % players].length = atoi(get_line()); } /* get an aray of players sorted by word length */ player_order = malloc(sizeof(int) * players); for(i = 0; i < players; i++) player_order[i] = i; qsort(player_order, players, sizeof(int), length_cmp); /* ask players for their word, shortest first */ for(j = 0; j < players; j++) { i = player_order[j]; if(player[i].length <= 0) { player[i].word = NULL; continue; } printf("%s, what is your %d-letter word? ", player[i].name, player[i].length); player[i].word = strdup(get_line()); if(strlen(player[i].word) != player[i].length || !valid_word(i, letter)) { free(player[i].word); /* try again once if they didn't supply a suitable word */ printf("%s, what is your real %d-letter word? ", player[i].name, player[i].length); player[i].word = strdup(get_line()); if(strlen(player[i].word) != player[i].length || !valid_word(i, letter)) player[i].length = 0; } } /* re-sort to get non-words removed */ qsort(player_order, players, sizeof(int), length_cmp); /* find the best scorers */ for(j = players - 2; j >= 0; j--) { i = player_order[j]; if(player[i].length < player[player_order[players - 1]].length) break; } /* assign points to the scorers */ for(k = players - 1; k > j; k--) { i = player_order[k]; if(player[i].length == num_letters) {/* 9 letters score double */ printf("%d points to %s.\n", num_letters * 2, player[i].name); player[i].score += num_letters * 2; } else { printf("%d points to %s.\n", player[i].length, player[i].name); player[i].score += player[i].length; } } /* TODO: display best word in the blue and white style */ /* ask dictionary corner if they got anything better */ dict_solve(letter); /* TODO: display dictionary corner's word in blue and white */ /* increment the player whose turn it is to choose letters */ round++; turn++; if(turn >= players) turn = 0; /* tidy up */ free(player_order); for(i = 0; i < players; i++) free(player[i].word); }
/* play one numbers round */ void numbers_round(void) { int num_large; int i, j; static int turn = 0; /* stages: pick numbers 30 second timer (let Rachel think during this time?) reveal answers reveal methods assign scores Rachel's solution */ printf(" Round %d: Numbers round\n", round); printf("It is %s's turn to choose numbers.\n", player[turn].name); /* choose numbers */ init_numbers(); do { printf("%s, how many large numbers? [0 to 4] ", player[turn].name); num_large = atoi(get_line()); } while(num_large < 0 || num_large > 4); /* generate numbers */ for(i = 0; i < num_large; i++) { number[i] = get_large(); } for(; i < 6; i++) { number[i] = get_small(); } /* display numbers */ for(j = 6; j >= 0; j--) { printf("\r"); if(nocolour) printf("| "); else printf("%s ", letter_colour); for(i = 0; i < j; i++) { if(number[i] > 9) printf(" "); if(number[i] > 99) printf(" "); printf(" "); } for(i = j; i < 6; i++) { printf("%d ", number[i]); } if(nocolour) printf("|"); else printf("%s", colour_off); fflush(stdout); usleep(500000); } putchar('\n'); speak(RACHEL, "And the target number is...\n"); /* generate a target */ for(i = 0; i < 20; i++) { printf(" \r"); if(!nocolour) printf("%s", number_colour); printf(" %d ", 100 + (rand() % 900)); if(!nocolour) printf("%s", colour_off); fflush(stdout); usleep(100000); } target = 100 + (rand() % 900); printf(" \r"); if(!nocolour) printf("%s", number_colour); printf(" %d ", target); if(!nocolour) printf("%s", colour_off); printf("\n"); /* let people think */ run_timer(); /* increment the player whose turn it is to choose numbers */ round++; turn++; if(turn >= players) turn = 0; }
int main(int argc, char**argv) { int opt; int expectedPomodoros = 0; int action = 0; std::string activityTitle; /* // no arguments given */ if(argc == 1) { fprintf(stderr, "This program needs arguments....\n\n"); print_help(); return 1; } while((opt = getopt(argc, argv, "hVp:a:lbBs:d:c:u:")) != -1) { switch(opt) { case 'h': print_help(); return 0; break; case 'V': printf("%s %s\n\n", PACKAGE, VERSION); return 0; break; case 'l': list_all_activities(); return 0; break; case 'p': if(!optarg || strlen(optarg) < 1) { fprintf(stderr, "%s: Error - Option '%c' needs a value\n\n", PACKAGE, opt); return 1; } else { expectedPomodoros = atoi(optarg); } break; case 'a': printf("Output: %s\n", optarg); if(!optarg || strlen(optarg) < 1) { fprintf(stderr, "%s: Error - Option '%c' needs a value\n\n", PACKAGE, opt); return 1; } else { action = ACTION_ADD; activityTitle = std::string(optarg); } break; case 's': if(!optarg || strlen(optarg) < 1) { fprintf(stderr, "%s: Error - Option '%c' needs a value\n\n", PACKAGE, opt); return 1; } else { runActivity(std::string(optarg)); } break; case 'd': if(!optarg || strlen(optarg) < 1) { fprintf(stderr, "%s: Error - Option '%c' needs a value\n\n", PACKAGE, opt); return 1; } else { deleteActivity(std::string(optarg)); return 0; } break; case 'c': if(!optarg || strlen(optarg) < 1) { fprintf(stderr, "%s: Error - Option '%c' needs a value\n\n", PACKAGE, opt); return 1; } else { completeActivity(std::string(optarg), true); return 0; } break; case 'u': if(!optarg || strlen(optarg) < 1) { fprintf(stderr, "%s: Error - Option '%c' needs a value\n\n", PACKAGE, opt); return 1; } else { completeActivity(std::string(optarg), false); return 0; } break; case 'b': run_timer(BREAK); return 0; break; case 'B': run_timer(LONG_BREAK); return 0; break; case ':': //fprintf(stderr, "%s: Error - Option '%c' needs a value\n\n", PACKAGE, opt); print_help(); return 1; break; case '?': //fprintf(stderr, "%s: Error - No such option: '%c'\n\n", PACKAGE, optopt); print_help(); return 1; } } switch(action) { case ACTION_ADD: add_activity(activityTitle, expectedPomodoros); break; } return 0; }
static void print_hello(unsigned long data) { printk("Hello %d\n", data); run_timer(); }