Professional Documents
Culture Documents
Module
DisplayService
Revision
1.1
Description
* This module sends data to Processing (on desktop computer) through UART1 in
* order to draw a map of ships and allow for weapons aiming.
Author:
* Emma Morgan (clearScreen, addShip, updateWeapons functions)
* Ethan Kurteff (init, post, run, updateShip, updateWeapons functions)
Revised:
* Ethan Kurteff 5-27-21
*/
#include "DisplayService.h"
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "math.h"
// Hardware
#include <xc.h>
#include <proc/p32mx170f256b.h>
#include <sys/attribs.h> // for ISR macors
#include "Hardware_Configure.h"
//SPI
#include "spi.h"
//UART
#include "UARTrx.h"
//Screen Updates
#define TICK_TIME 100 //Time between screen updates (mS) (10Hz)
#define ALIVE_TIME 100 //Time in mS btwn incrementing alive cntrs for ships
#define MAX_ALIVE_TICKS 10 //# intervals of ALIVE_TIME before ship is dead
//Gameplay
//How many teams (MAX) will be playing? Note that Team 0 is a ghost (does not
exist))
#define NUM_TEAMS 10//6+1
#define OUR_TEAM 4 //What's our team #?
//Formatting
#define clearTag 3
#define shipTag 2
1
#define weaponsTag 1
#define LR_X 512000
#define LR_Y 256000
#define HALF_LR_X 256000
#define HALF_LR_Y 128000
#define RANGE 128000
#define PI 3.14159265
#define MAX_DIFF 16384//max distance between ships in scaled value
128000/7.8125
/*Uncomment the following to add two fake static ships to our display*/
//#define FAKESHIP1
//#define FAKE_X1 32768
//#define FAKE_Y1 16384
//#define FAKESHIP2
//#define FAKE_X2 7812
//#define FAKE_Y2 7812
/*Update our ship with aiming & location from SPI1 data*/
static void updateOurShip(void);
/***************************** POST
*****************************************/
bool PostDisplayService(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}
/***************************** RUN
*****************************************/
ES_Event_t RunDisplayService(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
switch (ThisEvent.EventType)
{
case ES_TIMEOUT:
{
switch (ThisEvent.EventParam)
{
case INIT_TMR:
{
/*Initialization Code:*/
hardwareInit();
ES_Timer_InitTimer(TICK_TMR, TICK_TIME);
}
case TICK_TMR:
{
#ifndef SPI_DEBUG
/*Tell the Display Service to draw next frame*/
clearScreen(); //Delete all entities
updateWeapons(myWeapon);//Draw the aim + range
drawShips(); //Add all the ships
// printf("\n\r",myWeapon.DIST);
#endif //SPI_DEBUG
#ifdef FAKESHIP1
3
struct ship targetShip;
targetShip.X_LSB = FAKE_X1 & 255;
targetShip.X_MSB = (FAKE_X1 >> 8) & 255;
targetShip.Y_LSB = FAKE_Y1 & 255;
targetShip.Y_MSB = (FAKE_Y1 >> 8) & 255;
targetShip.TEAM = 1;
targetShip.ISALIVE = 1;
targetShip.ALIVE_TICKS = 1;
addShip(targetShip, ships[OUR_TEAM]);
#endif
#ifdef FAKESHIP2
struct ship targetShip2;
targetShip2.X_LSB = FAKE_X2 & 255;
targetShip2.X_MSB = (FAKE_X2 >> 8) & 255;
targetShip2.Y_LSB = FAKE_Y2 & 255;
targetShip2.Y_MSB = (FAKE_Y2 >> 8) & 255;
targetShip2.TEAM = 2;
targetShip2.ISALIVE = 1;
targetShip2.ALIVE_TICKS = 1;
addShip(targetShip2, ships[OUR_TEAM]);
#endif
//printf("\n\rTICK");
//Re-init the tick timer for next update
ES_Timer_InitTimer(TICK_TMR, TICK_TIME);
}break;
case ALIVE_TMR:
{
/*Iterate through each team's ship*/
for(uint8_t i = 0; i < NUM_TEAMS; i++){
if(ships[i].TEAM != OUR_TEAM){//If the ship is not ours
ships[i].ALIVE_TICKS++; //Increment the alive ticks
}
}
break;
case ES_OUR_SHIP_UPDATE:
{
4
updateOurShip();
}
break;
}
return ReturnEvent;
}
/***************************************************************************
Private Functions
***************************************************************************/
/***************************** CLEAR
****************************************/
void clearScreen(void){
printf("%c\n", clearTag);
}
/***************************** ADD
*****************************************/
static void addShip(struct ship targetShip, struct ship ourShip){
uint16_t targetX = targetShip.X_LSB | (targetShip.X_MSB << 8); //combine
into uint16_t
uint16_t ourX = ourShip.X_LSB | (ourShip.X_MSB << 8); //combine into
uint16_t
uint16_t targetY = targetShip.Y_LSB | (targetShip.Y_MSB << 8); //combine
into uint16_t
uint16_t ourY = ourShip.Y_LSB | (ourShip.Y_MSB << 8); //combine into
uint16_t
int32_t xInv = ourX - 65535;
int32_t yInv = ourY - 65535;
int16_t xDiff;
int16_t yDiff;
if((abs(ourX - targetX) < MAX_DIFF)){ // if closer than 128
xDiff = ourX - targetX;
if(abs(ourY - targetY) < MAX_DIFF){
yDiff = ourY - targetY;
}else if(abs(yInv - targetY) < MAX_DIFF) {
yDiff = yInv - targetY;
}else{
yDiff = 0;
xDiff = 0;
}
}else if(abs(xInv - targetX) < MAX_DIFF) {
xDiff = xInv - targetX;
if(abs(ourY - targetY) < MAX_DIFF){
yDiff = ourY - targetY;
}else if(abs(yInv - targetY) < MAX_DIFF){
yDiff = yInv - targetY;
}else{
yDiff = 0;
xDiff = 0;
}
}else{
5
xDiff = 0;
yDiff = 0;
}
//#ifdef SPI_DEBUG
//printf("%f",theta);
// printf(" %d",aimArray[0]);
7
// printf(" %d",aimArray[1]);
// printf(" %d",aimArray[2]);
// printf(" %d",aimArray[3]);
// uint16_t xC = ships[OUR_TEAM].X_MSB << 8 | ships[OUR_TEAM].X_LSB;
// uint16_t yC = (uint16_t)shipArray[2] << 8 | shipArray[3];
// printf(" %d",shipArray[0]);
// printf(" %d",shipArray[1]);
// printf(" %d",shipArray[2]);
// printf(" %d",shipArray[3]);
// printf(" %d",shipArray[4]);
//#endif //SPI_DEBUG
////
// printf(" %d",xCoord);
// printf(" %d",yCoord);
//printf(" %d",theta16);