Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
7.1 kB
1
Indexable
Never
/*
 * File:   main.c
 * Author: altug
 *
 * Created on May 23, 2023, 4:40 PM
 */

#include "the3.h"
#include "lcd.h"

#define teamA 0
#define teamB 1

//special char indexes
#define TEAM_A 0
#define TEAM_B 1
#define TEAM_A_SELECTED 2
#define TEAM_B_SELECTED 3
#define FRISBEE_REAL 4
#define FRISBEE_TARGET 5
#define TEAM_A_WITH_FRISBEE 6
#define TEAM_B_WITH_FRISBEE 7



byte char_ind = 0;
byte selected_player = 0;
byte rb_flag = 0x00;
byte changes;
typedef struct coordinate {
    byte x;
    byte y;
}coordinate;

typedef struct Player{
    byte id;
    byte selected_shape;
    byte default_shape;
    byte shape;
    coordinate coor;
    byte team;
}Player;

typedef struct Frisbee{
    coordinate coor;
    byte shape;
}Frisbee;

typedef struct Game{
    
}Game;

void drawToLCD(Player *player, Frisbee *frisbee);


Player players[4];
Frisbee targetFrisbee;
Frisbee realFrisbee;

void move_up(){
    Player *player = &players[selected_player];
    LCDGoto(player->coor.x, player->coor.y );
    LCDStr(" ");
    player->coor.y -= 1;
    drawToLCD(player, NULL);
}


void move_right(){
    Player *player = &players[selected_player];
    LCDGoto(player->coor.x, player->coor.y );
    LCDStr(" ");
    player->coor.x += 1;
    drawToLCD(player, NULL);
}
void move_down(){
    Player *player = &players[selected_player];
    LCDGoto(player->coor.x, player->coor.y );
    LCDStr(" ");
    player->coor.y += 1;
    drawToLCD(player, NULL);
}
void move_left(){
    Player *player = &players[selected_player];
    LCDGoto(player->coor.x, player->coor.y );
    LCDStr(" ");
    player->coor.x -= 1;
    drawToLCD(player, NULL);
}

void __interrupt() ISR()
{
    changes = PORTB;
    if (INTCONbits.TMR0IF)  // Check if Timer0 overflow interrupt flag is set
    {
        rb_flag = 0x00;
        // Your code to handle the Timer0 overflow interrupt
         TMR0 = 3500;
         //LCDGoto(players[0].coor.x, players[0].coor.y);
         //LCDDat(' ');
         //players[0].coor.x += 1;
         //drawToLCD(&players[0], NULL);

        // Clear the Timer0 overflow interrupt flag
        INTCONbits.TMR0IF = 0;
    }
    
    if (INTCONbits.RBIF) //portb<7:4>
    {      
        changes ^= 0xFF;
        if ((changes >>= 4) == 1){
            move_up();
            
        }
        else if ((changes >>= 1) == 1){
            move_right();
           
        }
        else if ((changes >>= 1 )== 1){
            move_down();
        
        }
        else if ((changes >>= 1 )== 1){
            move_left();
        }
        
        // Your code to handle the RB0 interrupt
        PORTA = PORTB;
        // Clear the RB0 interrupt flag
        INTCONbits.RBIF = 0;
        rb_flag++;
    }
    else if (INTCONbits.INT0IF){
        LCDStr("a");
        INTCONbits.INT0IF = 0;
    }
    else if (INTCON3bits.INT1F){
        if(PORTBbits.RB1 == 1){
        Player* p_player = &players[selected_player];
        p_player->shape = p_player->default_shape;
        selected_player = (++selected_player) % 4;
        Player *a_player = &players[selected_player];
        a_player->shape = a_player->selected_shape;
        drawToLCD(p_player, NULL);
        drawToLCD(a_player, NULL);
        }
        INTCON3bits.INT1F = 0;
    }
    
}

