Skip to content

Commit

Permalink
New
Browse files Browse the repository at this point in the history
  • Loading branch information
frlundst committed Mar 3, 2021
1 parent b178df7 commit 9b03e50
Show file tree
Hide file tree
Showing 20 changed files with 1,224 additions and 244 deletions.
2 changes: 2 additions & 0 deletions .deps/delay.S.P
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
delay.S.o: delay.S
delay.S :
6 changes: 6 additions & 0 deletions .deps/game.c.P
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
game.c.o: game.c /opt/mcb32tools/include/stdint.h \
/opt/mcb32tools/lib/gcc/mipsel-mcb32-elf/4.9.2/include/stdint-gcc.h \
/opt/mcb32tools/include/pic32mx.h snake.h
game.c /opt/mcb32tools/include/stdint.h :
/opt/mcb32tools/lib/gcc/mipsel-mcb32-elf/4.9.2/include/stdint-gcc.h :
/opt/mcb32tools/include/pic32mx.h snake.h :
2 changes: 2 additions & 0 deletions .deps/labwork.S.P
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
labwork.S.o: labwork.S
labwork.S :
2 changes: 2 additions & 0 deletions .deps/snake.c.P
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
snake.c.o: snake.c
snake.c :
Binary file modified data.c.o
Binary file not shown.
102 changes: 102 additions & 0 deletions delay.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# labwork.S
# Written 2015-2017 by F Lundevall
# Skeleton file for IS1200/IS1500 lab 1.
# The macros PUSH and POP are in the public domain.
# Please add your own code at the end of the file.

#
# Please keep the two macros PUSH and POP unchanged
#
.macro PUSH reg
addi $sp,$sp,-4
sw \reg,0($sp)
.endm

.macro POP reg
lw \reg,0($sp)
addi $sp,$sp,4
.endm
#
# Please add your own code below this line
#
.global delay
.global time2string
.data
.text

hexasc:
andi $a0,$a0, 0xf #Checks 4 LSB
addi $v0,$a0,0x30
jr $ra
nop

delay:
PUSH $s3
li $s3, 47000
li $t6, 0
li $t7, 0
j loop
nop
loop:
beq $t7, $a0, end
nop
sub $a0, $a0, 1
j loop2
nop
loop2:
beq $t6, $s3, loop
nop
addi $t6, $t6, 1
j loop2
nop
end:

POP $s3

jr $ra
nop

time2string:
PUSH $s0
PUSH $s1
PUSH $ra
PUSH $a0
add $s0,$a1,$0 #Saves time info to t0
add $s1,$a0,$0 #Saves time info address to t1

srl $a0,$s0,12 # Första minut-talet
jal hexasc
nop
sb $v0, 0($s1)

srl $a0,$s0,8 # Andra minut-talet
jal hexasc
nop
sb $v0, 1($s1)

li $t4, 0x3A #Contains ":"
nop
sb $t4, 2($s1) # Lägger till kolon

srl $a0,$s0,4 # första sekund-talet
jal hexasc
nop
sb $v0, 3($s1)

add $a0, $s0, $zero # andra sekund-talet
jal hexasc
nop
sb $v0, 4($s1)

li $t5,0x00
nop
sb $t5, 5($s1)

POP $a0
POP $ra
POP $s1
POP $s0

jr $ra
nop

Binary file added delay.S.o
Binary file not shown.
101 changes: 95 additions & 6 deletions display.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/*Written by Fredrik Lundström and Michell Dib 2021 (c)*/

#include <stdint.h>
#include <pic32mx.h>
#include "snake.h"
#include <pic32mx.h>
#include "snake.h" //Project header file

void *stdin, *stdout, *stderr;

#define DISPLAY_CHANGE_TO_COMMAND_MODE (PORTFCLR = 0x10)
#define DISPLAY_CHANGE_TO_DATA_MODE (PORTFSET = 0x10)
Expand All @@ -14,18 +18,25 @@
#define DISPLAY_TURN_OFF_VDD (PORTFSET = 0x40)
#define DISPLAY_TURN_OFF_VBAT (PORTFSET = 0x20)

/*If any point in the array is set to one the pixel att the same position be lit*/
uint8_t display[32][128]; //Human readable pixel position and activation
uint8_t oled_display[512]; //Computer readable pixel position and activation

/*(TAKEN FROM LAB) Quick sleep timer*/
void quicksleep(int cyc) {
int i;
for(i = cyc; i > 0; i--);
}

