Question: Hi, I am having trouble with a C program. It is about making a tic-tac-toe game in a specialized board with a ps2 control stick
Hi,
I am having trouble with a C program. It is about making a tic-tac-toe game in a specialized board with a ps2 control stick and 4 pushbuttons (up down left right) connected to a Tiva C Launchpad. To make the program you have to use the protocols in the api file to output the game to the board. So far we are using the drawline to make a grid function and a bitmap for an X and an O, which are labeled buckybitmap and bucky2 in the main folder. The thing we are mainly confused about is how best to implement code that places the x or o bitmap permanently, then switches the toggled bitmap back and forth between x and o, and also checks the positions to see which squares on the grid are filled by which bitmaps, and tell the user when one player has won, or when a stalemate has been reached. The program instruction file and the main file are given below. Please don't give me any of your previous works because this is being done in a specialized board and thus it is a bit different than the usual C program. I greatly appreciate any assistance.
Thanks!
Here is the instruction file:
// Redistribution and use in source or binary form, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions in source form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in
// the documentation and/or other materials provided with the distribution.
#ifndef __ECE210_API_H__
#define __ECE210_API_H__
#include
#include
#include
#include
#include "TM4C123.h"
extern volatile bool AlertTouch;
extern volatile bool AlertButtons;
extern void GPIOF_Handler(void);
extern volatile bool ALERT_ADC;
extern volatile bool ALERT_AUDIO_COMP;
#if !defined(TERMINAL_ALIGN_LEFT)
#define TERMINAL_ALIGN_LEFT 0
#define TERMINAL_ALIGN_RIGHT 1
#define TERMINAL_ALIGN_CENTER 2
#endif
#if !defined (LCD_COLOR_WHITE)
#define LCD_COLOR_WHITE 0xFFFF
#define LCD_COLOR_BLACK 0x0000
#define LCD_COLOR_RED 0xF800
#define LCD_COLOR_GREEN 0x07E0
#define LCD_COLOR_GREEN2 0xB723
#define LCD_COLOR_BLUE 0x001F
#define LCD_COLOR_BLUE2 0x051D
#define LCD_COLOR_YELLOW 0xFFE0
#define LCD_COLOR_ORANGE 0xFBE4
#define LCD_COLOR_CYAN 0x07FF
#define LCD_COLOR_MAGENTA 0xA254
#define LCD_COLOR_GRAY 0x7BEF
#define LCD_COLOR_BROWN 0xBBCA
#endif
#define PS2_CENTER 0x00
#define PS2_UP 0x01
#define PS2_DOWN 0x02
#define PS2_LEFT 0x03
#define PS2_RIGHT 0x04
#define PS2_THRESHOLD_X_HI 0xC00
#define PS2_THRESHOLD_X_LO 0x400
#define PS2_THRESHOLD_Y_HI 0xC00
#define PS2_THRESHOLD_Y_LO 0x400
/********************************************************************************
* Summary:
* Reads the values on the 4 bit switches (S600).
*
* Returns
* An Unsigned 8-bit value where the upper four bits are always 0, and the
* lower 4 bits represent which bit switches are turned on (1) of turned off (0)
*******************************************************************************/
uint8_t ece210_switches_read(void);
/********************************************************************************
* Summary:
* Reads the values on the 4 directional push buttons.
*
* Returns
* An Unsigned 8-bit value.
* Bit 7 0
* Bit 6 0
* Bit 5 0
* Bit 4 0
* Bit 3 1 for RIGHT Pressed. 0 for RIGHT NOT Pressed.
* Bit 2 1 for LEFT Pressed. 0 for LEFT NOT Pressed.
* Bit 1 1 for DOWN Pressed. 0 for DOWN NOT Pressed.
* Bit 0 1 for UP Pressed. 0 for UP NOT Pressed.
*******************************************************************************/
uint8_t ece210_buttons_read(void);
/********************************************************************************
* Summary:
* Writes the 8-bit parameter to the 8 RED leds.
*
* Returns
* Nothing
*******************************************************************************/
void ece210_red_leds_write(uint8_t leds);
/********************************************************************************
* Summary:
* Writes the 8-bit parameter to the rgb led on the Tiva Launchpad.
*
* Returns
* Nothing
*******************************************************************************/
void ece210_tiva_rgb_write(uint8_t leds);
/********************************************************************************
* Summary:
* Sets the color of the selected WS2812B LED
*
* Returns:
* Nothing
*******************************************************************************/
void ece210_ws2812b_write(uint8_t led_num, uint8_t red, uint8_t green, uint8_t blue);
/*******************************************************************************
* Function Name: ece210_lcd_add_msg
********************************************************************************
* Summary:
* Prints a message to the lowest line of the LCD screen. Any previous
* messages are scrolled up. The LCD can display up to 16 lines of data.
*
* Return:
* Nothing
*
* Examples
* ece210_lcd_add_msg("Hello WORLD", TERMINAL_ALIGN_RIGHT, LCD_COLOR_YELLOW);
* ece210_lcd_add_msg("Goodbye.", TERMINAL_ALIGN_LEFT, LCD_COLOR_RED);
* ece210_lcd_add_msg("Go Bucky!.", TERMINAL_ALIGN_CENTER, LCD_COLOR_RED);
*******************************************************************************/
void ece210_lcd_add_msg( char *msg, uint8_t alignment, uint32_t color );
/*******************************************************************************
* Function Name: ece210_wait_uSec
********************************************************************************
* Summary:
* Busy waits for a set number of mS to wait.
*
* Return:
* Nothing
*
*******************************************************************************/
void ece210_wait_mSec( uint32_t mSeconds );
/********************************************************************************
* Summary:
* Returns the raw value of the x direction of the PS2 joystick
*
* Returns
* Usigned Int 16 bit from 0 to 4095. 0 Down, 4095 is Up.
*******************************************************************************/
uint16_t ece210_ps2_read_x(void);
/********************************************************************************
* Summary:
* Returns the raw value of the y direction of the PS2 joystick
*
* Returns:
* Usigned Int 16 bit from 0 to 4095. 0 Right, 4095 is Left.
*******************************************************************************/
uint16_t ece210_ps2_read_y(void);
/********************************************************************************
* Summary:
* Returns an unsigned 8 bit integer which stores the bit of the direction detected.
* A bit is high for PS2_UP, PS2_DOWN, PS2_LEFT, PS2_RIGHT or PS2_CENTER.
* Returns
* An unsigned 8-bit value.
* Bit 7 0
* Bit 6 0
* Bit 5 0
* Bit 4 0
* Bit 3 1 for PS2_RIGHT. 0 for PS2_RIGHT NOT the direction.
* Bit 2 1 for PS2_LEFT. 0 for PS2_RIGHT NOT the direction.
* Bit 1 1 for PS2_DOWN. 0 for PS2_RIGHT NOT the direction.
* Bit 0 1 for PS2_UP. 0 for PS2_CENTER, RIGHT, LEFT or DOWN.
*
*******************************************************************************/
uint8_t ece210_ps2_read_position(void);
/********************************************************************************
* Summary:
* Sets a single pixel to a given color
*
* Returns
* Nothing
*******************************************************************************/
void ece210_draw_pixel(uint16_t x, uint16_t y, uint16_t color);
/********************************************************************************
* Summary:
* Sets a single pixel to a given color
*
* Returns
* Nothing
*******************************************************************************/
void ece210_lcd_draw_pixel(uint16_t x, uint16_t y, uint16_t color);
/*******************************************************************************
* Function Name: ece210_lcd_draw_rectangle
********************************************************************************
* Summary:
* Draws a solid rectangle witht he supplied color. The position of the
* rectangle using the lower right hand corner of the rectangle.
*
* Return:
* Nothing
*******************************************************************************/
void ece210_lcd_draw_rectangle (uint16_t x_start, uint16_t x_len, uint16_t y_start, uint16_t y_len, uint16_t color);
/*******************************************************************************
* Function Name: ece210_lcd_draw_image
********************************************************************************
* Summary: Prints an image where the lower right corner of the image is
* positioned at the coordinates x_start, y_start.
* Returns:
* Nothing
*******************************************************************************/
void ece210_lcd_draw_image(
uint16_t x_start,
uint16_t image_width_bits,
uint16_t y_start,
uint16_t image_height_pixels,
const uint8_t *image,
uint16_t fColor,
uint16_t bColor
);
/********************************************************************************
* Summary:
* Draws a 1-pixel wide line
*
* Returns
* Nothing
*******************************************************************************/
void ece210_lcd_draw_line(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t color);
/*******************************************************************************
* Function Name: ece210_lcd_draw_filled_circle
********************************************************************************
* Summary: Draws a circle on the LCD centered at x0, y0 with radius in pixels
* Returns:
* Nothing
*******************************************************************************/
void ece210_lcd_draw_circle(int16_t x0, int16_t y0, int16_t radius, uint32_t color);
/*******************************************************************************
* Function Name: ece210_lcd_print_string
********************************************************************************
* Summary: Prints a string at the specificed location.
* Returns:
* Nothing
*******************************************************************************/
void ece210_lcd_print_string(char* string, uint16_t x_pos, uint16_t y_pos, uint16_t fg_color, uint16_t bg_color);
/********************************************************************************
* Summary:
* Initializes system resources so that data from the microphone can be
* examined.
*
* Returns
bool If TRUE the audio card wil work as defined. FALSE the audio card will not work.
*******************************************************************************/
bool ece210_audio_init(uint32_t micro_seconds);
/********************************************************************************
* Summary:
* Returns the raw reading of the audio signal
*
* Returns
Unsigned Int from 0 to 4096.
*******************************************************************************/
uint16_t ece210_audio_read(void);
/********************************************************************************
* Summary:
* Sets the lower threshold of the audio comparator.
*
* Returns
Nothing
*******************************************************************************/
void ece210_audio_set_comparator_threshold(uint8_t threshold);
/********************************************************************************
* Summary:
* Sets the headphone data output
*
* Returns
* Nothing
*******************************************************************************/
void ece210_audio_headphone_out(uint8_t data);
/********************************************************************************
* Summary:
* Indicates if the Microphone data exceedes the current amplitue threshold.
*
* Returns
* bool TRUE mic signal s above the threshold. FALSE the mic signal is below the threshold.
*******************************************************************************/
bool ece210_audio_comparator_above_threshold(void);
/********************************************************************************
* Summary:
* Configures the wirless radio's local and remote IDs. Each ID needs to be
* 1 byte. local_id is the id of the your transceiver. Remote is the id of the
* tranceiver you are talking to.
* Returns
* Nothing
*******************************************************************************/
void ece210_wireless_init(uint8_t local_id, uint8_t remote_id);
/********************************************************************************
* Summary:
* Sends a 32-bit value to the remote board.
*
* Returns
* Bool TRUE transmission went out. FALSE Never trasnmitted.
*******************************************************************************/
bool ece210_wireless_send(uint32_t data);
/********************************************************************************
* Summary:
* Determines if a new 32-bit value has been received from the remote board.
*
* Returns
* Bool TRUE if a new 32 bit value is available. FALSE no new value.
*******************************************************************************/
bool ece210_wireless_data_avaiable(void);
/********************************************************************************
* Summary:
* Returns a 32-bit value from the remote board.
*
* Returns
* Unsigned Int 32 bits.
*******************************************************************************/
uint32_t ece210_wireless_get(void);
/*******************************************************************************
* Function Name: ece210_platform_initialize_board
********************************************************************************
* Summary:
* Configures the hardware resources on the ECE353/210 board
*
* Return:
* Nothing
*
*******************************************************************************/
void ece210_initialize_board(void);
//*****************************************************************************
//*****************************************************************************
void DisableInterrupts(void);
//*****************************************************************************
//*****************************************************************************
void EnableInterrupts(void);
#endif
*********Here is what we have done till now and this is the main file where the work needs to be done:
//*****************************************************************************
// main.c
// Author: hllo@mich.edu
// Modified: Yollo
//*****************************************************************************
#include "lab_buttons.h"
#include "Invader.black.h"
#include "BuckyBadger.black.h"
#include "BuckyBadger2.black.h"
// important information about the image size in here.
/******************************************************************************
* Global Constants and Variables
*****************************************************************************/
#define MOVE_PIXELS 20 // Number of Pixels to move each time.
#define LCD_SIZE_X 240 // X Size of LCD screen in pixels
#define LCD_SIZE_Y 320 // Y Size of LCD screen in pixels
#define LCD_MAX_DRAW_X LCD_SIZE_X-1 // 0 to LCD_MAX_DRAW_X is = LCD_SIZE_X
#define LCD_MAX_DRAW_Y LCD_SIZE_Y-1 // 0 to LCD_MAX_DRAW_Y is = LCD_SIZE_Y
#define LCD_HALF_SIZE_X LCD_SIZE_X/2 // X center of screen in pixels
#define LCD_HALF_SIZE_Y LCD_SIZE_Y/2 // Y center of screen in pixels
#define LCD_INIT_X LCD_HALF_SIZE_X - BUCKY_WIDTH_PXL/2 // Start image in the center of the screen X
#define LCD_INIT_Y LCD_HALF_SIZE_Y - BUCKY_HEIGHT_PXL/2 // Start image in the center of the screen Y
//*****************************************************************************
//*****************************************************************************
int
main(void)
{
uint16_t x_pos = 88; // x_pos holds the x position of the right corner of the image
uint16_t y_pos = 140; // y_pos holds the y position of the lower corner of the image
//uint16_t x_pos1 = 15; // x_pos holds the x position of the right corner of the image
//uint16_t y_pos1= 50; // y_pos holds the y position of the lower corner of the image
uint16_t width_pixels = BUCKY_WIDTH_PXL; // width of the imsge in pixels
uint16_t height_pixels = BUCKY_HEIGHT_PXL; // height of the image in pixels
uint8_t direction;
const uint8_t *p_image = bucky_bitmap;
ece210_initialize_board();
ece210_lcd_add_msg("Tic-Tac-Tons of Fun, press any button to begin ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
// ece210_lcd_draw_image(x_pos1,width_pixels, y_pos1,height_pixels, invader_bitmap,LCD_COLOR_RED, LCD_COLOR_BLACK);
while(1)
{
if(AlertButtons){
AlertButtons = false;
if(btn_right_pressed() || btn_left_pressed()|| btn_up_pressed()|| btn_down_pressed()){
//ece210_lcd_draw_rectangle (x_pos-1,width_pixels, y_pos+220, height_pixels-150, LCD_COLOR_BLACK);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
//(x_pos,width_pixels, y_pos, height_pixels, LCD_COLOR_BLACK);
ece210_lcd_add_msg("Use control stick to move ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
ece210_lcd_add_msg(" Place Piece? LEFT:Y RIGHT:N ",TERMINAL_ALIGN_CENTER,LCD_COLOR_YELLOW);
break;
}
}
}
ece210_lcd_draw_rectangle(1, 239, 122, 10, LCD_COLOR_RED);
ece210_lcd_draw_rectangle(1, 239, 212, 10, LCD_COLOR_RED);
ece210_lcd_draw_rectangle(75, 10, 50, 245, LCD_COLOR_RED);
ece210_lcd_draw_rectangle(155, 10, 50, 245, LCD_COLOR_RED);
while(1)
{
width_pixels=BUCKY_WIDTH_PXL;
height_pixels = BUCKY_HEIGHT_PXL;
//ece210_lcd_draw_rectangle (x_pos,width_pixels, y_pos, height_pixels, LCD_COLOR_BLACK);
ece210_lcd_draw_image(x_pos,width_pixels, y_pos,height_pixels, p_image,LCD_COLOR_RED, LCD_COLOR_BLACK);
//ece210_lcd_draw_image(x_pos,width_pixels, y_pos,height_pixels, p_image,LCD_COLOR_RED, LCD_COLOR_BLACK);
p_image = bucky_bitmap;
width_pixels=BUCKY_WIDTH_PXL;
height_pixels = BUCKY_HEIGHT_PXL;
if(AlertButtons){
AlertButtons=false;
if(btn_left_pressed()){
x_pos=(x_pos-40);
ece210_lcd_draw_image(x_pos,width_pixels, y_pos,height_pixels, bucky_bitmap,LCD_COLOR_RED, LCD_COLOR_BLACK);
}}
// ece210_lcd_draw_rectangle (x_pos,width_pixels, y_pos, height_pixels, LCD_COLOR_BLACK);
// if(AlertButtons){
// AlertButtons = false;
// if(btn_right_pressed() || btn_left_pressed()|| btn_up_pressed()|| btn_down_pressed()){
break;
}
// draws the whole image
/* while(1)
{
direction = ece210_ps2_read_position(); // Joystick function to read digital value of x or Y pointing.
if( AlertButtons){
AlertButtons = false;
// if(btn_right_pressed()){
// ece210_lcd_draw_rectangle (x_pos,width_pixels, y_pos, height_pixels, LCD_COLOR_BLACK);
p_image = invader_bitmap;
width_pixels= INVADER_WIDTH_PXL;
height_pixels= INVADER_HEIGHT_PXL;}
if(btn_left_pressed()){
ece210_lcd_draw_rectangle (x_pos,width_pixels, y_pos, height_pixels, LCD_COLOR_BLACK);
p_image = bucky_bitmap;
width_pixels=BUCKY_WIDTH_PXL;
height_pixels = BUCKY_HEIGHT_PXL;}
if(btn_up_pressed()){
ece210_lcd_draw_rectangle (x_pos,width_pixels, y_pos, height_pixels, LCD_COLOR_BLACK);
p_image = bucky_2_bitmap;
width_pixels = BUCKY2_WIDTH_PXL;
height_pixels = BUCKY2_HEIGHT_PXL;}
} */ // One of Up, Down, Left or Right value returned.
direction = ece210_ps2_read_position(); // Joystick function to read digital value of x or Y pointing.
switch (direction) // based on the joystick direction move the image.
{
case PS2_RIGHT: // Joystick pointing left.ll
{
if((x_pos+width_pixels) < LCD_SIZE_X - MOVE_PIXELS) // if not moving past the left edge
{
ece210_lcd_draw_rectangle (x_pos, MOVE_PIXELS, y_pos, height_pixels, LCD_COLOR_BLACK); // erase part of image
x_pos = x_pos + MOVE_PIXELS; // that won't be redrawn
}
break;
}
case PS2_LEFT:
{
if(x_pos > MOVE_PIXELS-1) // if not moving past the right edge
{
ece210_lcd_draw_rectangle (x_pos+width_pixels-MOVE_PIXELS, MOVE_PIXELS, y_pos, height_pixels, LCD_COLOR_BLACK);
x_pos = x_pos - MOVE_PIXELS;
}
break;
}
case PS2_DOWN:
{
if((y_pos+height_pixels) < LCD_SIZE_Y-MOVE_PIXELS) // if not moving past the top edge
{
ece210_lcd_draw_rectangle (x_pos, width_pixels, y_pos, MOVE_PIXELS, LCD_COLOR_BLACK);
y_pos = y_pos + MOVE_PIXELS;
}
break;
}
case PS2_UP:
{
if(y_pos > MOVE_PIXELS-1) // if not moving past the bottom edge
{
ece210_lcd_draw_rectangle (x_pos, width_pixels, y_pos+height_pixels-MOVE_PIXELS, MOVE_PIXELS, LCD_COLOR_BLACK);
y_pos = y_pos - MOVE_PIXELS;
}
break;
}
case PS2_CENTER:
{
// Do nothing
break;
}
default:
{
break;
}
} // end switch
// ece210_lcd_draw_image(x_pos,width_pixels, y_pos,height_pixels, p_image,LCD_COLOR_RED, LCD_COLOR_BLACK);
// draw image at the new position using size and bitmap info and forground and background info.
ece210_wait_mSec(100);
// } // end while 1
//}// end main
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