void initializePlayers(Player *players){
    players[0].id = 0;
    players[0].shape = TEAM_A_SELECTED;
    players[0].selected_shape = TEAM_A_SELECTED;
    players[0].default_shape = TEAM_A;
    players[0].coor.x = 3;
    players[0].coor.y = 2;
    players[0].team = teamA;

    players[1].id = 1;
    players[1].shape = TEAM_A;
    players[1].selected_shape = TEAM_A_SELECTED;
    players[1].default_shape = TEAM_A;
    players[1].coor.x = 3;
    players[1].coor.y = 3;
    players[1].team = teamA;

    players[2].id = 2;
    players[2].shape = TEAM_B;
    players[2].selected_shape = TEAM_B_SELECTED;
    players[2].default_shape = TEAM_B;
    players[2].coor.x = 14;
    players[2].coor.y = 2;
    players[2].team = teamB;

    players[3].id = 3;
    players[3].shape = TEAM_B;
    players[3].selected_shape = TEAM_B_SELECTED;
    players[3].default_shape = TEAM_B;
    players[3].coor.x = 14;
    players[3].coor.y = 3;
    players[3].team = teamB;
    return; 
}

void initializeFrisbee(Frisbee *real, Frisbee *target){
    real->coor.x = 9;
    real->coor.y = 2;
    real->shape = FRISBEE_REAL;
    
    target->coor.x = 0;
    target->coor.y = 0;
    target->shape = FRISBEE_TARGET;
    
}

void initSpecialCharacters(){
    LCDAddSpecialCharacter(TEAM_A, &teamA_player);
    LCDAddSpecialCharacter(TEAM_B, &teamB_player);
    LCDAddSpecialCharacter(TEAM_A_SELECTED, &selected_teamA_player);
    LCDAddSpecialCharacter(TEAM_B_SELECTED, &selected_teamB_player);
    LCDAddSpecialCharacter(FRISBEE_REAL, &frisbee);
    LCDAddSpecialCharacter(FRISBEE_TARGET, &frisbee_target);
    LCDAddSpecialCharacter(TEAM_A_WITH_FRISBEE, &selected_teamA_player_with_frisbee);
    LCDAddSpecialCharacter(TEAM_B_WITH_FRISBEE, &selected_teamB_player_with_frisbee);
    
}

// either draws player or frisbee to lcd
void drawToLCD(Player *player, Frisbee *frisbee){
   
   
    if( player ){
        LCDGoto(player->coor.x,player->coor.y);
        LCDDat(player->shape);
        
    }
    else if (frisbee){
        LCDGoto(frisbee->coor.x, frisbee->coor.y);
        LCDDat(frisbee->shape);
    }
    return;

     
}

void InitInterrupts(){


    
    

    
    byte x = PORTB;
    PORTB = 0xFF;
    TRISB = 0xFF;
    INTCONbits.GIE = 1;
    //PORTB <7:4>
    ADCON1 = 0x0F;
    x = PORTB;
    INTCONbits.RBIF = 0; //clear flag
    PORTB = 0x00;
    INTCONbits.RBIE = 1; //enable
    //RB0
    INTCONbits.INT0IF = 0; //clear flag
    INTCONbits.INT0E = 1; //enable
    INTCON2bits.INTEDG0 = 1; //rising edge
    INTCON2bits.RBPU = 1;
    //RB1
    INTCON3bits.INT1F = 0; //clear flag
    INTCON3bits.INT1E = 1; //enable
    INTCON2bits.INTEDG1 = 0; //rising edge
    
    T0CONbits.TMR0ON = 0;   // Turn off Timer0 initially
    T0CONbits.T08BIT = 0;   // Configure Timer0 as a 16-bit timer
    T0CONbits.T0CS = 0;     // Select internal instruction cycle clock (Fosc/4) as the clock source
    T0CONbits.PSA = 0;      // Assign prescaler to Timer0
    T0CONbits.T0PS2 = 1;    // Set the prescaler value to 1:64
    T0CONbits.T0PS1 = 0;
    T0CONbits.T0PS0 = 1;

    // Calculate the Timer0 initial value based on desired time period
    // For a 400ms interrupt with a 1:64 prescaler and 10 MHz clock frequency, the initial value is:
    TMR0 = 6800;

    // Enable Timer0 overflow interrupt
    INTCONbits.TMR0IE = 1;  // Enable Timer0 overflow interrupt

    // Configure and enable global interrupts

    // Start Timer0
    T0CONbits.TMR0ON = 1;   // Turn on Timer0

    
    
}

void main(void) {

    initializePlayers(players);
    initializeFrisbee(&realFrisbee, &targetFrisbee);
    InitLCD();
    InitInterrupts();
    initSpecialCharacters();

    drawToLCD(&players[0], NULL );
    drawToLCD(&players[1], NULL );
    drawToLCD(&players[2], NULL );
    drawToLCD(&players[3], NULL );
    drawToLCD(NULL, &realFrisbee );

    
    while (1);
    return;
}