random is working

This commit is contained in:
Wolfgang Hottgenroth 2024-03-21 11:34:20 +01:00
parent 50486f6ec0
commit f69c5bc59e
5 changed files with 18 additions and 19 deletions

View File

@ -7,7 +7,7 @@ MCU=msp430g2553
CFLAGS=-Wall -mmcu=$(MCU) -std=gnu99 -I $(TOOLCHAIN_PREFIX)/include -O1 -g0 CFLAGS=-Wall -mmcu=$(MCU) -std=gnu99 -I $(TOOLCHAIN_PREFIX)/include -O1 -g0
# for debugging # for debugging
CFLAGS+= -g3 -ggdb -gdwarf-2 #CFLAGS+= -g3 -ggdb -gdwarf-2
LDFLAGS=-mmcu=$(MCU) -L $(TOOLCHAIN_PREFIX)/include LDFLAGS=-mmcu=$(MCU) -L $(TOOLCHAIN_PREFIX)/include

View File

@ -10,6 +10,7 @@
#include "game.h" #include "game.h"
#include "buttons.h" #include "buttons.h"
#include "shapes.h" #include "shapes.h"
#include "myrand.h"
int main() { int main() {
@ -26,6 +27,7 @@ int main() {
schInit(); schInit();
ledInit(); ledInit();
myRandInit();
canvasInit(); canvasInit();
shapesInit(); shapesInit();

View File

@ -4,21 +4,23 @@
#include "myrand.h" #include "myrand.h"
void myRandInit() { void myRandInit() {
ADC10CTL0 = SREF_1 | ADC10SHT_1 | ADC10SR | REFON | ADC10ON;
ADC10CTL1 = INCH_10; ADC10CTL1 = INCH_10;
ADC10CTL0 = SREF_1 | ADC10SHT_1 | REFON | ADC10ON;
} }
uint8_t myRandGet() { uint16_t myRandGet() {
ADC10CTL0 |= ENC | ADC10SC; 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; res <<= 1;
uint8_t r = n & 0x00ff; res |= ADC10MEM & 0x0001;
}
ADC10CTL0 &= ~(ADC10IFG | ENC); return res;
return r;
} }

View File

@ -2,7 +2,7 @@
#define _MYRAND_H_ #define _MYRAND_H_
void myRandInit(); void myRandInit();
uint8_t myRandGet(); uint16_t myRandGet();
#endif // _MYRAND_H_ #endif // _MYRAND_H_

View File

@ -356,14 +356,9 @@ void shapesInit() {
} }
void stoneCreate() { void stoneCreate() {
// static uint8_t cnt = 0; uint16_t n = myRandGet();
uint8_t r = myRandGet(); uint8_t m = n % 7;
uint8_t cnt = r % e_ShapeInvalid; stone.shape = ((shape_t[]){ e_I, e_O, e_T, e_Z, e_S, e_L, e_J })[m];
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;
// }
stone.orientation = e_0; stone.orientation = e_0;
stone.x = 5; stone.x = 5;
stone.y = 0; stone.y = 0;