roox has uploaded this change for review. (
https://gerrit.osmocom.org/c/osmocom-bb/+/19487 )
Change subject: Implemented rand() and automove for snake.
..
Implemented rand() and automove for snake.
Change-Id: I94bf887160a4a58423b93fcfd47c92e09404507c
---
M src/target/firmware/abb/twl3025.c
M src/target/firmware/apps/snake_game/main.c
2 files changed, 73 insertions(+), 32 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmocom-bb refs/changes/87/19487/1
diff --git a/src/target/firmware/abb/twl3025.c
b/src/target/firmware/abb/twl3025.c
index 55ff2ad..5b792d6 100644
--- a/src/target/firmware/abb/twl3025.c
+++ b/src/target/firmware/abb/twl3025.c
@@ -229,9 +229,10 @@
twl3025_reg_write(VRPCDEV, 0x01);
}
-void twl3025_power_off_now(void) {
-/* The phone will restart if the power butten has not been released.
- * This can be useful for development. */
+void twl3025_power_off_now(void)
+{
+ /* The phone will restart if the power butten has not been released.
+* This can be useful for development. */
unsigned long flags;
local_firq_save(flags);
twl3025_reg_write(VRPCDEV, 0x01);
diff --git a/src/target/firmware/apps/snake_game/main.c
b/src/target/firmware/apps/snake_game/main.c
index a4e8296..c263df4 100644
--- a/src/target/firmware/apps/snake_game/main.c
+++ b/src/target/firmware/apps/snake_game/main.c
@@ -48,6 +48,16 @@
#include
#include
+unsigned long next = 1;
+/* This is not a good random number generator ... */
+int rand(void) {
+ next = next * 110351 + 12;
+ return (unsigned int)(next & 0x7fff);
+}
+void srand(unsigned int seed) {
+ next = seed;
+}
+
#define BLANK 0
#define HEAD 1
#define TAIL 2
@@ -60,6 +70,10 @@
#define STDLEN 3
#define HEIGHT 7
#define WIDTH 16
+
+/* Time in ms to wait to the next auto move of the snake. */
+#define WAIT_TIME_AUTOMOVE 300
+
struct position {
int x;
int y;
@@ -75,14 +89,16 @@
void increaseBodyAge();
void setFood() {
int x, y, c;
- /* for (c = 0; c < 10;c++) { */
- /* x = rand() % (WIDTH -1); */
- /* y = rand() % (HEIGHT -1); */
- /* if (field[x][y] == BLANK) { */
- /* field[x][y] = FOOD; */
- /* return; */
- /* } */
- /* } */
+ for (c = 0; c < 10;c++) {
+ x = rand() % (WIDTH -1);
+ y = rand() % (HEIGHT -1);
+ if (DEBUG > 0) printf("Next %u\n", next);
+ if (DEBUG > 0) printf("Rand (%d|%d)\n", x, y);
+ if (field[x][y] == BLANK) {
+ field[x][y] = FOOD;
+ return;
+ }
+ }
for (x = 0; x < WIDTH; x++) {
for (y = 0; y < HEIGHT; y++) {
if (field[x][y] == BLANK) {
@@ -98,25 +114,29 @@
{
x = 6 * x;
y = 8 * (y+1) -3;
- printf("Put string %s to (%d|%d)\n", text, x, y);
+ if (DEBUG > 1) printf("Put string %s to (%d|%d)\n", text, x, y);
fb_gotoxy(x, y);
fb_putstr(text, framebuffer->width);
}
-void movepos(char dim) {
+
+
+char Move;
+void movepos(char move) {
+ Move = move;
setItem(pos.x, pos.y, SBODY);
- switch (dim) {
- case 'Y': pos.y++;break;
- case 'y': pos.y--;break;
- case 'X': pos.x++;break;
- case 'x': pos.x--;break;
+ switch (move) {
+ case 'h': pos.x--;break;
+ case 'j': pos.y++;break;
+ case 'k': pos.y--;break;
+ case 'l': pos.x++;break;
}
- switch (dim) {
- case 'Y': case 'y':
+ switch (move) {
+ case 'j': case 'k':
if (pos.y == -1) pos.y = HEIGHT -1;
else if (pos.y == HEIGHT) pos.y = 0;
increaseBodyAge();
break;
- case 'X': case 'x':
+ case 'l': case 'h':
if (pos.x == -1) pos.x = WIDTH -1;
else if (pos.x == WIDTH) pos.x = 0;
increaseBodyAge();
@@ -125,6 +145,21 @@
setItem(pos.x, pos.y, HEAD);
printField();
}
+void movepos_timer_cb(void *p) {
+ struct osmo_timer_list *tmr = (struct osmo_timer_list*)p;
+ if (DEBUG > 0) printf("Auto move %c\n", Move);
+ movepos(Move);
+
+ osmo_timer_schedule(tmr, WAIT_TIME_AUTOMOVE);
+}
+static struct osmo_timer_list move_snake_timer = {
+ .cb = _timer_cb,
+ .data = _snake_timer
+};
+void movepos_keypress(char keypress) {
+ Move = keypress;
+ osmo_timer_schedule(_snake_timer, 0);
+}
void increaseBodyAge() {
int y, x;
@@ -309,15 +344,12 @@
calypso_clk_dump();
puts(hr);
- keypad_set_handler(_handler);
-
/* Dump clock config after PLL set */