Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: N5110 PowerControl mbed
main.cpp
- Committer:
- ThomasBGill
- Date:
- 2015-04-15
- Revision:
- 2:0b0311609edc
- Parent:
- 1:0f774d41584c
- Child:
- 3:1a25939df22a
File content as of revision 2:0b0311609edc:
#include "mbed.h"
#include "N5110.h"
#include "PowerControl/PowerControl.h"
#include "PowerControl/EthernetPowerControl.h"
// vcc sce rst dc mosi clk led
N5110 lcd (p5, p6, p7, p8, p11, p13, p21);
InterruptIn Act (p22);
InterruptIn Start (p23);
InterruptIn Up (p24);
InterruptIn Down (p25);
InterruptIn Left (p26);
InterruptIn Right (p27);
AnalogIn Noise (p19);
#define USR_POWERDOWN (0x104)
#define WALL 0
#define FLOOR 1
#define ENTER 2
#define EXIT 3
#define RIGHT 0
#define LEFT 1
#define UP 2
#define DOWN 3
//Variables
int buttonFlagA = 0;
int buttonFlagS = 0;
int buttonFlagU = 0;
int buttonFlagD = 0;
int buttonFlagL = 0;
int buttonFlagR = 0;
int map[84][48];
//Enterance coordinates
int enx;
int eny;
//Exit coordinates
int exx;
int exy;
int sx;
int sy;
int dir;
int n;
//Power Saving
int semihost_powerdown()
{
uint32_t arg;
return __semihost(USR_POWERDOWN, &arg);
}
//ISR
void ActPressed()
{
buttonFlagA = 0;
}
void StartPressed()
{
buttonFlagS = 0;
}
void UpPressed()
{
buttonFlagU = 0;
}
void DownPressed()
{
buttonFlagD = 0;
}
void LeftPressed()
{
buttonFlagL = 0;
}
void RightPressed()
{
buttonFlagR = 0;
}
void Walls()
{
//Fill map with walls
for(int i=0; i<84; i++) {
for (int j=0; j<48; j++) {
map[i][j] = WALL;
}
}
}
void DrawMap()
{
//Draw map on screen
for(int i=0; i<84; i++) {
for (int j=0; j<48; j++) {
if(map[i][j] == FLOOR) {
lcd.clearPixel(i, j);
} else {
lcd.setPixel(i, j);
}
}
}
lcd.refresh();
}
void FirstRoom()
{
//Create initial room
int si = rand()%25 + 1;
int sj = rand()%15 + 1;
int sw = rand()%5+5;
int sh = rand()%5+5;
for(int i = si, w = si + sw; i <w; i++) {
for (int j = sj, h = sj + sh; j <h; j++) {
map[i][j] = FLOOR;
}
}
//Create enterance in room
enx = rand()%sw + si;
eny = rand()%sh + sj;
map[enx][eny] = ENTER;
}
void SetExit()
{
do {
exx = enx + rand()%30 +15;
exy = eny + rand()%20 +15;
} while(map[exx][exy] == WALL && exx > 84 && exy > 48);
map[exx][exy] = EXIT;
}
void MinePoint()
{
//Find start location
for(int i=0; i<84; i++) {
for (int j=0; j<48; j++) {
int r = rand()%6;
if(map[i][j] == FLOOR && map[i+1][j] == WALL && r == 0) {
dir = RIGHT;
sx = i;
sy = j;
break;
} else if(map[i][j] == FLOOR && map[i-1][j] == WALL && r == 0) {
dir = LEFT;
sx = i;
sy = j;
break;
} else if(map[i][j] == FLOOR && map[i][j+1] == WALL && r == 0) {
dir = UP;
sx = i;
sy = j;
break;
} else if(map[i][j] == FLOOR && map[i][j-1] == WALL && r == 0) {
dir = DOWN;
sx = i;
sy = j;
break;
}
}
}
}
void MineCorridorBuilder()
{
MinePoint();
//Get length
int l = rand()%5 + 5;
//Check direction of corridor
if(dir == RIGHT) {
for(int i = l; i>0; i--) {
if(map[sx+1][sy] == WALL && map[sx+1][sy+1] == WALL && map[sx+1][sy-1] == WALL && sx < 83) {
sx++;
map[sx][sy] = FLOOR;
} else
break;
}
} else if(dir == LEFT) {
for(int i = l; i>0; i--) {
if(map[sx-1][sy] == WALL && map[sx-1][sy+1] == WALL && map[sx-1][sy-1] == WALL && sx > 1) {
sx--;
map[sx][sy] = FLOOR;
} else
break;
}
} else if(dir == UP) {
for(int i = l; i>0; i--) {
if(map[sx][sy+1] == WALL && map[sx-1][sy+1] == WALL && map[sx+1][sy+1] == WALL && sy < 47) {
sy++;
map[sx][sy] = FLOOR;
} else
break;
}
} else if(dir == DOWN) {
for(int i = l; i>0; i--) {
if(map[sx][sy-1] == WALL && map[sx-1][sy-1] == WALL && map[sx+1][sy-1] == WALL && sy > 1) {
sy--;
map[sx][sy] = FLOOR;
} else
break;
}
}
}
void MineRoomBuilder()
{
MinePoint();
//Get length
int sw = rand()%5 + 5;
int sh = rand()%5 + 5;
int b = 0;
if(dir == RIGHT) {
sx++;
//Check each space. +1 to variable if wall. If total = w*h then build room
for(int i = sx; i < sx + sw; i++) {
for(int j = sy; j < sy + sh; j++) {
if(map[i][j] == WALL) {
b++;
}
}
}
if(b == sw*sh) {
for(int i = sx; i < sx + sw; i++) {
for(int j = sy; j < sy + sh; j++) {
if(i < 84 && j < 48) {
map[i][j] = FLOOR;
}
}
}
}
}
if(dir == LEFT) {
sx--;
//Check each space. +1 to variable if wall. If total = w*h then build room
for(int i = sx; i > sx - sw; i--) {
for(int j = sy; j < sy + sh; j++) {
if(map[i][j] == WALL) {
b++;
}
}
}
if(b == sw*sh) {
for(int i = sx; i > sx - sw; i--) {
for(int j = sy; j < sy + sh; j++) {
if(i < 84 && j < 48) {
map[i][j] = FLOOR;
}
}
}
}
}
if(dir == UP) {
sy++;
//Check each space. +1 to variable if wall. If total = w*h then build room
for(int i = sx; i < sx + sw; i++) {
for(int j = sy; j < sy + sh; j++) {
if(map[i][j] == WALL) {
b++;
}
}
}
if(b == sw*sh) {
for(int i = sx; i < sx + sw; i++) {
for(int j = sy; j < sy + sh; j++) {
if(i < 84 && j < 48) {
map[i][j] = FLOOR;
}
}
}
}
}
if(dir == DOWN) {
sy--;
//Check each space. +1 to variable if wall. If total = w*h then build room
for(int i = sx; i < sx + sw; i++) {
for(int j = sy; j > sy - sh; j--) {
if(map[i][j] == WALL) {
b++;
}
}
}
if(b == sw*sh) {
for(int i = sx; i < sx + sw; i++) {
for(int j = sy; j > sy - sh; j--) {
if(i < 84 && j < 48) {
map[i][j] = FLOOR;
}
}
}
}
}
}
void DungeonRoomBuilder()
{
sx = rand()%84;
sy = rand()%48;
//Get length
int sw = rand()%5 + 5;
int sh = rand()%5 + 5;
int b = 0;
//Check each space. +1 to variable if wall. If total = w*h then build room
for(int i = sx; i < sx + sw; i++) {
for(int j = sy; j < sy + sh; j++) {
if(map[i][j] == WALL) {
b++;
}
}
}
if(b == sw*sh) {
for(int i = sx; i < sx + sw; i++) {
for(int j = sy; j < sy + sh; j++) {
if(i < 84 && j < 48) {
map[i][j] = FLOOR;
}
}
}
}
}
void Neighbours()
{
//Check neighbours
n = 0;
if(map[sx+1][sy] == FLOOR) {
n++;
}
if(map[sx-1][sy] == FLOOR) {
n++;
}
if(map[sx][sy+1] == FLOOR) {
n++;
}
if(map[sx][sy-1] == FLOOR) {
n++;
}
}
void Maze()
{
//Random start point
sx = rand()%84;
sy = rand()%48;
char unvisited = 0x0; //0000 (Up, Down, Left, Right)
int visited[84][48];
int totalVisited = 0; //Max 4032
map[sx][sy] = FLOOR;
visited[sx][sy] = 1;
while(totalVisited < 4032) {
DrawMap();
unvisited = 0x0;
//Check if cell neighbours are visited
if(visited[sx+1][sy] == 0 && sx < 83) {
unvisited |= (1<<0); //Set 1st bit to 1
}
if(visited[sx-1][sy] == 0 && sx > 1) {
unvisited |= (1<<1); //Set 2nd bit to 1
}
if(visited[sx][sy-1] == 0 && sy > 1) {
unvisited |= (1<<2); //Set 3rd bit to 1
}
if(visited[sx][sy+1] == 0 && sy < 47) {
unvisited |= (1<<3); //Set 4th bit to 1
}
dir = rand()%4;
if(dir == RIGHT && (0x1 & unvisited)) {
sx++;
visited[sx][sy] = 1;
totalVisited++;
//Check neighbours
Neighbours();
//If only has 1 neighbour create floor
if(n == 1) {
map[sx][sy] = FLOOR;
} else {
sx = rand()%84;
sy = rand()%48;
Neighbours();
}
while(visited[sx][sy] == 1 || n != 1 || map[sx][sy] != WALL);
} else if(dir == LEFT && (0x2 & unvisited)) {
sx--;
visited[sx][sy] = 1;
totalVisited++;
//Check neighbours
Neighbours();
//If only has 1 neighbour create floor
if(n == 1) {
map[sx][sy] = FLOOR;
} else {
sx = rand()%84;
sy = rand()%48;
Neighbours();
}
while(visited[sx][sy] == 1 || n != 1 || map[sx][sy] != WALL);
} else if(dir == DOWN && (0x4 & unvisited)) {
sy--;
visited[sx][sy] = 1;
totalVisited++;
//Check neighbours
Neighbours();
//If only has 1 neighbour create floor
if(n == 1) {
map[sx][sy] = FLOOR;
} else {
sx = rand()%84;
sy = rand()%48;
Neighbours();
}
while(visited[sx][sy] == 1 || n != 1 || map[sx][sy] != WALL);
} else if(dir == UP && (0x8 & unvisited)) {
sy++;
visited[sx][sy] = 1;
totalVisited++;
//Check neighbours
Neighbours();
//If only has 1 neighbour create floor
if(n == 1) {
map[sx][sy] = FLOOR;
} else {
sx = rand()%84;
sy = rand()%48;
Neighbours();
}
while(visited[sx][sy] == 1 || n != 1 || map[sx][sy] != WALL);
} else if((0x1 & unvisited) || (0x2 & unvisited) || (0x4 & unvisited) || (0x8 & unvisited)) {
continue;
} else {
//Find new starting co-ordinate
do {
sx = rand()%84;
sy = rand()%48;
Neighbours();
} while(visited[sx][sy] == 1 || n != 1 || map[sx][sy] != WALL);
}
}
}
void MineBuilder()
{
Walls();
FirstRoom();
int fn = rand()%20 + 20;
for(int i = fn; i>0; i--) {
int f = rand()% 5;
if(f == 0) {
MineRoomBuilder();
} else {
MineCorridorBuilder();
}
}
SetExit();
}
void DungeonBuilder()
{
Walls();
int rn = rand()%20 + 20;
for(int i = rn; i>0; i--) {
DungeonRoomBuilder();
}
int cn = rand()%20 + 20;
for(int i = cn; i>0; i--) {
MineCorridorBuilder();
}
}
void World()
{
Walls();
MineBuilder();
//DungeonBuilder();
DrawMap();
}
int main()
{
//Power Saving
PHY_PowerDown ();
int result = semihost_powerdown();
//Generate random seed
srand(Noise*1000000);
//Initilize screen
lcd.init();
//Game loop
while(1) {
Maze();
//World();
wait(2.0);
}
}