From f69c5bc59e1a1241edb7720eec0a1813a763f172 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Thu, 21 Mar 2024 11:34:20 +0100 Subject: [PATCH] random is working --- game-ctrl/Makefile | 2 +- game-ctrl/main.c | 2 ++ game-ctrl/myrand.c | 20 +++++++++++--------- game-ctrl/myrand.h | 2 +- game-ctrl/shapes.c | 11 +++-------- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/game-ctrl/Makefile b/game-ctrl/Makefile index ebba119..92d1cd0 100644 --- a/game-ctrl/Makefile +++ b/game-ctrl/Makefile @@ -7,7 +7,7 @@ MCU=msp430g2553 CFLAGS=-Wall -mmcu=$(MCU) -std=gnu99 -I $(TOOLCHAIN_PREFIX)/include -O1 -g0 # for debugging -CFLAGS+= -g3 -ggdb -gdwarf-2 +#CFLAGS+= -g3 -ggdb -gdwarf-2 LDFLAGS=-mmcu=$(MCU) -L $(TOOLCHAIN_PREFIX)/include diff --git a/game-ctrl/main.c b/game-ctrl/main.c index 487657f..f315b53 100644 --- a/game-ctrl/main.c +++ b/game-ctrl/main.c @@ -10,6 +10,7 @@ #include "game.h" #include "buttons.h" #include "shapes.h" +#include "myrand.h" int main() { @@ -26,6 +27,7 @@ int main() { schInit(); ledInit(); + myRandInit(); canvasInit(); shapesInit(); diff --git a/game-ctrl/myrand.c b/game-ctrl/myrand.c index 43928fe..663ef8e 100644 --- a/game-ctrl/myrand.c +++ b/game-ctrl/myrand.c @@ -4,21 +4,23 @@ #include "myrand.h" void myRandInit() { - ADC10CTL0 = SREF_1 | ADC10SHT_1 | ADC10SR | REFON | ADC10ON; ADC10CTL1 = INCH_10; + ADC10CTL0 = SREF_1 | ADC10SHT_1 | REFON | ADC10ON; } -uint8_t myRandGet() { - ADC10CTL0 |= ENC | ADC10SC; +uint16_t myRandGet() { + uint16_t res = 0; - while ((ADC10CTL0 & ADC10IFG) == 0); + for (uint8_t i = 0; i < 16; i++) { + ADC10CTL0 |= ENC | ADC10SC; + + while ((ADC10CTL1 & ADC10BUSY)); - uint16_t n = ADC10MEM; - uint8_t r = n & 0x00ff; + res <<= 1; + res |= ADC10MEM & 0x0001; + } - ADC10CTL0 &= ~(ADC10IFG | ENC); - - return r; + return res; } diff --git a/game-ctrl/myrand.h b/game-ctrl/myrand.h index fef8e87..4802f18 100644 --- a/game-ctrl/myrand.h +++ b/game-ctrl/myrand.h @@ -2,7 +2,7 @@ #define _MYRAND_H_ void myRandInit(); -uint8_t myRandGet(); +uint16_t myRandGet(); #endif // _MYRAND_H_ diff --git a/game-ctrl/shapes.c b/game-ctrl/shapes.c index 22e4760..34ac487 100644 --- a/game-ctrl/shapes.c +++ b/game-ctrl/shapes.c @@ -356,14 +356,9 @@ void shapesInit() { } void stoneCreate() { -// static uint8_t cnt = 0; - uint8_t r = myRandGet(); - uint8_t cnt = r % e_ShapeInvalid; - stone.shape = ((shape_t[]){ e_I, e_O, e_T, e_Z, e_S, e_L, e_J })[cnt]; -// cnt++; -// if (cnt >= e_ShapeInvalid) { -// cnt = 0; -// } + uint16_t n = myRandGet(); + uint8_t m = n % 7; + stone.shape = ((shape_t[]){ e_I, e_O, e_T, e_Z, e_S, e_L, e_J })[m]; stone.orientation = e_0; stone.x = 5; stone.y = 0;