Ejemplo n.º 1
0
void ToneAlarm_PX4::stop_cont_tone() {
    if(_cont_tone_playing == _tone_playing) {
        play_string("");
        _tone_playing = -1;
    }
    _cont_tone_playing = -1;
}
Ejemplo n.º 2
0
/*
  handle a PLAY_TUNE message
*/
void ToneAlarm_PX4::handle_play_tune(mavlink_message_t *msg)
{
    // decode mavlink message
    mavlink_play_tune_t packet;
    
    mavlink_msg_play_tune_decode(msg, &packet);

    play_string(packet.tune);
}
Ejemplo n.º 3
0
// play_tune - play one of the pre-defined tunes
void ToneAlarm_PX4::play_tone(const uint8_t tone_index)
{
    uint32_t tnow_ms = AP_HAL::millis();
    const Tone &tone_requested = _tones[tone_index];

    if(tone_requested.continuous) {
        _cont_tone_playing = tone_index;
    }

    _tone_playing = tone_index;
    _tone_beginning_ms = tnow_ms;

    play_string(tone_requested.str);
}
Ejemplo n.º 4
0
int
tone_alarm_main(int argc, char *argv[])
{
	unsigned tune = 0;

	/* start the driver lazily */
	if (g_dev == nullptr) {
		g_dev = new ToneAlarm;

		if (g_dev == nullptr) {
			errx(1, "couldn't allocate the ToneAlarm driver");
		}

		if (g_dev->init() != OK) {
			delete g_dev;
			errx(1, "ToneAlarm init failed");
		}
	}

	if (argc > 1) {
		const char *argv1 = argv[1];

		if (!strcmp(argv1, "start")) {
			play_tune(TONE_STOP_TUNE);
		}

		if (!strcmp(argv1, "stop")) {
			play_tune(TONE_STOP_TUNE);
		}

		if ((tune = strtol(argv1, nullptr, 10)) != 0) {
			play_tune(tune);
		}

		/* It might be a tune name */
		for (tune = 1; tune < TONE_NUMBER_OF_TUNES; tune++)
			if (!strcmp(g_dev->name(tune), argv1)) {
				play_tune(tune);
			}

		/* If it is a file name then load and play it as a string */
		if (*argv1 == '/') {
			FILE *fd = fopen(argv1, "r");
			int sz;
			char *buffer;

			if (fd == nullptr) {
				errx(1, "couldn't open '%s'", argv1);
			}

			fseek(fd, 0, SEEK_END);
			sz = ftell(fd);
			fseek(fd, 0, SEEK_SET);
			buffer = (char *)malloc(sz + 1);

			if (buffer == nullptr) {
				errx(1, "not enough memory memory");
			}

			fread(buffer, sz, 1, fd);
			/* terminate the string */
			buffer[sz] = 0;
			play_string(buffer, true);
		}

		/* if it looks like a PLAY string... */
		if (strlen(argv1) > 2) {
			if (*argv1 == 'M') {
				play_string(argv1, false);
			}
		}

	}

	errx(1, "unrecognized command, try 'start', 'stop', an alarm number or name, or a file name starting with a '/'");
}
Ejemplo n.º 5
0
int main (void)
{
  uint8_t choice;
  uint8_t current_pos;
  uint16_t time_limit;

  ioinit(); //Setup IO pins and defaults

 BEGIN_GAME:

  //Display fancy LED pattern waiting for two buttons to be pressed
  //Wait for user to begin game
  while(1)
    {
      sbi(LED_RED_PORT, LED_RED);
      cbi(LED_BLUE_PORT, LED_BLUE);
      cbi(LED_GREEN_PORT, LED_GREEN);
      cbi(LED_YELLOW_PORT, LED_YELLOW);
      delay_ms(100);
      if(check_button())
        break;

      cbi(LED_RED_PORT, LED_RED);
      sbi(LED_BLUE_PORT, LED_BLUE);
      cbi(LED_GREEN_PORT, LED_GREEN);
      cbi(LED_YELLOW_PORT, LED_YELLOW);
      delay_ms(100);
      if(check_button())
        break;

      cbi(LED_RED_PORT, LED_RED);
      cbi(LED_BLUE_PORT, LED_BLUE);
      sbi(LED_GREEN_PORT, LED_GREEN);
      cbi(LED_YELLOW_PORT, LED_YELLOW);
      delay_ms(100);
      if(check_button())
        break;

      cbi(LED_RED_PORT, LED_RED);
      cbi(LED_BLUE_PORT, LED_BLUE);
      cbi(LED_GREEN_PORT, LED_GREEN);
      sbi(LED_YELLOW_PORT, LED_YELLOW);
      delay_ms(100);
      if(check_button())
        break;
    }

  //Indicate the start of game play
  sbi(LED_RED_PORT, LED_RED);
  sbi(LED_BLUE_PORT, LED_BLUE);
  sbi(LED_GREEN_PORT, LED_GREEN);
  sbi(LED_YELLOW_PORT, LED_YELLOW);
  delay_ms(1000);
  cbi(LED_RED_PORT, LED_RED);
  cbi(LED_BLUE_PORT, LED_BLUE);
  cbi(LED_GREEN_PORT, LED_GREEN);
  cbi(LED_YELLOW_PORT, LED_YELLOW);
  delay_ms(250);

  game_string_position = 0; //Start new game

  while(1)
    {
      add_to_string(); //Add the first button to the string
      play_string(); //Play the current contents of the game string back for the player

      //Wait for user to input buttons until they mess up, reach the end of the current string, or time out
      for(current_pos = 0 ; current_pos < game_string_position ; current_pos++)
        {
          //Button scanning
          //=======================================
          time_limit = 0; //Run timer while we wait for the user to push a button


          int button_state;
          int last_button = check_button();
          while(1)
            {
              button_state = check_button();
              if(button_state != 0 && last_button == button_state)
                break;
              last_button = button_state;

              delay_ms(5);

              time_limit++; //3 second time limit
              if (time_limit > 3000)
                {
                  play_loser(); //Play anoying loser tones
                  delay_ms(1000);
                  play_string();
                  goto BEGIN_GAME;
                }
            }

          // Button debounce.
          delay_ms(10);

          //Wait for user to release button
          choice = 0;
          while(choice == 0)
            {
              int button = check_button();
              if(button == 1) choice = '3'; //Lower left button "Blue"
              if(button == 2) choice = '4'; //Lower right "Yellow"
              if(button == 4) choice = '1'; //Upper left "Red"
              if(button == 8) choice = '2'; //Upper right "Green"

              toner(choice, 150); //Fire the button and play the button tone
            }

          // Debounce
          delay_ms(10);

          //=======================================
          if (choice != game_string[current_pos])
            {
              play_loser(); //Play annoying loser tones
              delay_ms(1000);
              play_string();
              goto BEGIN_GAME;
            }

        }//End user input loop

      //If user reaches the game length of X, they win!
      if (current_pos == 13)
        {
          play_winner(); //Play winner tones
          goto BEGIN_GAME;
        }

      //Otherwise, we need to wait just a hair before we play back the last string
      delay_ms(1000);
    }

  return(0);
}
Ejemplo n.º 6
0
/*
 * @brief  Step through the main stages in the code.
 * @return Void
 */
