Пример #1
0
int main (int argc , char **argv)
{
	int i = 0;

	printf ("My PID : %d\n", getpid ());

	tmlib_init ();
	char task[4] = "TS";


	void* id = start_timer (10 * tm_get_ticks_per_second (), NULL, (void (*) (void *))&Just_log, 0);
	void* id1 = start_timer ((11) * tm_get_ticks_per_second (), NULL, (void (*) (void *))&Just_log, 0);
	
	setup_timer (&id2, exp, NULL);

	mod_timer (id2, 2);
#if 1
	while (1) {
	printf ("Remaining time id (10 secs): %u  id1 (1 min) : %u \n",timer_get_remaining_time(id) / tm_get_ticks_per_second () ,timer_get_remaining_time(id1) / tm_get_ticks_per_second ());
	sleep (3);
	}
#endif


	timer_restart (id1);
	while (1) {
		sleep (1);
		printf ("Remaining time id (10 secs): %u  id1 (1 min) : %u \n",timer_get_remaining_time(id) / tm_get_ticks_per_second () ,timer_get_remaining_time(id1) / tm_get_ticks_per_second ());

	}

	for ( i = 0; i <=25; i++) {
		task[2] = (char)(97+i);
		task_create (task, 10,0 ,32000 + i,
				&(start_rout2), NULL,
				(void *)(i+1), &rettskid[i]);
	}

	sleep_forever ();
}
Пример #2
0
// Beep and blink every once in a while if battery is low
static void low_battery_check(void)
{
  if (--batt_check_counter == 0) {
    adc_init();
    uint8_t voltage = read_voltage();
    adc_disable();
    set_extra_url_data(voltage);
    if (is_battery_low(voltage)) {
      beep_n_times_and_wait(4);
      // The battery_dead threshold should be set high enough to avoid dropping
      // the AVR into BOD when the RF field is on, because the processor may
      // stop with the field on, which drains the battery rapidly.
      if (is_battery_dead(voltage)) {
        // Turn off the NFC module to minimize power consumption
        module_power_down();
        wdt_disable();
        sleep_forever();
      }
    }
    batt_check_counter = SECS2COUNT(CHECK_BATT_EVERY_NSECS);
  }
}
Пример #3
0
int main(void)
{
  disable_unused_circuits();
  _delay_ms(50);

#ifdef HAS_CHARGER
  reset_on_power_change();
#endif /* HAS_CHARGER */

  /* Shut off right away if battery is very low. Do not power on NFC module */
  adc_init();
  (void)read_voltage();
  // Read one more time in case first reading is corrupted
  uint8_t voltage = read_voltage();
  adc_disable();
  if (is_battery_dead(voltage)) {
    //beep_n_times_and_wait(4);
    //sleep_forever();
  }

  lcd_init();
  print_idle();

  // Initialize and self-test
  module_power_up();
  led_on();
#ifdef WITH_TARGET
  // We may have come out of target power down mode
  //(void)pasori_wake_up();
#endif

  while (!rcs956_reset()) {};

  if (!eeprom_has_station_info()) {
    beep_n_times_and_wait(3);
    sleep_forever();
  }
  if (is_on_external_power()) {
    play_song_and_wait(melody_start_up_external,
                       sizeof(melody_start_up_external) / sizeof(struct note));
  } else {
    play_song_and_wait(melody_start_up_battery,
                       sizeof(melody_start_up_battery) / sizeof(struct note));
  }
  led_off();
  initiator_set_defaults();
  watchdog_start();

  for (;;) {
    watchdog_reset();
    // initiator exits after polling times out (false) or URL is pushed (true)
    if (initiator(PUSH_URL_LABEL)) {
      lcd_puts(0, "PUSH SLEEP");
      play_url_push_success_song_and_wait();
    }
#ifdef WITH_TARGET
    uint8_t loop;
    // Loop here to not skip watchdog timer
    for (loop = 0; loop < TARGET_MODE_RETRY; loop++) {
      watchdog_reset();
      (void)rcs956_reset();
      enum target_res res = target(PUSH_URL_LABEL_ENGLISH);
      if (res == TGT_COMPLETE) {
        led_off();
        play_url_push_success_song_and_wait();
        break;
      } else if (res == TGT_TIMEOUT || res == TGT_ERROR) {
        break;
      } // loop on TGT_RETRY
    }
    led_off();
    (void)rcs956_reset();
    low_battery_check();
#else /* !WITH_TARGET */
    // Check battery level while RF field is still on
    low_battery_check();
    sleep_after_timeout();
#endif /* WITH_TARGET */

    // Reconfigure Felica module if communication timed out,
    // e.g. due to temporary disconnect.
    if (protocol_errno == TIMEOUT) {
      initiator_set_defaults();
      eeprom_increment_usart_fail();
    }
    protocol_errno = SUCCESS;
  }

  /* NOT REACHABLE */
  return 0;
}
Пример #4
0
static VALUE
rb_mutex_sleep_forever(VALUE time)
{
    sleep_forever(GET_THREAD(), 1, 0); /* permit spurious check */
    return Qnil;
}
Пример #5
0
int main(int argc, char* argv[]) {
  std::istream& in = std::cin;
  std::ostream& out = std::cout;

  out.setf(std::ios::unitbuf);

  unsigned time_budget = 60;

  bool debug = false;
  
  // if present, this is the path to a file where the agent is to be serialized.
  boost::optional<std::string> path_to_storage;
  if (argc > 0)
    path_to_storage = std::string(argv[1]);

  Game game;
  MCTSAgent agent(2);
  if (path_to_storage && file_readable(*path_to_storage))
    agent.load_yourself(*path_to_storage);

  boost::optional<Color> agent_color;

  boost::future<void> future_quit;
  boost::future<Move> future_decision;
  boost::future<std::string> future_command;

  auto protocol_debug = [&](std::string s) {
    if (debug)
      out << "#" << s << std::endl;
  };

  auto recv_command = [&]() {
    std::string line;
    if (!std::getline(in, line))
      future_quit = boost::make_ready_future();
    protocol_debug("<xboard> " + line);
    return line;
  };

  auto send_command = [&](std::string line) {
    protocol_debug("<engine> " + line);
    out << line << std::endl;
  };

  auto report_illegal = [&](std::string move) {
    send_command("Illegal move: " + move);
  };

  auto report_error = [&](std::string class_, std::string instance) {
    send_command("Error (" + class_ + "): " + instance);
  };

  auto protocol_assert = [&](bool condition, std::string message) {
    if (!condition)
      report_error("protocol assumption violated", message);
  };

  typedef std::vector<std::string> ARGV;

  std::function<void(std::vector<std::string>)> do_nothing = [&](ARGV argv){};
  std::function<void(std::vector<std::string>)> unsupported = [&](ARGV argv){
    report_error("unsupported", argv[0]);
  };

  std::map<std::string, std::function<void(std::vector<std::string>)> > handlers = {
    {"xboard", do_nothing},
    {"protover", [&](ARGV argv) {
        for (auto feature: features)
          send_command("feature " + feature);
      }},
    // TODO: handle these as soon as we get a use case
    {"accepted", do_nothing},
    {"rejected", unsupported},
    {"new", [&](ARGV argv) {
        protocol_assert(!agent_color, "expected \"new\" only in force mode");
        game = Game();
        agent_color = colors::black;
        agent.set_state(game.current_state());
      }},
    {"variant", unsupported},
    {"quit", [&](ARGV argv) {
        agent.abort_decision();
        agent.stop_pondering();
        future_quit = boost::make_ready_future();
      }},
    {"random", do_nothing},
    {"force", [&](ARGV argv) {
        agent_color = boost::none;
        agent.abort_decision();
        agent.stop_pondering();
      }},
    {"go", [&](ARGV argv) {
        agent_color = game.color_to_move();
        agent.set_state(game.current_state());
        agent.start_pondering();
        future_decision = agent.start_decision(time_budget);
      }},
    {"playother", [&](ARGV argv) {
        protocol_assert(!agent_color, "expected \"playother\" only in force mode");
        agent_color = colors::opposite(game.color_to_move());
      }},
    {"level", unsupported},
    {"st", unsupported},
    {"sd", unsupported},
    {"nps", unsupported},
    {"time", unsupported},
    {"otim", unsupported},
    {"usermove", [&](ARGV argv) {
        protocol_assert(!agent_color || agent_color != game.color_to_move(),
                        "usermove during engine's turn");
        Move move;
        try {
          move = notation::algebraic::parse(argv[1], game.current_state());
        } catch (notation::algebraic::OverdeterminedException& e) {
          report_illegal(argv[1]);
          return;
        } catch (notation::algebraic::UnderdeterminedException& e) {
          report_error("ambiguous move", argv[1]);
          return;
        }
        game.make_move(move);
        if (agent_color) {
          agent.advance_state(move);
          agent.start_pondering();
          future_decision = agent.start_decision(time_budget);
        }
      }},
    {"?", [&](ARGV argv) {
        agent.finalize_decision();
      }},
    {"ping", [&](ARGV argv) {
        if (future_decision.valid())
          future_decision.wait();
        send_command("pong " + argv[1]);
      }},
    {"draw", [&](ARGV argv) {
        protocol_assert(!!agent_color, "\"draw\" when engine not assigned to any color");
        agent.set_state(game.current_state());
        if (agent.accept_draw(*agent_color))
          send_command("offer draw");
      }},
    {"result", [&](ARGV argv) {
        agent.idle();
      }},
    {"setboard", [&](ARGV argv) {
        protocol_assert(!agent_color, "expected \"setboard\" only in force mode");
        std::string fen = boost::algorithm::join(ARGV(argv.begin() + 1, argv.end()), " ");
        game.setboard(fen);
        agent.set_state(game.current_state());
      }},
    {"edit", unsupported},
    {"hint", unsupported},
    {"bk", do_nothing},
    {"undo", [&](ARGV argv) {
        protocol_assert(!agent_color, "expected \"undo\" only in force mode");
        game.undo();
        agent.set_state(game.current_state());
      }},
    {"remove", [&](ARGV argv) {
        protocol_assert(!agent_color || agent_color != game.color_to_move(),
                        "expected \"remove\" only during user's turn");
        game.undo();
        game.undo();
        agent.set_state(game.current_state());
      }},
    {"hard", do_nothing},
    {"easy", do_nothing},
    {"post", do_nothing},
    {"nopost", do_nothing},
    {"analyze", unsupported},
    {"name", do_nothing},
    {"rating", do_nothing},
    {"ics", unsupported},
    {"computer", do_nothing},
    {"pause", [&](ARGV argv) {
        agent.pause();
      }},
    {"resume", [&](ARGV argv) {
        agent.resume();
      }},
    // this could prove to be useful
    {"memory", unsupported},
    {"cores", unsupported},
    {"egtpath", do_nothing},
    {"option", do_nothing},
  };

  auto handle_decision = [&](){
    Move move = future_decision.get();
    send_command("move " + notation::coordinate::format(move));
    game.make_move(move);
    agent.advance_state(move);
    if (path_to_storage)
      agent.save_yourself(*path_to_storage);
    // there has to be a better way to make sure we don't keep redetecting
    // the same future decision value, but i don't know it.  really, the
    // wait_for_any function should wait for any one of them to turn from
    // not-yet-ready to ready, but the manual says that's not how it works.
    // so, replace the ready future with one that will never be ready.
    // i considered keeping a set of futures that need waiting for, and then
    // simply removing processed futures.  but the futures have different
    // value types so we can't store them in a single container unless we use
    // something like boost::variant and then we need a boost::visitor and a
    // boost::welcome_committee and a boost::mayor to ceremonially snip a
    // ribbon with a giant pair of scissors and...  what was the question?
    future_decision = boost::async([]() -> Move {
        sleep_forever();
        throw std::runtime_error("greetings, time traveler!");
      });
  };
  
  auto handle_command = [&]() {
    std::string line = future_command.get();
    std::vector<std::string> argv;
    boost::algorithm::split(argv, line, boost::algorithm::is_any_of("\t "), boost::token_compress_on);
    std::function<void(ARGV)> handler;
    try {
      handler = handlers.at(argv[0]);
    } catch (std::out_of_range& e) {
      protocol_assert(false, "unknown command: " + line);
      goto after_handler;
    }
    handler(argv);

    after_handler:
    future_command = boost::async(recv_command);
  };
  
  future_command = boost::async(recv_command);
  while (true) {
    unsigned short ifuture;
    try {
      ifuture = boost::wait_for_any(future_quit, future_decision, future_command);
    } catch (boost::thread_interrupted&) {
    }

    switch (ifuture) {
    case 0:
      goto quit;
      break;
    case 1:
      handle_decision();
      break;
    case 2:
      handle_command();
      break;
    }

    ifuture = -1;
  }

  quit:
  ;
}