uint8_t spi_send_recv(uint8_t data) {
/*(TAKEN FROM LAB) Send data to the OLED display*/
uint8_t spi_send_recv(uint8_t data) { //LAB
while(!(SPI2STAT & 0x08));
SPI2BUF = data;
while(!(SPI2STAT & 1));
return SPI2BUF;
}

/*(TAKEN FROM LAB) Initialize OLED display*/
void display_init(void){
DISPLAY_CHANGE_TO_COMMAND_MODE;
quicksleep(10);
Expand Down Expand Up @@ -56,13 +67,12 @@ void display_init(void){
spi_send_recv(0xAF);
}

void display_string(int line, char *s) {
void display_string(int line, char *s) { //LAB
int i;
if(line < 0 || line >= 4)
return;
if(!s)
return;

for(i = 0; i < 16; i++)
if(*s) {
textbuffer[line][i] = *s;
Expand All @@ -71,7 +81,7 @@ void display_string(int line, char *s) {
textbuffer[line][i] = ' ';
}

void display_update(void) {
void display_update(void) { //LAB
int i, j, k;
int c;
for(i = 0; i < 4; i++) {
Expand All @@ -94,3 +104,82 @@ void display_update(void) {
}
}
}

/*(TAKEN FROM LAB) This will print any image on the display with the help of an array containing the map of the active and inactive pixels*/
void display_image(int x, const uint8_t *data) { //LAB
int i, j;

for(i = 0; i < 4; i++) {
DISPLAY_CHANGE_TO_COMMAND_MODE;

spi_send_recv(0x22);
spi_send_recv(i);

spi_send_recv(x & 0xF);
spi_send_recv(0x10 | ((x >> 4) & 0xF));

DISPLAY_CHANGE_TO_DATA_MODE;

for(j = 0; j < 128; j++)
spi_send_recv(data[i*128 + j]);
}
}

void translateToImage() {
int page, column, row, c, k;
uint8_t powerOfTwo = 1;
uint8_t oledNumber = 0;
int survivalMode = 0;

for(page = 0; page < 4; page++) {
for(column = 0; column < 128; column++) {
powerOfTwo = 1;
oledNumber = 0;

for(row = 0; row < 8; row++) {
if(display[8 * page + row][column]) {
oledNumber |= powerOfTwo;
}
powerOfTwo <<= 1;
}
oled_display[column + page * 128] = oledNumber;
}
}
}

void create_object(int xPos, int yPos, int width, int height) {
int row, column;

for (row = 0; row < 32; row++) {
for (column = 0; column < 128; column++) {
if (row >= yPos && row <= (yPos + height) && column >= xPos && column <= (xPos + width)) {
display[row][column] = 1;
}
}
}
}

void clear_display() {
int row, column, i;

for(row = 0; row < 32; row++) {
for(column = 0; column < 128; column++) {
display[row][column] = 0;
}
}

for (i = 0; i < 512; i++) {
oled_display[i] = 0;
}
}

void display_start(){
clear_display();
snake();
//create_object(64,16,1,1);
translateToImage();
display_image(0, oled_display);
}



Binary file modified display.c.o
Binary file not shown.
54 changes: 54 additions & 0 deletions game.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*Written by Fredrik Lundström and Michell Dib 2021 (c)*/

#include <stdint.h>
#include <pic32mx.h>
#include "snake.h" //Project header file

int score = 0;
int speed = 20;


int snake_length;
int snake_width = 1;
int snake_x = 64;
int snake_y = 16;

int apple_x;
int apple_y;

void snake(){
snake_create();
apple_create();

if(snake_x == apple_x && snake_y == apple_y){
apple_eat();
}
delay(speed);
snake_move();
}

void snake_create(){
snake_length = 5;
create_object(snake_x, snake_y, snake_length, snake_width);
}

void snake_move(){
if(snake_x > 128){
snake_x = 0;
}else{
snake_x++;
}
}

void apple_create(){
//Randomize
create_object(apple_x, apple_y, 1, 1);
}

void apple_eat(){
display[apple_x][apple_y] = 0; //Clear apple pixel
apple_x = 5;
apple_y = 5;
apple_create();
score++;
}
Binary file added game.c.o
Binary file not shown.
19 changes: 15 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/*Written by Fredrik Lundström and Michell Dib 2021 (c)*/

#include <stdint.h>
#include <pic32mx.h>
#include "snake.h"

void user_isr(){
return;
}

int main(void){
/*
/*
This will set the peripheral bus clock to the same frequency
as the sysclock. That means 80 MHz, when the microcontroller
is running at 80 MHz. Changed 2017, as recommended by Axel.
Expand Down Expand Up @@ -44,9 +50,14 @@ int main(void){
SPI2CONSET = 0x20;
/* SPI2CON bit ON = 1; */
SPI2CONSET = 0x8000;

display_init(); //Initialize OLED display

while(1){
display_start();
}

display_init();
display_string(0, "TEST");
display_update();
return;
}


Binary file modified main.c.o
Binary file not shown.
Binary file modified outfile.elf
Binary file not shown.
Loading

0 comments on commit 9b03e50

Please sign in to comment.