game: added jitter and interval fields, added text display

This commit is contained in:
schneider 2011-12-20 02:38:18 +01:00
parent 4534c46aff
commit 6697274019
4 changed files with 62 additions and 13 deletions

View File

@ -125,3 +125,5 @@ i2cInit
timer32Callback0
lcdRead
lcdInit
lcdSetCrsr

View File

@ -51,22 +51,29 @@ struct packet{
uint8_t gameMac[5];
uint8_t gameChannel;
//uint8_t playerMac[5]; playerMac = gameMac+1;
uint32_t gameId;
uint16_t gameId;
uint8_t gameFlags;
uint8_t interval;
uint8_t jitter;
uint8_t gameTitle[8];
}__attribute__((packed)) announce;
struct join{
uint32_t gameId;
uint8_t reserved[15];
uint16_t gameId;
uint8_t reserved[17];
}__attribute__((packed)) join;
}c;
uint16_t crc;
}__attribute__((packed));
#define FLAGS_MASS_GAME 1
#define FLAGS_MASS_GAME 1
#define FLAGS_SHORT_PACKET 2
#define FLAGS_LONG_RECV 4
#define FLAGS_ACK_JOINOK 1
#define MASS_ID 1
#define FLAGS_CLS 1
/**************************************************************************/
/* l0dable for playing games which are announced by other r0kets with the l0dabel r_game */
/* Values of buf[3]:
@ -82,12 +89,16 @@ struct packet{
uint32_t ctr;
uint32_t id;
uint32_t gameId;
uint16_t gameId;
uint8_t interval;
uint8_t jitter;
uint8_t flags;
void sendButton(uint8_t button);
void sendJoin(uint32_t game);
void processPacket(struct packet *p);
void processAnnounce(struct announce *a);
void processText(struct text *t);
uint8_t selectGame();
void playGame();
@ -124,14 +135,19 @@ void playGame(void)
sendButton(button);
while(1){
len = nrf_rcv_pkt_time(32,sizeof(p),(uint8_t*)&p);
if( flags & FLAGS_LONG_RECV )
len = nrf_rcv_pkt_time(64,sizeof(p),(uint8_t*)&p);
else
len = nrf_rcv_pkt_time(32,sizeof(p),(uint8_t*)&p);
if(len==sizeof(p)){
processPacket(&p);
}else{
break;
}
}
delayms(20);
int rnd = getRandom() % jitter;
delayms(interval+rnd);
};
}
@ -247,6 +263,9 @@ uint8_t selectGame()
memcpy(config.mac0, games[selected].gameMac, 5);
config.mac0[4]++;
config.channel = games[selected].gameChannel;
interval = games[selected].interval;
jitter = games[selected].jitter;
flags = games[selected].gameFlags;
nrf_config_set(&config);
if( games[selected].gameFlags & FLAGS_MASS_GAME )
return 1;
@ -274,7 +293,15 @@ void processPacket(struct packet *p)
{
if ((p->len==32) && (p->protocol=='G') && (p->id == id || p->id == 0) ){ //check sanity, protocol, id
if (p->command=='T'){
//processText(&(p->c.text));
struct packet ack;
ack.len=sizeof(p);
ack.protocol='G';
ack.command='a';
ack.id= id;
ack.ctr= p->ctr;
ack.c.ack.flags = 0;
nrf_snd_pkt_crc(sizeof(ack),(uint8_t*)&ack);
processText(&(p->c.text));
}
else if (p->command=='N'){
processNickRequest(&(p->c.nickrequest));
@ -293,6 +320,17 @@ void processAnnounce(struct announce *a)
}
}
void processText(struct text *t)
{
if( t->flags & FLAGS_CLS )
lcdClear() ;
lcdSetCrsr(t->x, t->y);
t->text[16] = 0;
lcdPrint(t->text);
lcdRefresh();
}
//increment ctr and send button state, id and ctr
void sendButton(uint8_t button)
{
@ -306,7 +344,9 @@ void sendButton(uint8_t button)
//lcdClear();
//lcdPrint("Key:"); lcdPrintInt(buf[2]); lcdNl();
nrf_snd_pkt_crc(sizeof(p),(uint8_t*)&p);
if( flags & FLAGS_SHORT_PACKET )
nrf_snd_pkt_crc(16,(uint8_t*)&p);
else
nrf_snd_pkt_crc(sizeof(p),(uint8_t*)&p);
}

View File

@ -19,7 +19,7 @@ class Game:
self.gamemac = [int(random.random()*254) for x in range(1,6)]
self.playermac = list(self.gamemac)
self.playermac[4]+=1
self.gameid = int(random.random()*(2**31))
self.gameid = int(random.random()*(2**15))
self.bridge = bridge.Bridge(device, self.channel, self.gamemac)
self.announce = packets.Announce(self.gamemac, self.channel,

View File

@ -4,6 +4,9 @@ def inttouint32(v):
def uint32toint(v):
return (ord(v[3])<< 24) + (ord(v[2])<<16) + (ord(v[1])<<8) + (ord(v[0]))
def inttouint16(v):
return chr(v&0xff)+chr((v>>8)&0xff)
class Packet:
def __init__(self, command, id=None):
self.ctr = 0
@ -55,7 +58,7 @@ class Button(Packet):
return s
class Announce(Packet):
def __init__(self, gameMac, gameChannel, gameId, gameFlags, gameTitle):
def __init__(self, gameMac, gameChannel, gameId, gameFlags, gameTitle, interval=30, jitter=16):
#always a broadcast
Packet.__init__(self, 'A', 0)
self.gameMac = gameMac
@ -63,14 +66,18 @@ class Announce(Packet):
self.gameId = gameId
self.gameFlags = gameFlags
self.gameTitle = gameTitle[0:8]
self.interval = interval
self.jitter = jitter
self.priority = 3
def toMessage(self):
message = Packet.toMessage(self)
message += ''.join([chr(x) for x in self.gameMac])
message += chr(self.gameChannel)
message += inttouint32(self.gameId)
message += inttouint16(self.gameId)
message += chr(self.gameFlags)
message += chr(self.interval)
message += chr(self.jitter)
message += self.gameTitle
if len(self.gameTitle) < 8:
message += '\x00'*(8-len(self.gameTitle))