/** @file message.c
 *  @author Tyler Cooper,& Blake tolmie
 *  @date 14 October 2024
 *  @brief takes a message variable and displays it till button pushed or signal receive
 */
// end and start screen
#include "system.h"
#include "button.h"
#include "pacer.h"
#include "tinygl.h"
#include "../fonts/font5x7_1.h"
#include "timer.h"
#include "ir_uart.h"

#define PACER_RATE 500
#define MESSAGE_RATE 10
/* Welcome message runs until a button is pushed */
void message(const char *message_text, bool scroll, uint16_t duration,uint8_t* state,uint16_t* stager)
{
    if (*stager == 0){
        tinygl_init (PACER_RATE);             // speed at which it scrolls text
        tinygl_text_speed_set (MESSAGE_RATE);  // speed of the displayed message and is given in characters per 10 seconds.
        if (scroll) {
            tinygl_text_mode_set (TINYGL_TEXT_MODE_SCROLL);
        } else {
            tinygl_text_mode_set (TINYGL_TEXT_MODE_STEP);
        }
        tinygl_text(message_text);
        pacer_init (PACER_RATE);
        ir_uart_init();
    }
    
    pacer_wait();
    button_update();
    tinygl_update();

    *stager += 1;
    
    if (*stager >= duration) {     // Check if the total duration has elapsed
        *state = 2;
        *stager = 0;
    }

    if (button_push_event_p(BUTTON1)) {
        ir_uart_putc('b');     // Check if BUTTON1 is pushed to exit the message loop
        *stager = 0;
        *state = 2;
    } else if (ir_uart_read_ready_p()){
        char received_character = ir_uart_getc ();
        if(received_character == 'b') {
            *stager = 0;
            *state = 2;
        }
    }
}
        