static void * sparky_main(void *unused) { track_t *track; stop_t *stop; int last_state = 0; set_lights(false, false); if ((track = track_new("tesla.wav")) == NULL) { perror("tesla.wav"); exit(1); } if (track) track_set_volume(track, 100); stop = stop_new(); while (1) { stop_reset(stop); if (track) track_play_asynchronously(track, stop); while (! stop_is_stopped(stop)) { int ms = random_number_in_range(25, 100); int state; while ((state = random_number_in_range(0, 3)) == last_state) {} last_state = state; set_lights(state & 1, state & 2); ms_sleep(ms); } } return NULL; }
int main(int argc, char **argv) { stop_t *stop; track_t *track; fogger_args_t fogger_args = { .default_duty = 0.01, .delta_duty = 0.001, }; seed_random(); if (wb_init() < 0) { perror("wb_init"); exit(1); } if ((track = track_new("coffin-dog.wav")) == NULL) { perror("coffin-dog.wav"); exit(1); } //track_set_volume(track, 50); stop = stop_new(); fogger_run_in_background(FOGGER_PIN, &fogger_args); wb_set(LIGHT_PIN, 1); wb_set(LID_PIN, 0); while (true) { bool is_lit = true; stop_reset(stop); track_play_asynchronously(track, stop); ms_sleep(ACTION_START); while (! stop_is_stopped(stop)) { int open_time = random_number_in_range(LID_OPEN_LOW, LID_OPEN_HIGH); int closed_time = random_number_in_range(LID_CLOSED_LOW, LID_CLOSED_HIGH); printf("\t\tlid %d %d\n", open_time, closed_time); fflush(stdout); wb_set(LID_PIN, 1); blink_lights_for(&is_lit, open_time); wb_set(LID_PIN, 0); blink_lights_for(&is_lit, closed_time); } wb_set(LIGHT_PIN, 1); wb_set(LID_PIN, 0); ms_sleep(random_number_in_range(INTERDELAY_LOW, INTERDELAY_HIGH)); } return 0; }
static void blink_main(void) { unsigned mask = 31 | (31 << 8); /* bits 1-5 of both banks*/ while (1) { ms_sleep(random_number_in_range(200, 500)); wb_set_outputs(mask, random_number_in_range(0, mask)); } }
void add_grain_on_random_square (int radius, int seed) { TRACEINW("(radius=%d seed=%d)", radius, seed); srandom(seed); int random_x = random_number_in_range(-radius, radius); int random_y = random_number_in_range(-radius, radius); TRACEMESS("Add 1 grain on (%d,%d)", random_x, random_y); add_grains_on_square(random_x, random_y, 1); TRACEOUT; }
int main(int argc, char **argv) { int last_track = -1; if (argc == 2 && strcmp(argv[1], "--always") == 0) { always = 1; } else if (argc > 1) { fprintf(stderr, "usage: [--always]\n"); exit(1); } if (wb_init() < 0) { fprintf(stderr, "failed to initialize wb\n"); exit(1); } set_flash(false); load_tracks(); while (1) { int track; int bolt; if (! always) { ween_time_wait_until_valid(ween_time_constraints, n_ween_time_constraints); } ms_sleep(random_number_in_range(2000, 5000)); do { track = random_number_in_range(0, N_TRACKS-1); } while (track == last_track); last_track = track; track_set_volume(tracks[track], ween_time_is_valid(&is_halloween_constraint, 1) ? 100 : 75); track_play_asynchronously(tracks[track], NULL); for (bolt = 0; bolt < N_BOLTS && bolt_times[track][bolt]; bolt++) { int flashes = random_number_in_range(1, 2); int ms_left = bolt_times[track][bolt]; while (flashes-- && ms_left) { set_flash(true); ms_left -= do_sleep(200, ms_left); set_flash(false); ms_left -= do_sleep(50, ms_left); } if (ms_left) ms_sleep(ms_left); } } }
static void do_attack(unsigned pin, lights_t *l, double up, double down) { unsigned i; for (i = 0; i < 3; i++) { wb_set(ANIMATION_OUTPUT_BANK, pin, 1); ms_sleep((500 + random_number_in_range(0, 250) - 125)*up); wb_set(ANIMATION_OUTPUT_BANK, pin, 0); ms_sleep((200 + random_number_in_range(0, 100) - 50)*down); } }
static int do_sleep(int ms, int ms_left) { int this_ms = random_number_in_range(50, ms+50); if (this_ms > ms_left) this_ms = ms_left; ms_sleep(this_ms); return this_ms; }
static void joust(void *picked_winner_as_vp, lights_t *l, unsigned pin) { unsigned picked_winner = (unsigned) picked_winner_as_vp; unsigned want_winner = picked_winner == WIN_2_PIN; unsigned winner_id = random_number_in_range(0, 1); stop_stop(pick_stop); fprintf(stderr, "Starting joust\n"); lights_blink_one(l, pin); track_play_asynchronously(jousting, stop); motor_forward(0, 1); motor_forward(1, 1); ms_sleep(WIN_MS); stop_stop(stop); track_play_asynchronously(crash, stop); motor_forward(0, TROT_DUTY); motor_forward(1, TROT_DUTY); ms_sleep(TROT_MS); stop_stop(stop); motor_stop(0); motor_stop(1); fprintf(stderr, "wanted %d got %d\n", want_winner, winner_id); if (winner_id == 0) { wb_set(MOTOR_BANK, WINNER_LIGHT_1, 1); } else { wb_set(MOTOR_BANK, WINNER_LIGHT_2, 1); } if (winner_id == want_winner) { track_play(winner); } else { track_play(loser); } ms_sleep(1000); wb_set(MOTOR_BANK, WINNER_LIGHT_1, 0); wb_set(MOTOR_BANK, WINNER_LIGHT_2, 0); lights_off(l); track_play_asynchronously(beeping, stop); go_to_start_position(); stop_stop(stop); }
static void blink_lights_for(bool *is_lit, int ms) { struct timespec start; struct timespec now; nano_gettime(&start); do { int light_time = random_number_in_range(LIGHT_LOW, LIGHT_HIGH); *is_lit = !(*is_lit); wb_set(LIGHT_PIN, *is_lit); printf("light %d for %d\n", *is_lit, light_time); fflush(stdout); ms_sleep(light_time); nano_gettime(&now); } while (nano_elapsed_ms(&now, &start) < ms); }