void mode_5_main(void) {
    switch(current_state) {

        case INITIAL:
/*            if (!done_rd_dict) {
                log_msg("Reading dictionary file...");
                play_direction(MP3_PLEASE_WAIT)
                init_read_dict((unsigned char *)"wordsEn.txt");
                while (!done_rd_dict)
                    read_dict_file();
            } */
            play_welcome();
            current_state = REQUEST_QUESTION;
            break;



        case REQUEST_QUESTION:
        if (got_input) {
            got_input = false;
            current_state = GENERATE_QUESTION;
        }
        break;



        case GENERATE_QUESTION:
            if (cell == 0) { // if return was entered twice
                mode_5_chosen_word[input_word_index] = '\0';

//                if (bin_srch_dict((unsigned char *)mode_5_chosen_word)) { // valid word; switch to player 2
                    // @TODO "valid word, please hand device to player 2 and press enter when ready"
//                    play_mode_audio(MP3_VALID_WORD_PASS_DEVICE);
                    input_word_index = 0;

                    play_feedback(MP3_YOUR_WORD_IS);
                    play_string(mode_5_chosen_word, strlen(mode_5_chosen_word));
                    play_feedback(MP3_PASS_DEVICE_PRESS_ENTER);
                    current_state = SWITCH_USERS;

   /*             }
                else { // invalid word; clear variables and try again
                    play_feedback(MP3_WORD_NOT_FOUND);
                    mode_5_reset();
                    current_state = REQUEST_QUESTION;
                } */
            }
            
            else if (mode_5_valid_letter(cell)) { // letter valid; word not complete
                log_msg("Letter %c entered.", entered_letter);
                char letter[2] = {entered_letter, '\0'};
                play_lang_audio(letter);

                // reset because too many letters were input
                if (input_word_index == MAX_WORD_LENGTH) {
                    log_msg("Too many letters inputted!");
                    play_feedback(MP3_TOO_LONG);
                    mode_5_reset();
                    current_state = REQUEST_QUESTION;
                    break;
                }

                mode_5_chosen_word[input_word_index] = entered_letter;  // add letter to chosen_word
                input_word_index++;
                current_state = REQUEST_QUESTION;
            }
            else { // invalid letter
                play_feedback(MP3_INVALID_PATTERN);
                current_state = REQUEST_QUESTION;
            }
            break;



        case SWITCH_USERS:
            if (got_input) {
                got_input = false;
                current_state = PROMPT;
            }
            break;


        case PROMPT:
            log_msg("Entering ask for guess state.");
            play_direction(MP3_PLAYER_2);
            play_feedback(MP3_YOUR_WORD_IS_NOW);
            play_string(input_word, strlen(mode_5_chosen_word));
            play_direction(MP3_GUESS_A_LETTER);
            current_state = GET_INPUT;
            break;




        case GET_INPUT:
            if (got_input) {
                got_input = false;
                current_state = PROCESS_ANSWER;
            }
            break;




        case PROCESS_ANSWER:
            log_msg("Entering checkans state.");
            if (cell == 0) // nothing entered: repeat word
                current_state = GAME_OVER;

            else if (mode_5_valid_letter(cell)) { // valid letter: read aloud
                char buff[7];
                sprintf(buff, "%c", entered_letter);
                play_lang_audio(buff);  //@todo fix this
                current_state = CHECK_ANSWER;
            }
            else {
                play_feedback(MP3_INVALID_PATTERN);
                mistakes++;
                current_state = GAME_OVER;
            }
            break;





        case CHECK_ANSWER:
            log_msg("Entering check match state.");
            if (mode_5_place_letter()) // letters is in word; fn places it
                play_feedback(MP3_YES);
            else {
                play_feedback(MP3_NO);
                if (!mode_5_is_past_mistake(entered_letter)) {
                    mistake_pool[mistakes] = entered_letter;
                    mistakes++;
                }
                else
                    play_feedback(MP3_YOU_HAVE_MADE_THE_SAME_MISTAKE);
            }
            current_state = GAME_OVER;
            break;





        case GAME_OVER:
            log_msg("Entering evaluate game state.");
            if (!strncmp(input_word, mode_5_chosen_word, strlen(mode_5_chosen_word))) {
                play_feedback(MP3_YOU_HAVE_GUESSED_THE_WORD);
                play_tada();
                current_state = INITIAL;
            }
            else if (mistakes == max_mistakes) {
                play_feedback(MP3_7_MISTAKES_YOU_MISSED);
                play_string(mode_5_chosen_word, strlen(mode_5_chosen_word));
                current_state = INITIAL;
            }
            else {
                if (mistakes > 0) {
                    play_feedback(MP3_YOU_HAVE);
                    play_number(max_mistakes - mistakes);
                    if (max_mistakes - mistakes == 1)
                        play_feedback(MP3_MISTAKE_REMAINING);
                    else
                        play_feedback(MP3_MISTAKES_REMAINING);
                }
                current_state = PROMPT;
            }
            break;

        default:
            log_msg("Invalid state_t %d", current_state);
            quit_mode();
            break;

    }
}