Jogo da Cobrinha desenvolvido como avaliação da disciplina de Sistemas Embarcados do curso de Inovação e Desenvolvimento Mobile e Web do IFPE.
Revision 0:6ca395acc2a5, committed 2016-12-10
- Comitter:
- renangueiros
- Date:
- Sat Dec 10 13:43:27 2016 +0000
- Commit message:
- final-commit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.gitignore Sat Dec 10 13:43:27 2016 +0000 @@ -0,0 +1,4 @@ +.build +.mbed +projectfiles +*.py*
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Sat Dec 10 13:43:27 2016 +0000 @@ -0,0 +1,89 @@ +# Getting started with Blinky on mbed OS + +This is a very simple guide, reviewing the steps required to get Blinky working on an mbed OS platform. + +Please install [mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli). + +## Get the example application! + +From the command line, import the example: + +``` +mbed import mbed-os-example-blinky +cd mbed-os-example-blinky +``` + +### Now compile + +Invoke `mbed compile` specifying the name of your platform and your favorite toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the ARM Compiler 5: + +``` +mbed compile -m K64F -t ARM +``` + +Your PC may take a few minutes to compile your code. At the end you should get the following result: + +``` +[snip] ++----------------------------+-------+-------+------+ +| Module | .text | .data | .bss | ++----------------------------+-------+-------+------+ +| Misc | 13939 | 24 | 1372 | +| core/hal | 16993 | 96 | 296 | +| core/rtos | 7384 | 92 | 4204 | +| features/FEATURE_IPV4 | 80 | 0 | 176 | +| frameworks/greentea-client | 1830 | 60 | 44 | +| frameworks/utest | 2392 | 512 | 292 | +| Subtotals | 42618 | 784 | 6384 | ++----------------------------+-------+-------+------+ +Allocated Heap: unknown +Allocated Stack: unknown +Total Static RAM memory (data + bss): 7168 bytes +Total RAM memory (data + bss + heap + stack): 7168 bytes +Total Flash memory (text + data + misc): 43402 bytes +Image: .\.build\K64F\ARM\mbed-os-example-blinky.bin +``` + +### Program your board + +1. Connect your mbed device to the computer over USB. +1. Copy the binary file to the mbed device . +1. Press the reset button to start the program. + +You should see the LED of your platform turning on and off. + +Congratulations if you managed to complete this test! + +## Export the project to Keil MDK and debug your application + +From the command line, run the following command: + +``` +mbed export -m K64F -i uvision +``` + +To debug the application: + +1. Start uVision. +1. Import the uVision project generated earlier. +1. Compile your application and generate an `.axf` file. +1. Make sure uVision is configured to debug over CMSIS-DAP (From the Project menu > Options for Target '...' > Debug tab > Use CMSIS-DAP Debugger). +1. Set breakpoints and start a debug session. + +![Image of uVision](img/uvision.png) + +## Troubleshooting + +1. Make sure `mbed-cli` is working correctly and its version is greater than `0.8.9` + + ``` + mbed --version + ``` + + If not, you can update it easily: + + ``` + pip install mbed-cli --upgrade + ``` + +2. If using Keil MDK, make sure you have a license installed. [MDK-Lite](http://www.keil.com/arm/mdk.asp) has a 32KB restriction on code size.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TermControl.lib Sat Dec 10 13:43:27 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/earlz/code/TermControl/#ef410bacf0e5
Binary file img/uvision.png has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Dec 10 13:43:27 2016 +0000 @@ -0,0 +1,234 @@ +#ifndef _COLORS_ +#define _COLORS_ + +/* FOREGROUND */ +#define RST "\x1B[0m" +#define KRED "\x1B[31m" +#define KGRN "\x1B[32m" +#define KYEL "\x1B[33m" +#define KBLU "\x1B[34m" +#define KMAG "\x1B[35m" +#define KCYN "\x1B[36m" +#define KWHT "\x1B[37m" + +#define FRED(x) KRED x RST +#define FGRN(x) KGRN x RST +#define FYEL(x) KYEL x RST +#define FBLU(x) KBLU x RST +#define FMAG(x) KMAG x RST +#define FCYN(x) KCYN x RST +#define FWHT(x) KWHT x RST + +#define BOLD(x) "\x1B[1m" x RST +#define UNDL(x) "\x1B[4m" x RST + +#endif /* _COLORS_ */ + +#include "mbed.h" +#include "TermControl.h" +#include "rtos.h" + +DigitalOut buzz(A0); + +Serial saida(USBTX, USBRX); +Serial entrada(USBTX, USBRX); + +TermControl terminal; + +unsigned char atualiza_stk[1024]; +unsigned char controle_stk[1024]; + +Thread atualiza_thread(osPriorityNormal, 1024, &atualiza_stk[0]); +Thread controle_thread(osPriorityNormal, 1024, &controle_stk[0]); + +char tecla; + +int x; +int direcao; +int cobraX[500]; +int cobraY[500]; +int tamanho; +int macaX; +int macaY; +int velocidade; +int aceleracao; +int pontuacao; +int statusJogo; + +static void atualiza_task(void) +{ + for (;;) { + + buzz = 0; + + for (x = tamanho; x > 0; x--) { + cobraX[x] = cobraX[x - 1]; + cobraY[x] = cobraY[x - 1]; + } + + if (direcao == 0) + cobraX[0]--; + if (direcao == 1) + cobraY[0]--; + if (direcao == 2) + cobraX[0]++; + if (direcao == 3) + cobraY[0]++; + + terminal.SetCursor(cobraX[tamanho], cobraY[tamanho]); + saida.printf(" "); + + if (macaX == cobraX[0] && macaY == cobraY[0]) { + tamanho++; + pontuacao++; + + buzz = 1; + saida.putc('\a'); + + macaX = 2 + (rand() % 77); + macaY = 4 + (rand() % 18); + + aceleracao -= 10; + velocidade += 10; + } + + terminal.SetCursor(cobraX[0], cobraY[0]); + saida.printf(FGRN("%c"), 219); + + terminal.SetCursor(macaX, macaY); + saida.printf(FRED("%c"), 219); + + terminal.SetCursor(0, 0); + saida.printf("Velocidade: %d", velocidade); + + terminal.SetCursor(20, 0); + saida.printf("Pontos: %d", pontuacao); + + Thread::wait(aceleracao); + + for (x = 1; x < tamanho; x++) { + if (cobraX[0] == cobraX[x] && cobraY[0] == cobraY[x]) { + statusJogo = 0; + return; + } + } + + if (cobraY[0] == 2 || cobraY[0] == 22 || cobraX[0] == 1 || cobraX[0] == 79) { + statusJogo = 0; + return; + } + } +} + +static void controle_task(void) +{ + for(;;) { + + tecla = entrada.getc(); + + if (tecla == 'a' || tecla == 'A') + direcao = 0; + if (tecla == 'w' || tecla == 'W') + direcao = 1; + if (tecla == 'd' || tecla == 'D') + direcao = 2; + if (tecla == 's' || tecla == 'S') + direcao = 3; + } +} + +void resetaJogo() +{ + + atualiza_thread.terminate(); + controle_thread.terminate(); + + statusJogo = 1; + + memset(cobraX, 0, sizeof(cobraX)); + memset(cobraY, 0, sizeof(cobraY)); + cobraX[0] = 2; + cobraY[0] = 12; + + tamanho = 1; + + direcao = 2; + + aceleracao = 200; + velocidade = 10; + + pontuacao = 0; +} + +void novoJogo() +{ + resetaJogo(); + + terminal.Reset(); + terminal.Clear(); + + for (x = 2; x < 22; x++) { + terminal.SetCursor(0, x); + saida.printf(FBLU("%c"), 219); + } + + for (x = 0; x < 79; x++) { + terminal.SetCursor(x, 2); + saida.printf(FBLU("%c"), 219); + } + + for (x = 2; x < 22; x++) { + terminal.SetCursor(79, x); + saida.printf(FBLU("%c"), 219); + } + + for (x = 0; x < 80; x++) { + terminal.SetCursor(x, 22); + saida.printf(FBLU("%c"), 219); + } + + srand(time(NULL)); + macaX = 2 + (rand() % 77); + macaY = 4 + (rand() % 18); + + do { + atualiza_thread.start(atualiza_task); + controle_thread.start(controle_task); + } while(statusJogo != 0); + + terminal.Reset(); + terminal.Clear(); + + terminal.SetCursor(0, 0); + + saida.printf(FGRN("\r██████████████████████████████████████████████████████████████████████████████\r\n█ █\r\n█ █\r\n█ ████ ██ █ \r\n█ █ ████ █ ███ █ █ █ █ ███ █ ███ █ \r\n█ █ █ █████ █ █ █ █ █ █ █ █ ██ █\r\n█ █ ███ █████ █ █ █ █████ █ █ █ █ █████ █ █\r\n█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █\r\n█ ████ ███ █ █ █ ████ ██ █ ████ █ █\r\n█ █\r\n█ █\r\n█ █\r\n█ Pontuação: %d █\r\n█ █\r\n█ █\r\n█ Sua cobra ficou muito pequena! █\r\n█ █\r\n█ Pressione ENTER para tentar aumentar sua cobra. █\r\n█ █\r\n██████████████████████████████████████████████████████████████████████████████"), pontuacao); + + resetaJogo(); + + do { + tecla = entrada.getc(); + } while((tecla != 13)); + + novoJogo(); +} + +int main() +{ + saida.baud(115200); + entrada.baud(115200); + + terminal.SetTerminal(&saida); + terminal.Reset(); + terminal.Clear(); + + terminal.SetCursor(0, 0); + + saida.printf(FGRN("\r██████████████████████████████████████████████████████████████████████████████\r\n█ █\r\n█ █\r\n█ █\r\n█ ██████ █ █\r\n█ ██ █ █ ███ ████ █ █ ███ █\r\n█ ██ ██ █ █ █ █ █ █ █\r\n█ ██ █ █ █████ ███ █████ █\r\n█ █ ██ █ █ █ █ █ █ █ █\r\n█ ██████ █ █ ███ █ █ ████ █\r\n█ █\r\n█ █\r\n█ █\r\n█ █\r\n█ █\r\n█ █\r\n█ Sua cobra pode ser realmente grande? █\r\n█ █\r\n█ Pressione ENTER para tentar aumentar sua cobra █\r\n█ █\r\n██████████████████████████████████████████████████████████████████████████████")); + + do { + tecla = entrada.getc(); + } while((tecla != 13)); + + novoJogo(); + +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Sat Dec 10 13:43:27 2016 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#a1c0840b3d69060e5eb708edb18358e424a40f51