unsigned char motor_wait(unsigned int time_ms) { unsigned char button; unsigned long time = get_ms(); while (get_ms() - time < time_ms) { // check if top or bottom buttons have been pressed button = button_is_pressed(TOP_BUTTON | MIDDLE_BUTTON); if (button != 0) // if so, turn off motors and LEDs, return button ID { set_motors(0, 0); return button; } lcd_goto_xy(10, 0); print("cur: "); print_unsigned_long(x2_get_motor_current(MOTOR1)); print(" "); lcd_goto_xy(10, 1); print("cur: "); print_unsigned_long(x2_get_motor_current(MOTOR2)); print(" "); } return 0; }
void computer_control_task() { hardwareInit(); engineSetSpeed(300); while (1) { if ((savedTime + 100) < get_ms()) { if (flag) { engineSetSpeed(300); flag = 0; } else { engineSetSpeed(200); flag = 1; } savedTime = get_ms(); } } }
void test_lwext4_cleanup(void) { long int start; long int stop; long int diff; int r; printf("\ncleanup:\n"); r = ext4_fremove("/mp/hello.txt"); if (r != EOK && r != ENOENT) { printf("ext4_fremove error: rc = %d\n", r); } printf("remove /mp/test1\n"); r = ext4_fremove("/mp/test1"); if (r != EOK && r != ENOENT) { printf("ext4_fremove error: rc = %d\n", r); } printf("remove /mp/dir1\n"); io_timings_clear(); start = get_ms(); r = ext4_dir_rm("/mp/dir1"); if (r != EOK && r != ENOENT) { printf("ext4_fremove ext4_dir_rm: rc = %d\n", r); } stop = get_ms(); diff = stop - start; printf("cleanup: time: %d ms\n", (int)diff); printf_io_timings(diff); }
void music_test() { static char fugue_title_pos = 0; static long last_shift = 0; char c,i; if(get_ms() - last_shift > 250) { for(i=0; i<8; i++) { c = pgm_read_byte(fugue_title + fugue_title_pos + i); print_character(c); } last_shift = get_ms(); fugue_title_pos ++; if(fugue_title_pos + 8 >= sizeof(fugue_title)) fugue_title_pos = 0; } if(!is_playing()) { play_from_program_space(fugue); } delay_ms(100); }
void pot_test() { long start = get_ms(); char elapsed_ms; int value; set_analog_mode(MODE_10_BIT); print_long(read_trimpot()); print(" "); // to clear the display while((elapsed_ms = get_ms() - start) < 100) { value = read_trimpot(); play_frequency(value, 200, 15); if(value < elapsed_ms*10) { red_led(0); green_led(1); } else { red_led(1); green_led(0); } } }
void bench(void) { Chess chess; SearchData sd; S64 timer; int npos = 0; int nfen = (int)(sizeof(bench_fen) / sizeof(char*)); double avg_bfactor; double t_elapsed; double hhit_rate; U64 nnodes_all; /* num. of all nodes (main + qs) */ int nps; /* nodes (all types) per second */ int i; init_chess(&chess); init_search_data(&sd); chess.max_depth = 8; chess.increment = 60000; printf("Running benchmark at search depth %d...\n", chess.max_depth); timer = get_ms(); progressbar(nfen, 0); for (i = 0; i < nfen; i++) { const char *fen = bench_fen[i]; if (strlen(fen) <= 1) continue; if (!fen_to_board(&chess.board, fen)) { id_search(&chess, NULLMOVE); if (chess.sd.cmd_type != CMDT_CONTINUE) { printf("Benchmark cancelled by user\n"); return; } sd.nnodes += chess.sd.nnodes; sd.nqs_nodes += chess.sd.nqs_nodes; sd.nhash_probes += chess.sd.nhash_probes; sd.nhash_hits += chess.sd.nhash_hits; sd.bfactor += chess.sd.bfactor; npos++; progressbar(nfen, npos); } else printf("\nInvalid FEN string: %s\n", fen); } timer = get_ms() - timer; t_elapsed = timer / 1000.0; avg_bfactor = sd.bfactor / npos; hhit_rate = (sd.nhash_hits * 100.0) / sd.nhash_probes; nnodes_all = sd.nnodes + sd.nqs_nodes; nps = (double)nnodes_all / ((double)timer / 1000.0); printf("\n\nBenchmark finished in %.2f seconds.\n", t_elapsed); printf("Main nodes searched: %" PRIu64 "\n", sd.nnodes); printf("Quiescence nodes searched: %" PRIu64 "\n", sd.nqs_nodes); printf("Total nodes per second: %d\n", nps); printf("Average branching factor: %.2f\n", avg_bfactor); printf("Hash table hit rate: %.2f%%\n", hhit_rate); }
void delay_ms(uint_least32_t delay) { uint_least32_t current = get_ms(); while((get_ms() - current) < delay); return; }
/* Run a test suite like: - Win at Chess (WAC) - Winning Chess Sacrifices and Combinations (WCSAC) - Encyclopedia of Chess Middlegames (ECM) The tests in the suite must be in the format test_pos() uses. */ void test_suite(Chess *chess, const char *filename) { S64 timer; int npos = 0; int nsolved = 0; int nfailed = 0; char pos[MAX_BUF]; SearchData *sd; SearchData sd_total; FILE *fp; if ((fp = fopen(filename, "r")) == NULL) { my_perror("Can't open file %s", filename); return; } sd = &chess->sd; init_search_data(&sd_total); printf("Running test suite...\n"); timer = get_ms(); while (fgetline(pos, MAX_BUF, fp) != EOF) { if (strlen(pos) <= 1) continue; npos++; printf("%d.: ", npos); switch (test_pos(chess, pos)) { case -1: printf("Invalid test position: %s\n", pos); continue; case 0: printf("Couldn't solve test: %s\n", pos); nfailed++; break; case 1: printf("Solved test: %s\n", pos); nsolved++; break; case 2: printf("Test suite cancelled by user\n"); my_close(fp, filename); return; } sd_total.nnodes += sd->nnodes; sd_total.nqs_nodes += sd->nqs_nodes; sd_total.nhash_hits += sd->nhash_hits; sd_total.nhash_probes += sd->nhash_probes; sd_total.bfactor += sd->bfactor; } my_close(fp, filename); sd_total.bfactor /= nsolved + nfailed; timer = get_ms() - timer; printf("\n"); print_search_data(&sd_total, (int)timer); printf("\n%d of %d tests were solved.\n", nsolved, nsolved + nfailed); }
void c_seq::remove(wisdom_IOStream& io) { SEQ_INIT(); if (!is_clean()) return; c_wlock lock(&m_lock); uint32 del_count = 0; uint64_t ms = get_ms(); uint32 last = m_seq_head.g_cleantime(); uint32 count = m_seq_head.g_count(); uint32 ibegin = seq_begin_index(); leveldb::WriteOptions op; leveldb::WriteBatch bh; while (true) { string value; _zmsg_head* head = NULL; if (c_base::get_value(__tos(m_key << "@" << ibegin), value, &head) != 0) { break; } if (head->type != T_SEQ_VALUE) { LOG4_ERROR("SEQ KEY:" << __tos(m_key << "@" << ibegin) << " error."); break; } uint32 idelay = m_seq_head.g_delay() == 0 ? c_server::get_instance()->seq_valid_time() : m_seq_head.g_delay(); if (head->g_effective() + idelay > time(0)) { break; } seq_del(ibegin, bh); del_count++; ibegin++; } m_seq_head.s_cleantime(time(0)); seq_update(bh); if (!c_base::write(op, bh)) { m_seq_head.s_count(count); ZERROR_RESULT("seq write error."); } io->push(ZRESULT_OK); LOG4_INFO("SEQ " << m_key << " clean record:" << del_count << " last min " << ((time(0) - last) / 60) << " cost " << (uint32)(get_ms() - ms) << " ms"); }
/* Choose the best move (by searching or using the book), and make it. */ static void cpu_move(Chess *chess) { bool book_used = false; char san_move[MAX_BUF]; char str_move[MAX_BUF]; U32 move = NULLMOVE; int score = 0; S64 timer; Board *board; ASSERT(1, chess != NULL); board = &chess->board; timer = get_ms(); chess->sd.cmd_type = CMDT_CONTINUE; if (settings.book_type != BOOK_OFF) move = get_book_move(board, chess->show_pv, chess->book); if (move != NULLMOVE) { book_used = true; chess->in_book = true; } else { score = id_search(chess, NULLMOVE); if (chess->sd.cmd_type == CMDT_CANCEL) { chess->cpu_color = COLOR_NONE; return; } move = chess->sd.move; chess->in_book = false; } timer = get_ms() - timer; ASSERT(1, move != NULLMOVE); move_to_str(move, str_move); if (SIGN(board->color)*score < VAL_RESIGN) { if (board->color == WHITE) printf("0-1 {White resigns}\n"); else printf("1-0 {Black resigns}\n"); chess->game_over = true; return; } printf("move %s\n", str_move); if (chess->debug && chess->sd.nnodes > 0) { print_search_data(&chess->sd, (int)timer); printf("Score: %d\n", score); } move_to_san(san_move, board, move); update_game_log(board, san_move, score, book_used); update_game(chess, move); }
void perft_file(char *file) { FILE *f; char s[256]; char fen[256]; int nmoves, ln; int depth; Bitmap ms, ds; char id[100]; Bitmap x; Bitmap inx; ms = get_ms(); inx = 0; ln = 0; f = fopen(file, "rb"); if( ! f ) { printf( "%s can't be open\n", file ); return; } strcpy( id, "-" ); while (fgets(s, 256, f)) { ++ln; if (!strncmp(s, "id ", 3)) { strcpy(id, s + 4); strip(id); } else if (!strncmp(s, "epd ", 4)) { strcpy(fen, s + 4); strip(fen); strcat(fen, " 0 1"); } else if (!strncmp(s, "perft ", 6)) { sscanf(s + 6, "%d %d", &depth, &nmoves); printf( "%5d: [%s] depth:%2d must be:%10d -> ", ln, fen, depth, nmoves ); x = calc_perft(fen, depth); if (nmoves == x) printf( "ok\n" ); else { printf( "ERROR calculated:%d\n", (int)x ); break; } inx += x; } } fclose(f); ds = get_ms() - ms; printf("\nTotal:%lu ms", (long unsigned int) ds); if( ds ) { printf( " (%lu)", (long unsigned int) (inx * 1000 / ds)); } printf( "\n"); }
static int wait_flash_ready() { // Wait for the flash controller to be ready or to timeout. Note although // we implement a timeout operation, it is not clear what to do if we // ever get one. If the system is still trying to write to flash then // it may not be possible to execute code from flash... int status; int timeout = get_ms() + FLASH_TIMEOUT; do { status = *AT91C_MC_FSR; if (status & AT91C_MC_FRDY) return status; } while (get_ms() < timeout); return status; }
void u2f_hid_check_timeouts() { uint8_t i; for(i = 0; i < CID_MAX; i++) { if (CIDS[i].busy && ((get_ms() - CIDS[i].last_used) >= 750)) { u2f_printlx("timeout cid ",2,CIDS[i].cid,get_ms()); stamp_error(CIDS[i].cid, ERR_MSG_TIMEOUT); del_cid(CIDS[i].cid); u2f_hid_reset_packet(); } } }
/* Probe the book (already in RAM) for a good move to be played. Returns NULLMOVE if no book moves with a score above 0 are found. If <show_book> is true, a list of available moves is displayed. */ U32 get_book_move(Board *board, bool show_book, const AvlNode *book) { int i; int tot_score; int cur_score; int rand_val; MoveLst move_list; ASSERT(1, board != NULL); if (settings.book_type == BOOK_MEM && book == NULL) return NULLMOVE; cur_score = 0; tot_score = get_book_move_list(board, &move_list, book); if (tot_score <= 0) return NULLMOVE; if (show_book) print_book_x(board, &move_list, tot_score); my_srand((int)get_ms()); rand_val = my_rand() % tot_score; for (i = 0; i < move_list.nmoves; i++) { int score = move_list.score[i]; if (score != VAL_NONE) { cur_score += score; if (cur_score > rand_val) return move_list.move[i]; } } return NULLMOVE; }
// Runs through an automatic calibration sequence void auto_calibrate() { time_reset(); set_motors(60, -60); while(get_ms() < 250) calibrate_line_sensors(IR_EMITTERS_ON); set_motors(-60, 60); while(get_ms() < 750) calibrate_line_sensors(IR_EMITTERS_ON); set_motors(60, -60); while(get_ms() < 1000) calibrate_line_sensors(IR_EMITTERS_ON); set_motors(0, 0); serial_send_blocking("c",1); }
void bench() { int i; int t[3]; double nps; /* setting the position to a non-initial position confuses the opening book code. */ close_book(); for (i = 0; i < 64; ++i) { color[i] = bench_color[i]; piece[i] = bench_piece[i]; } side = LIGHT; xside = DARK; castle = 0; ep = -1; fifty = 0; ply = 0; hply = 0; set_hash(); print_board(); max_time = 1 << 25; max_depth = 5; for (i = 0; i < 3; ++i) { think(1); t[i] = get_ms() - start_time; printf("Time: %d ms\n", t[i]); } if (t[1] < t[0]) t[0] = t[1]; if (t[2] < t[0]) t[0] = t[2]; printf("\n"); printf("Nodes: %d\n", nodes); printf("Best time: %d ms\n", t[0]); if (!ftime_ok) { printf("\n"); printf("Your compiler's ftime() function is apparently only accurate\n"); printf("to the second. Please change the get_ms() function in main.c\n"); printf("to make it more accurate.\n"); printf("\n"); return; } if (t[0] == 0) { printf("(invalid)\n"); return; } nps = (double)nodes / (double)t[0]; nps *= 1000.0; /* Score: 1.000 = my Athlon XP 2000+ */ printf("Nodes per second: %d (Score: %.3f)\n", (int)nps, (float)nps/243169.0); init_board(); open_book(); gen(); }
static void busy_loop_thread(void){ int cpu = atomic_add_get_oldval(&bl_cpu_num,1); bound_thread_to_cpu(cpu_nums[cpu]); set_realtime(SCHED_FIFO,audio_priority - 2); while(true){ semaphore_wait(bl_semaphore[cpu]); //fprintf(stderr,"starting to busy-loop %d\n",cpu); while(bl_please_stop[cpu]==0){ double start = get_ms(); while( (get_ms() - start) < 0.01); //sched_yield(); } //fprintf(stderr,"stopping busy-loop %d\n",cpu); } }
void perft(int depth) { Bitmap ms, ds; Bitmap rs; ms = get_ms(); board_reset(); movegen(); rs = xperft(depth); ds = get_ms() - ms; printf("Total:%lu ", (long unsigned int) rs); if( ds ) { printf( " (%lu positions/second)", (long unsigned int) (rs * 1000 / ds)); } printf( "\n"); }
/* checkup() is called once in a while during the search. */ void checkup(clock_t stoping_time) { must_stop = 0; if (get_ms() >= stoping_time) { must_stop = 1; } }
void checkup() { /* is the engine's time up? if so, longjmp back to the beginning of think() */ if (get_ms() >= stop_time) { stop_search = TRUE; longjmp(env, 0); } }
void time_test() { static long elapsed_time = 0; static long last_read = 0; static long is_ticking = 0; static char a_is_pressed = 0; static char c_is_pressed = 0; static char last_seconds = 0; long current_time = get_ms(); if(is_ticking) elapsed_time += current_time - last_read; last_read = current_time; if(button_is_pressed(BUTTON_A) && !a_is_pressed) { // reset a_is_pressed = 1; is_ticking = 0; elapsed_time = 0; if(!is_playing()) // only play once play_from_program_space(beep_button_a); } // find the end of the button press without stopping if(!button_is_pressed(BUTTON_A)) a_is_pressed = 0; if(button_is_pressed(BUTTON_C) && !c_is_pressed) { // start/stop c_is_pressed = 1; is_ticking = !is_ticking; play_from_program_space(beep_button_c); } // find the end of the button press without stopping if(!button_is_pressed(BUTTON_C)) c_is_pressed = 0; print_long((elapsed_time/1000/60/10)%10); // tens of minutes print_long((elapsed_time/1000/60)%10); // minutes print_character(':'); print_long((elapsed_time/1000)%60/10); // tens of seconds char seconds = ((elapsed_time/1000)%60)%10; print_long(seconds); // seconds print_character('.'); print_long((elapsed_time/100)%10); // tenths of seconds print_long((elapsed_time/10)%10); // hundredths of seconds // beep every second if(seconds != last_seconds && elapsed_time != 0 && !is_playing()) play_from_program_space(timer_tick); last_seconds = seconds; }
int8_t u2f_get_user_feedback() { uint32_t t; //_delay_ms(1); t = get_ms(); while(U2F_BUTTON_IS_PRESSED()){ idleTasks(); sleep_cpu(); } while(!U2F_BUTTON_IS_PRESSED()) { // turn red if (serious) { //rgb_hex(U2F_DEFAULT_COLOR_ERROR); } else { // yellow //rgb_hex(U2F_DEFAULT_COLOR_INPUT); } if (get_ms() - t > 10000) { // FIXME: for testing. Accept after 10 second timeout return 0; } // break; //watchdog(); idleTasks(); sleep_cpu(); } if (U2F_BUTTON_IS_PRESSED()) { //rgb_hex(U2F_DEFAULT_COLOR_INPUT_SUCCESS); } else { //rgb_hex(U2F_DEFAULT_COLOR_ERROR); return 1; } return 0; }
double ms_ticker_synchronizer_set_external_time(MSTickerSynchronizer* ts, const MSTimeSpec *time) { int64_t sound_time,diff; uint64_t wc = get_wallclock_ms(); uint64_t ms = get_ms(time); if (ts->offset == 0) { ts->offset = wc - ms; } sound_time = ts->offset + ms; diff = wc - sound_time; ts->av_skew = (ts->av_skew * (1.0 - clock_coef)) + ((double) diff * clock_coef); return ts->av_skew; }
bool test_lwext4_dir_test(int len) { ext4_file f; int r; int i; char path[64]; long int diff; long int stop; long int start; printf("test_lwext4_dir_test: %d\n", len); io_timings_clear(); start = get_ms(); printf("directory create: /mp/dir1\n"); r = ext4_dir_mk("/mp/dir1"); if (r != EOK) { printf("ext4_dir_mk: rc = %d\n", r); return false; } printf("add files to: /mp/dir1\n"); for (i = 0; i < len; ++i) { sprintf(path, "/mp/dir1/f%d", i); r = ext4_fopen(&f, path, "wb"); if (r != EOK) { printf("ext4_fopen: rc = %d\n", r); return false; } } stop = get_ms(); diff = stop - start; test_lwext4_dir_ls("/mp/dir1"); printf("test_lwext4_dir_test: time: %d ms\n", (int)diff); printf("test_lwext4_dir_test: av: %d ms/entry\n", (int)diff / (len + 1)); printf_io_timings(diff); return true; }
int game_init() { srand(get_ms()); screen = set_videomode(); init_res(); game_start(); /* Should not reach here! */ assert(0); return 0; }
/** * Benchmark test that calculates how many nodes/second chess-at-nite searches. * * It sets the position to move 18 of "The Game of the Century" * Donald Byrne vs Robert James Fischer (Rosenwald Memorial Tour, Oct 17 1956) * More info: * - http://en.wikipedia.org/wiki/The_Game_of_the_Century_%28chess%29 * - http://www.chessgames.com/perl/chessgame?gid=1008361 * - http://goo.gl/Yp0o (shortet the above game) */ void CLI::run_benchmark() { // old bench: "rq3rk1/4bppp/p1Rp1n2/8/4p3/1B2BP2/PP4PP/3Q1RK1 w - - 0 17" string fen = BENCHMARK_FEN; Board* board = new Board(fen); Player* player = new ComputerPlayer(false); player->set_board(board); player->set_max_thinking_time(max_thinking_time); //during the benchmark show the thinking it's fun... player->set_show_thinking(true); cout << "----- The Game of the Century -----\n"; cout << "Donald Byrne vs Robert J. Fischer\n"; cout << "Rosenwald Memorial, Oct 17 1956\n"; cout << "Watch the game: http://goo.gl/Yp0o\n"; cout << *board; int times[3]; int nodes[3]; for (int i = 0; i < 3; i++) { int start = get_ms(); player->get_move(); times[i] = get_ms() - start; nodes[i] = board->checked_nodes; } cout << "-------- Benchmark Results --------\n"; double best_nps = 0; for (int i = 0; i < 3; i++) { double nps = (nodes[i] / (double) times[i]) * 1000; if (best_nps < nps) { best_nps = nps; } cout << " Run #" << i + 1 << ": " << (int) nps << " nodes/sec\n"; } cout << " Best of 3: " << (int) (best_nps) << " nodes/sec\n"; delete player; delete board; }
int main() { // set up the 3pi initialize(); int last_proximity = 0; const int base_speed = 200; const int set_point = 100; // what is the set point for? // This is the "main loop" - it will run forever. while(1) { // In case it gets stuck: for 1 second every 15 seconds back up if (get_ms() % 15000 > 14000) { back_up(); continue; } // If something is directly in front turn to the right in place int front_proximity = analog_read(5); if (front_proximity > 200) { turn_in_place(); continue; } int proximity = analog_read(1); // 0 (far away) - 650 (close) int proportional = proximity - set_point; int derivative = proximity - last_proximity; // Proportional-Derivative Control Signal int pd = proportional / 3 + derivative * 20; int left_set = base_speed + pd; int right_set = base_speed - pd; set_motors(left_set, right_set); if (TIME_TO_DISPLAY) { clear(); lcd_goto_xy(0,0); print_long(proximity); lcd_goto_xy(5,0); print_long(pd); lcd_goto_xy(0,1); print_long(left_set); lcd_goto_xy(4,1); print_long(right_set); } last_proximity = proximity; // remember last proximity for derivative } // This part of the code is never reached. while(1) { set_motors(0,0) } }
int main() { lcd_init_printf(); clear(); // clear the LCD printf("Time: "); while (1) { unsigned char button = get_single_debounced_button_press(ANY_BUTTON); switch (button) { case BUTTON_A: play_note(A(4), 50, 10); break; case BUTTON_B: play_note(B(4), 50, 10); break; case BUTTON_C: play_note(C(5), 50, 10); } button = get_single_debounced_button_release(ANY_BUTTON); switch (button) { case BUTTON_A: play_note(A(5), 50, 10); break; case BUTTON_B: play_note(B(5), 50, 10); break; case BUTTON_C: play_note(C(6), 50, 10); } unsigned long ms = get_ms(); // get elapsed milliseconds // convert to the current time in minutes, seconds, and hundredths of seconds unsigned char centiseconds = (ms / 10) % 100; unsigned char seconds = (ms / 1000) % 60; unsigned char minutes = (ms / 60000) % 60; lcd_goto_xy(0, 1); // go to the start of the second LCD row // print as [m]m:ss.cc (m = minute, s = second, c = hundredth of second) printf("%2u:%02u.%02u", minutes, seconds, centiseconds); } }
static void jpegenc_callback(jpeg_job_status_t status, uint32_t handle, uint32_t job, mm_jpeg_output_t* out, void* cookie) { logi("do jpeg callback."); if (cookie != &jpeg) { loge("invalid cookie[%p != %p]", cookie, &jpeg); return; } jpeg.status = status; jpeg.out_size = out->buf_filled_len; n2 = get_ms(); logd("====in callback %d", n2-n1); pthread_mutex_lock(&jpeg.lock); pthread_cond_signal(&jpeg.cond); pthread_mutex_unlock(&jpeg.lock); }
// ack:e3458d0aceff8b70a3e5c0afec632881=38;e3458d0aceff8b70a3e5c0afec632881=42; void handle_ack(connection_ptr connection,const wscmd::cmd& command) { wscmd::arg_list::const_iterator arg_it; size_t count; for (arg_it = command.args.begin(); arg_it != command.args.end(); arg_it++) { if (m_msgs.find(arg_it->first) == m_msgs.end()) { std::cout << "ack for message we didn't send" << std::endl; continue; } count = atol(arg_it->second.c_str()); if (count == 0) { continue; } struct msg& m(m_msgs[arg_it->first]); m.acked += count; if (m.acked == m.sent) { m.time = get_ms(m.time_sent); } } }