Merge git://github.com/r0ket/r0ket

This commit is contained in:
bernd 2011-08-03 23:58:15 +02:00
commit 4b7df23466
34 changed files with 1340 additions and 458 deletions

View File

@ -73,6 +73,8 @@ clean:
for dir in $(SUBDIRS); do \
$(MAKE) $(CONFIG_MAKE_PRINTDIRECTORY) -C $$dir clean; \
done
$(MAKE) $(CONFIG_MAKE_PRINTDIRECTORY) -C l0dable clean
flash: all
../tools/bootloader/autoflash

View File

@ -7,7 +7,7 @@
TARGET = LPC13xx
FLASH = 32K
SRAM = 8K
RAMCODE=2048
RAMCODE=2548
# For USB HID support the LPC134x reserves 384 bytes from the sram,
# if you don't want to use the USB features, just use 0 here.

View File

@ -4,6 +4,8 @@ Ck<hex>: set encryptionkey
Ct<hex>: set txmac
Cc<hex>: set channel
Ct<hex>: set rxmac(0)
Cl<hex>: set rxlens
Ce<hex>: sec encrpytion on/off
s <hex>: send packet
s+ <hex>: send packet 10 times

View File

@ -15,25 +15,9 @@
void init_nick();
void fancyNickname();
#include "lcd/allfonts.h"
void forLoadables(int i){
if(i){
lcdSetPixel(0,0);
font=&Font_Invaders;
ECIES_encyptkeygen(0,0,0,0,0,0);
systickGetTicks();
ECIES_embedded_public_key_validation();
point_copy(0,0,0,0);
point_is_zero(0,0);
bitstr_parse_export(0,0);
nrf_rcv_pkt_time_encr();
};
};
void main_final(void) {
//checkFirstBoot();
init_final();
forLoadables(0);
menuflags|=MENU_TIMEOUT;
while(1){

View File

@ -47,7 +47,7 @@ void changer(void){
lcdPrint("[");
lcdPrint(IntToStr(current_offset/visible_lines,1,0));
lcdPrint("/");
lcdPrint(IntToStr(numentries/visible_lines,1,0));
lcdPrint(IntToStr((numentries-1)/visible_lines,1,0));
lcdPrint("]");
lcdNl();

View File

@ -94,7 +94,7 @@ void tick_flame(void) { // every 10ms
}
if (flameMode == FLAME_UP) {
if (flameI2Cpwm + flameSpeedUp > flameI2Cpwm ) {
if (0xFF - flameI2Cpwm >= flameSpeedUp ) {
flameI2Cpwm += flameSpeedUp;
} else {
flameI2Cpwm = 0xFF;
@ -113,7 +113,7 @@ void tick_flame(void) { // every 10ms
}
if (flameMode == FLAME_DOWN) {
if (flameI2Cpwm - flameSpeedDown < flameI2Cpwm ) {
if (flameSpeedDown <= flameI2Cpwm) {
flameI2Cpwm -= flameSpeedDown;
} else {
flameI2Cpwm = 0x00;

View File

@ -22,59 +22,6 @@ void init_mesh(void){
initMesh();
};
void m_tset(void){
_timet=1311961112;
};
//# MENU debug MeshInfo
void m_time(void){
struct tm* tm;
char c[2]={0,0};
getInputWaitRelease();
delayms(100);
do{
lcdClear();
tm= mygmtime(getSeconds());
lcdPrint(IntToStr(tm->tm_hour,2,F_LONG));
lcdPrint(":");
lcdPrint(IntToStr(tm->tm_min,2,F_LONG|F_ZEROS));
lcdPrint(":");
lcdPrint(IntToStr(tm->tm_sec,2,F_LONG|F_ZEROS));
lcdNl();
lcdPrint(IntToStr(tm->tm_mday,2,F_LONG));
lcdPrint(".");
lcdPrint(IntToStr(tm->tm_mon+1,2,0));
lcdPrint(".");
lcdPrint(IntToStr(tm->tm_year+YEAR0,4,F_LONG|F_ZEROS));
lcdNl();
lcdNl();
lcdPrint("<");
for(int i=0;i<MESHBUFSIZE;i++){
if(!meshbuffer[i].flags&MF_USED){
c[0]='_';
}else{
c[0]=meshbuffer[i].pkt[0];
};
lcdPrint(c);
};
lcdPrintln(">");
lcdPrint("Gen:");
lcdPrintInt(meshgen);
lcdNl();
lcdRefresh();
delayms_queue(50);
}while ((getInputRaw())==BTN_NONE);
};
inline void blink(char a, char b){
gpioSetValue (a,b, 1-gpioGetValue(a,b));
};
int choose(char * texts, int8_t menuselection){
uint8_t numentries = 0;
uint8_t visible_lines = 0;

View File

@ -13,147 +13,6 @@
/**************************************************************************/
//# MENU debug ChkLight
void ChkLight(void) {
int dx=0;
int dy=8;
dx=DoString(0,dy,"Light:");
DoString(0,dy+16,"Night:");
while ((getInputRaw())==BTN_NONE){
DoInt(dx,dy,GetLight());
DoInt(dx,dy+16,isNight());
DoInt(dx,dy+8,GLOBAL(daytrig));
lcdDisplay();
delayms_queue(100);
};
dx=DoString(0,dy+24,"Done.");
}
//# MENU debug ChkBattery
void ChkBattery(void) {
do{
lcdClear();
lcdPrintln("Voltage:");
lcdPrintln(IntToStr(GetVoltage(),5,0));
lcdNl();
lcdPrintln("Chrg_stat:");
if(gpioGetValue(RB_PWR_CHRG)){
lcdPrintln("1");
}else{
lcdPrintln("0");
};
lcdRefresh();
delayms_queue(100);
} while ((getInputRaw())==BTN_NONE);
}
//# MENU debug Uptime
void uptime(void) {
int t;
int h;
char flag;
while ((getInputRaw())==BTN_NONE){
lcdClear();
lcdPrintln("Uptime:");
t=getTimer()/(1000/SYSTICKSPEED);
h=t/60/60;
flag=F_ZEROS;
if(h>0){
lcdPrint(IntToStr(h,2,flag));
lcdPrint("h");
flag|=F_LONG;
};
h=t/60%60;
if(h>0){
lcdPrint(IntToStr(h,2,flag));
lcdPrint("m");
flag|=F_LONG;
};
h=t%60;
if(h>0){
lcdPrint(IntToStr(h,2,flag));
lcdPrint("s");
};
lcdNl();
lcdNl();
lcdPrintln("Ticks:");
lcdPrint(IntToStr(_timectr,10,0));
lcdRefresh();
delayms_queue(200);
};
lcdPrintln("done.");
}
//# MENU debug Uuid
void uuid(void) {
IAP_return_t iap_return;
iap_return = iapReadSerialNumber();
lcdClear();
lcdPrintln("UUID:");
lcdPrintIntHex(iap_return.Result[0]); lcdNl();
lcdPrintIntHex(iap_return.Result[1]); lcdNl();
lcdPrintIntHex(iap_return.Result[2]); lcdNl();
lcdPrintIntHex(iap_return.Result[3]); lcdNl();
lcdNl();
lcdPrintln("Beacon ID:");
lcdPrintln(IntToStrX(GetUUID32(),4));
lcdRefresh();
}
//# MENU debug Qstatus
void Qstatus(void) {
int dx=0;
int dy=8;
lcdClear();
dx=DoString(0,dy+16,"Qdepth:");
while ((getInputRaw())!=BTN_ENTER){
DoInt(dx,dy+16,(the_queue.qend-the_queue.qstart+MAXQENTRIES)%MAXQENTRIES);
lcdDisplay();
if(getInputRaw()!=BTN_NONE)
work_queue();
else
delayms(10);
};
dy+=16;
dx=DoString(0,dy,"Done.");
};
void blink_led0(void){
gpioSetValue (RB_LED0, 1-gpioGetValue(RB_LED0));
};
void tick_alive(void){
static int foo=0;
if(GLOBAL(alivechk)==0)
return;
if(foo++>500/SYSTICKSPEED){
foo=0;
if(GLOBAL(alivechk)==2)
push_queue(blink_led0);
else
blink_led0();
};
return;
};
//# MENU debug ShowSP
void getsp(void) {
int dx=0;
int dy=8;
int x;
lcdClear();
dx=DoString(0,dy,"SP:");
while ((getInputRaw())==BTN_NONE){
__asm( "mov %0, sp\n" : "=r" (x) :);
DoIntX(0,dy+8,x);
lcdDisplay();
delayms_queue(50);
};
dy+=16;
dx=DoString(0,dy,"Done.");
};
//# MENU msc
void msc_menu(void){

View File

@ -84,7 +84,7 @@ void tick_flame(void) { // every 10ms
}
if (flameMode == FLAME_UP) {
if (flameI2Cpwm + flameSpeedUp > flameI2Cpwm ) {
if (0xFF - flameI2Cpwm >= flameSpeedUp) {
flameI2Cpwm += flameSpeedUp;
} else {
flameI2Cpwm = 0xFF;
@ -103,7 +103,7 @@ void tick_flame(void) { // every 10ms
}
if (flameMode == FLAME_DOWN) {
if (flameI2Cpwm - flameSpeedDown < flameI2Cpwm ) {
if (flameSpeedDown <= flameI2Cpwm) {
flameI2Cpwm -= flameSpeedDown;
} else {
flameI2Cpwm = 0x00;

View File

@ -109,13 +109,13 @@ print "}};";
print "";
print "// Tick & init functions:";
print qq!void tick_$app(void) {!;
print qq!inline void tick_$app(void) {!;
for (sort @ticks){
print qq!\t$_();!;
};
print qq!};!;
print qq!void init_$app(void) {!;
print qq!inline void init_$app(void) {!;
for (sort @inits){
print qq!\t$_();!;
};

View File

@ -5,120 +5,34 @@
#include "lcd/render.h"
#include "lcd/allfonts.h"
void ReinvokeISP(void);
void EnableWatchdog(uint32_t ms);
void delayms(uint32_t ms);
void backlightInit(void);
/**************************************************************************/
void main_s(void) {
//Make PIO1_11 an analog input
gpioSetDir(RB_LED3, gpioDirection_Input);
IOCON_PIO1_11 = 0x41;
backlightInit();
uint32_t j=0;
//disable the JTAG on PIO3_3
IOCON_PIO3_3 = 0x10;
int yctr=8;
int dx=0;
font_direction = FONT_DIR_LTR; // LeftToRight is the default
font = &Font_8x8;
static FONT fonts[] = {
&Font_7x8,
&Font_Ubuntu18pt, // 3 byte-font
&Font_8x8,
};
int fontctr=0;
yctr=18;
uint8_t trigger;
#define SEND
#ifdef SEND
trigger=200;
gpioSetDir(RB_LED0, gpioDirection_Output);
IOCON_JTAG_TDI_PIO0_11 = 0x11;
#else
trigger=380;
gpioSetDir(RB_LED0, gpioDirection_Input);
IOCON_JTAG_TDI_PIO0_11 = 0x42;
#endif
uint32_t ctr=0;
uint8_t key;
//lcdClear();
while (1) {
ctr++;
uint32_t results;
lcdDisplay();
delayms(10);
uint8_t contrast = lcdRead(0xDF);
lcdInit(); // display
lcdPrint("reg=");
lcdPrintCharHex(contrast);
lcdNl();
lcdDisplay();
while(1);
delayms(500);
font=fonts[fontctr];
//key= getInputWait();
if(gpioGetValue(RB_BTN3)==0){
while(gpioGetValue(RB_BTN3)==0);
trigger +=10;
};
if(gpioGetValue(RB_BTN2)==0){
while(gpioGetValue(RB_BTN2)==0);
trigger -=10;
};
//dx=DoString(0,0,"Trig:");
//dx=DoInt(dx,0,trigger);
//DoString(dx,0," ");
if(gpioGetValue(RB_BTN0)==0){
while(gpioGetValue(RB_BTN0)==0);
DoString(0,8,"Enter ISP!");
lcdDisplay();
EnableWatchdog(1000*5);
ReinvokeISP();
};
font = &Font_Ubuntu36pt;
static uint8_t ctrx=0, ctry=0, dirx=1, diry=1;
dx=DoString(ctrx,ctry,"S");
if( dirx ){
if(ctrx++ == 60)
dirx = 0;
}else{
if(ctrx-- == 0)
dirx=1;
// Easy flashing
if(key==BTN_LEFT){
DoString(0,8,"Enter ISP!");
lcdDisplay();
ISPandReset();
}
}
if( diry ){
if(ctry++ == 12)
diry = 0;
}else{
if(ctry-- == 0)
diry=1;
}
font = &Font_7x8;
results = adcRead(1);
//dx=DoString(0,yctr+28,"Voltage:");
results *= 10560;
results /= 1024;
//DoInt(dx,yctr+28,results);
if( results < 3500 ){
DoString(0,yctr+30,"Shutdown");
gpioSetValue (RB_PWR_GOOD, 0);
gpioSetValue (RB_LCD_BL, 0);
SCB_SCR |= SCB_SCR_SLEEPDEEP;
PMU_PMUCTRL = PMU_PMUCTRL_DPDEN_DEEPPOWERDOWN;
__asm volatile ("WFI");
}else{
//DoString(0,yctr+30,"OK ");
;
}
}
return;
}
void tick_minimal(void){
return;
};

View File

@ -29,10 +29,9 @@ uint32_t const beaconkey[4] = {
uint32_t remotekey[4] = {
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
};
uint32_t meshkey[4] = {
0x0, 0x0, 0x0, 0x0
};
extern uint32_t const meshkey[4];
char funkencrypt=0;
#if CFG_USBMSC
#error "MSC is defined"
@ -151,6 +150,7 @@ int process(char * input){
config.nrmacs=1;
nrf_config_set(&config);
memcpy(thekey,meshkey,sizeof(thekey));
funkencrypt=1;
}else if(input[1]=='r'){
config.channel=REMOTE_CHANNEL;
memcpy(config.txmac,REMOTE_MAC,5);
@ -159,6 +159,7 @@ int process(char * input){
config.nrmacs=1;
nrf_config_set(&config);
memcpy(thekey,remotekey,sizeof(thekey));
funkencrypt=1;
}else if(input[1]=='b'){
config.channel=BEACON_CHANNEL;
memcpy(config.txmac,BEACON_MAC,5);
@ -167,6 +168,7 @@ int process(char * input){
config.nrmacs=1;
nrf_config_set(&config);
memcpy(thekey,beaconkey,sizeof(thekey));
funkencrypt=1;
}else if(input[1]=='?'){
nrf_config_get(&config);
puts_plus("Ch: ");puts_plus(IntToStrX( config.channel,2 )); puts_plus("\r\n");
@ -202,6 +204,10 @@ int process(char * input){
puts_plus(" ");
};
puts_plus("\r\n");
puts_plus("encrypt : ");
puts_plus(IntToStrX( funkencrypt,2 ));
puts_plus("\r\n");
};
}else if(input[0]=='C'){
int len;
@ -228,6 +234,15 @@ int process(char * input){
}else if(input[1]=='c'){
config.channel=*hex;
nrf_config_set(&config);
}else if(input[1]=='l'){
config.maclen[0]=uint8ptouint32(hex);
config.maclen[1]=uint8ptouint32(hex+4);
config.maclen[2]=uint8ptouint32(hex+8);
config.maclen[3]=uint8ptouint32(hex+12);
config.maclen[4]=uint8ptouint32(hex+16);
nrf_config_set(&config);
}else if(input[1]=='e'){
funkencrypt= uint8ptouint32(hex);
};
}else if (input[0]=='s'){
__attribute__ ((aligned (4))) uint8_t buf[32];
@ -251,7 +266,7 @@ int process(char * input){
len+=2; // Add crc!
memcpy(buf,hex,len);
status=nrf_snd_pkt_crc_encr(len,buf,thekey);
status=nrf_snd_pkt_crc_encr(len,buf,funkencrypt?thekey:NULL);
puts_plus("P ");
puts_plus("[");puts_plus(IntToStrX(len,2));puts_plus("] ");
@ -266,7 +281,7 @@ int process(char * input){
while(--ctr>0){
delayms(23);
memcpy(buf,hex,len);
status=nrf_snd_pkt_crc_encr(len,buf,thekey);
status=nrf_snd_pkt_crc_encr(len,buf,funkencrypt?thekey:NULL);
};
}else if (input[1]=='t'){
static int ctr=1;
@ -283,7 +298,7 @@ int process(char * input){
buf[12]=0xff; // salt (0xffff always?)
buf[13]=0xff;
status=nrf_snd_pkt_crc_encr(16,buf,thekey);
status=nrf_snd_pkt_crc_encr(16,buf,funkencrypt?thekey:NULL);
}else{
};
puts_plus("S state=");
@ -310,7 +325,7 @@ int process(char * input){
puts_plus("D receive ...\r\n");
nrf_rcv_pkt_start();
do{
len=nrf_rcv_pkt_poll_dec(sizeof(buf),buf,thekey);
len=nrf_rcv_pkt_poll_dec(sizeof(buf),buf,funkencrypt?thekey:NULL);
// Receive
if(len==0||len==-1||len==-2){ //No pkt, Pkt2large, NoPktError
delayms(10);

View File

@ -17,20 +17,16 @@ int bitstr_parse_export(char *exp, const char *s);
void ECIES_setup(void);
void ECIES_encyptkeygen(uint8_t *px, uint8_t *py,
uint8_t k1[16], uint8_t k2[16], uint8_t *Rx_exp, uint8_t *Ry_exp);
void ECIES_encyptkeygen(uint8_t *px, uint8_t *py, uint8_t k1[16], uint8_t k2[16], uint8_t *Rx_exp, uint8_t *Ry_exp);
int ECIES_decryptkeygen(uint8_t *rx, uint8_t *ry,
uint8_t k1[16], uint8_t k2[16], const char *privkey);
int ECIES_decryptkeygen(uint8_t *rx, uint8_t *ry, uint8_t k1[16], uint8_t k2[16], const char *privkey);
#define ECIES_OVERHEAD (8 * NUMWORDS + 8)
/* ECIES encryption; the resulting cipher text message will be
(len + ECIES_OVERHEAD) bytes long */
void ECIES_encryption(char *msg, const char *text, int len,
const char *Px, const char *Py);
void ECIES_encryption(char *msg, const char *text, int len, const char *Px, const char *Py);
/* ECIES decryption */
int ECIES_decryption(char *text, const char *msg, int len,
const char *privkey);
int ECIES_decryption(char *text, const char *msg, int len, const char *privkey);
#define MACRO(A) do { A; } while(0)
#define MIN(a, b) ((a) < (b) ? (a) : (b))

View File

@ -52,6 +52,8 @@ uint8_t getInputWait(void) {
uint8_t getInputWaitTimeout(int timeout) {
uint8_t key;
if(timeout==0)
return getInputWait();
int end=_timectr+timeout*(1000/SYSTICKSPEED);
while ((key=getInputRaw())==BTN_NONE){
if(_timectr>end)

View File

@ -1,6 +1,6 @@
#ifndef _RANDOM_H_
#define _RANDOM_H_
#include <stdint.h>
void randomInit(void);
uint32_t getRandom(void);

View File

@ -14,7 +14,7 @@ struct in{
bool done;
} s_input;
void inputInit(char p[],char s[], uint8_t l, uint8_t as, uint8_t ae) {
static void inputInit(char p[],char s[], uint8_t l, uint8_t as, uint8_t ae) {
//TODO: Check length!
s_input.prompt = p;
s_input.line = s;
@ -30,7 +30,7 @@ void inputInit(char p[],char s[], uint8_t l, uint8_t as, uint8_t ae) {
}
void inputMove() {
static void inputMove() {
char *cur = s_input.line+s_input.pos+s_input.dcursor;
switch(getInputWaitRepeat()){
case BTN_LEFT:
@ -78,7 +78,7 @@ void inputMove() {
}
}
void inputDraw() {
static void inputDraw() {
char tmp[2]= {0,0};
lcdClear();
DoString(0,0,s_input.prompt);
@ -89,7 +89,7 @@ void inputDraw() {
DoString(s_input.dcursor * CHARWIDTH, 40, "-");
}
void inputClean() {
static void inputClean() {
for (int x=0;x<=s_input.maxlength;x++) {
if (s_input.line[x] == 0) {
x--;

View File

@ -1,8 +1,8 @@
#ifndef _XXTEA_H_
#define _XXTEA_H_
#include <stdint.h>
void xxtea_cbcmac(uint32_t mac[4], uint32_t *data,
uint32_t len, uint32_t const key[4]);
void xxtea_cbcmac(uint32_t mac[4], uint32_t *data, uint32_t len, uint32_t const key[4]);
void xxtea_encode_words(uint32_t *v, int n, uint32_t const k[4]);
void xxtea_decode_words(uint32_t *v, int n, uint32_t const k[4]);

View File

@ -30,7 +30,7 @@ void execute_file (const char * fname, uint8_t checksignature, uint8_t decode){
dst=(void (*)(void)) (sram_top);
lcdPrint("T:"); lcdPrintIntHex(dst); lcdNl();
*/
dst=(void (*)(void)) 0x10001800;
dst=(void (*)(void)) 0x1000160C;
res=f_open(&file, fname, FA_OPEN_EXISTING|FA_READ);

View File

@ -8,11 +8,11 @@
#include "basic/random.h"
char meshgen=0; // Generation
char meshincctr=0; // Generation
char meshincctr=0;
MPKT meshbuffer[MESHBUFSIZE];
uint32_t const meshkey[4] = {
0x00000000, 0x00000000, 0x00000000, 0x00000000
0x00000042, 0x000005ec, 0x00000023, 0x00000005
};
struct NRF_CFG oldconfig;
@ -45,6 +45,8 @@ MPKT * meshGetMessage(uint8_t type){
if(meshbuffer[free].flags==MF_FREE){
memset(&meshbuffer[free],0,sizeof(MPKT));
MO_TYPE_set(meshbuffer[free].pkt,type);
MO_GEN_set(meshbuffer[free].pkt,meshgen);
meshbuffer[free].flags=MF_USED;
};
return &meshbuffer[free];
};

View File

@ -153,13 +153,12 @@ int nrf_rcv_pkt_poll_dec(int maxsize, uint8_t * pkt, uint32_t const key[4]){
return len;
cmpcrc=crc16(pkt,len-2);
if(cmpcrc != (pkt[len-2] <<8 | pkt[len-1])) {
if(key!=NULL)
xxtea_decode_words((uint32_t*)pkt,len/4,key);
cmpcrc=crc16(pkt,len-2);
if(cmpcrc != (pkt[len-2] <<8 | pkt[len-1])) {
return -3; // CRC failed
};
cmpcrc=crc16(pkt,len-2);
if(cmpcrc != (pkt[len-2] <<8 | pkt[len-1])) {
return -3; // CRC failed
};
return len;
};

View File

@ -35,3 +35,33 @@ push_queue
the_config
the_queue
work_queue
selectFile
nrf_snd_pkt_crc_encr
nrf_rcv_pkt_time_encr
getInput
ECIES_encyptkeygen
f_open
f_read
strlen
strcpy
xxtea_encode_words
getRandom
crc16
f_write
f_close
ECIES_decryptkeygen
bitstr_parse_export
f_get_rc_string
xxtea_decode_words
systickGetTicks
lcdFill
memcpy
DoChar
font
Font_Invaders
Font_7x8
lcdBuffer
meshGetMessage
nickname
uint32touint8p
uint8ptouint32

View File

@ -124,7 +124,7 @@ void uuid(void) {
lcdPrintIntHex(iap_return.Result[3]); lcdNl();
lcdNl();
lcdPrintln("Beacon ID:");
lcdPrintln(IntToStrX(GetUUID32(),4));
lcdPrintln(IntToStrX(GetUUID32(),8));
lcdRefresh();
}

491
firmware/l0dable/invaders.c Normal file
View File

@ -0,0 +1,491 @@
#include <sysinit.h>
#include <string.h>
#include "basic/basic.h"
#include "basic/config.h"
#include "basic/random.h"
#include "lcd/render.h"
#include "lcd/display.h"
#include "lcd/allfonts.h"
#include "funk/mesh.h"
#include "usetable.h"
/**************************************************************************/
#define POS_PLAYER_Y 60
#define POS_PLAYER_X RESX/2-3
#define POS_UFO_Y 0
#define ENEMY_ROWS 3
#define ENEMY_COLUMNS 6
#define DISABLED 255
#define UFO_PROB 1024
#define TYPE_PLAYER 1
#define TYPE_ENEMY_A 3
#define TYPE_ENEMY_B 2
#define TYPE_ENEMY_C 4
#define TYPE_UFO 5
#define BUNKERS 3
#define BUNKER_WIDTH 10
static const uint8_t BUNKER_X[] = {15, RESX/2-BUNKER_WIDTH/2,RESX-BUNKER_WIDTH-15};
static const uint8_t ENEMY_WIDTHS[] = {8,10,12};
struct gamestate {
char player;
char ufo;
char shot_x, shot_y;
char shots_x[ENEMY_COLUMNS];
char shots_y[ENEMY_COLUMNS];
char alive;
int16_t move;
char direction, lastcol;
bool killed;
bool step;
uint32_t score;
uint16_t level;
int8_t rokets;
char enemy_x[ENEMY_ROWS][ENEMY_COLUMNS];
char enemy_row_y[ENEMY_ROWS];
uint8_t bunker[BUNKERS][BUNKER_WIDTH];
} game;
char key;
bool highscore_set(uint32_t score, char nick[]);
uint32_t highscore_get(char nick[]);
void init_game();
void init_enemy();
void check_end();
void move_ufo();
void move_shot();
void move_shots();
void move_player();
void move_enemy();
void draw_score();
void draw_bunker();
void draw_player();
void draw_enemy();
void draw_shots();
void draw_sprite(char type, char x, char y);
void draw_ufo();
void screen_intro();
void screen_gameover();
void screen_level();
bool check_bunker(char xpos, char ypos, int8_t shift);
void ram(void) {
//gpioSetValue (RB_LED1, CFG_LED_OFF);
//backlightInit();
while(1) {
screen_intro();
game.rokets = 3;
game.level = 1;
game.score = 0;
init_game();
screen_level();
while (game.rokets>=0) {
////checkISP();
lcdFill(0);
check_end();
move_ufo();
move_shot();
move_shots();
move_player();
move_enemy();
draw_score();
draw_ufo();
draw_bunker();
draw_player();
draw_enemy();
draw_shots();
// draw_status();
lcdDisplay();
delayms(12);
}
screen_gameover();
}
return;
}
void screen_intro() {
uint32_t highscore;
char highnick[20];
char key=0;
while(key==0) {
lcdFill(0);
font = &Font_Invaders;
DoString(28,25,"ABC");
font = &Font_7x8;
DoString (28,40,"SPACE");
DoString (18,50,"INVADERS");
highscore = highscore_get(highnick);
DoInt(0, 0, highscore);
DoString (0, 9, highnick);
lcdDisplay();
delayms_queue(50);
key=getInput();
}
}
void screen_gameover() {
char key =0;
while(key==0) {
lcdFill(0);
font = &Font_7x8;
DoString (12,32, "GAME OVER");
DoInt (0,0, game.score);
if (highscore_set(game.score, GLOBAL(nickname)))
DoString (0,9,"HIGHSCORE!");
lcdDisplay();
delayms_queue(50);
key=getInput();
}
}
void screen_level() {
lcdFill(0);
draw_score();
font = &Font_7x8;
int dx = DoString(20,32, "Level ");
DoInt(dx,32,game.level);
lcdDisplay();
delayms(500);
}
bool highscore_set(uint32_t score, char nick[]) {
MPKT * mpkt= meshGetMessage('i');
if(MO_TIME(mpkt->pkt)>score)
return false;
MO_TIME_set(mpkt->pkt,score);
strcpy((char*)MO_BODY(mpkt->pkt),nick);
return true;
}
uint32_t highscore_get(char nick[]){
MPKT * mpkt= meshGetMessage('i');
strcpy(nick,(char*)MO_BODY(mpkt->pkt));
return MO_TIME(mpkt->pkt);
}
void init_game(void) {
game.player = POS_PLAYER_X;
game.shot_x = DISABLED;
game.shot_y = 0;
game.alive = ENEMY_ROWS*ENEMY_COLUMNS;
game.move = 0;
if (getRandom()%2 == 0) {
game.direction = -1;
game.lastcol = ENEMY_COLUMNS-1;
} else {
game.direction = 1;
game.lastcol = 0;
}
game.killed = 0;
game.step = false;
game.ufo = DISABLED;
init_enemy();
for (int col=0; col<ENEMY_COLUMNS; col++){
game.shots_x[col] = DISABLED;
}
for (int b=0; b<BUNKERS; b++){
//for (int slice=0; slice<BUNKER_WIDTH; slice++){
// game.bunker[b][slice] = 255<<2;
//}
game.bunker[b][0] = 0b00111100;
game.bunker[b][1] = 0b01111100;
game.bunker[b][2] = 0b11111100;
game.bunker[b][3] = 0b11100000;
game.bunker[b][4] = 0b11100000;
game.bunker[b][5] = 0b11100000;
game.bunker[b][6] = 0b11100000;
game.bunker[b][7] = 0b11111100;
game.bunker[b][8] = 0b01111100;
game.bunker[b][9] = 0b00111100;
}
}
void init_enemy() {
for (int row = 0; row<ENEMY_ROWS; row++) {
game.enemy_row_y[row] = 10 + (40/ENEMY_ROWS)*row;
for (int col = 0; col<ENEMY_COLUMNS; col++) {
game.enemy_x[row][col] = 5+(86/ENEMY_COLUMNS)*col+(2-row);
}
}
}
bool check_bunker(char xpos, char ypos, int8_t shift){
for (int b=0; b<BUNKERS; b++) {
if (xpos>BUNKER_X[BUNKERS-1-b] &&
xpos<BUNKER_X[BUNKERS-1-b]+BUNKER_WIDTH &&
ypos<RESY-8 &&
ypos>RESY-16) {
int offset = BUNKER_WIDTH - (xpos-BUNKER_X[BUNKERS-1-b]);
if (game.bunker[b][offset]!=0) {
if (shift>0)
game.bunker[b][offset]&=game.bunker[b][offset]<<shift;
else
game.bunker[b][offset]&=game.bunker[b][offset]>>-shift;
return true;
}
}
}
return false;
}
void move_shot() {
//No shot, do nothing
if(game.shot_x == DISABLED) {
return;
}
//moving out of top, end shot
if (game.shot_y <= 0) {
game.shot_x = DISABLED;
return;
}
if (check_bunker(game.shot_x,game.shot_y-5,1 ))
game.shot_x=DISABLED;
//check for collision with enemy, kill enemy if
for (int row=0; row<ENEMY_ROWS; row++) {
if (game.enemy_row_y[row]+6 >= game.shot_y && game.enemy_row_y[row]+6 < game.shot_y+7) {
for(int col = 0; col<ENEMY_COLUMNS; col++) {
if(game.shot_x >= game.enemy_x[row][col] && game.shot_x < game.enemy_x[row][col]+ENEMY_WIDTHS[row]) {
game.enemy_x[row][col]=DISABLED;
game.shot_x = DISABLED;
game.alive--;
game.score+=(3-row)*10;
return;
}
}
}
}
//check for collision with ufo
if (game.ufo != DISABLED &&
game.shot_x>game.ufo &&
game.shot_x<game.ufo + 16 &&
game.shot_y<8) {
game.ufo = DISABLED;
game.score += 50;
}
game.shot_y -= 2;
}
void move_shots() {
for (int col = 0; col<ENEMY_COLUMNS; col++){
//No shot, maybe generate
if (game.shots_x[col] == DISABLED) {
for (int row = 0; row<ENEMY_ROWS; row++) {
if (game.enemy_x[row][col] != DISABLED) {
if(getRandom()%(game.alive*20/((game.level/3)+1))==0) {
game.shots_x[col] = game.enemy_x[row][col]+5;
game.shots_y[col] = game.enemy_row_y[row]+0;
}
}
}
continue;
}
//moving out of bottm, end shot
if (game.shots_y[col] >= RESY) {
game.shots_x[col] = DISABLED;
return;
}
//check for collision with bunker
if (check_bunker(game.shots_x[col],game.shots_y[col],-1))
game.shots_x[col]=DISABLED;
//check for collision with player
if (game.shots_y[col] >= RESY-13 &&
game.shots_x[col] > game.player+1 &&
game.shots_x[col] < game.player+6) {
game.killed = true;
}
//move shots down
game.shots_y[col] += 1;
}
}
void move_ufo() {
if (game.ufo == DISABLED) {
if ((getRandom()%UFO_PROB)==0) {
game.ufo = 0;
}
return;
}
if (game.ufo >= RESX){
game.ufo = DISABLED;
return;
}
game.ufo++;
}
void move_player() {
if(gpioGetValue(RB_BTN0)==0 && game.player > 0 ){
game.player-=1;
}
if(gpioGetValue(RB_BTN1)==0 && game.player < RESX-8){
game.player+=1;
}
if(gpioGetValue(RB_BTN4)==0 && game.shot_x == 255){
game.shot_x = game.player+4;
game.shot_y = POS_PLAYER_Y;
}
}
void move_enemy() {
if(game.move > 0){
game.move-=game.level/5+1;
return;
}
game.step = !game.step;
for (int col = 0; col < ENEMY_COLUMNS; col++) {
for (int row = 0; row < ENEMY_ROWS; row++) {
char pos = game.enemy_x[row][(game.direction==1)?(ENEMY_COLUMNS-(col+1)):col];
if (pos != DISABLED) {
//Check collision with player
if((game.enemy_row_y[row]+8 >= POS_PLAYER_Y && pos+8 >= game.player && pos < game.player+8) ||
game.enemy_row_y[row]+8 >= POS_PLAYER_Y+8) {
for(int row=0; row<ENEMY_ROWS; row++) {
game.enemy_row_y[row] = 10 + (40/ENEMY_ROWS)*row;
}
game.killed = true;
}
check_bunker(pos,game.enemy_row_y[row]+8,-2);
//Are we at the beginning or end? Direction change
if((pos <=0 && game.direction != 1) ||
(pos >=RESX-10 && game.direction == 1)){
game.direction = (game.direction==1)?-1:1;
for (int r = 0; r<ENEMY_ROWS; r++) {
game.enemy_row_y[r]+=game.level>=23?4:2;
}
return;
}
game.enemy_x[row][(game.direction==1)?(ENEMY_COLUMNS-(col+1)):col] += game.direction;
}
}
}
game.move = game.alive*2-1;
}
void draw_player() {
draw_sprite(TYPE_PLAYER, game.player, POS_PLAYER_Y);
}
void draw_ufo() {
if (game.ufo!=DISABLED)
draw_sprite(TYPE_UFO, game.ufo, POS_UFO_Y);
}
void draw_enemy() {
for (int row = 0; row<ENEMY_ROWS; row++) {
for (int col = 0; col<ENEMY_COLUMNS; col++) {
if (game.enemy_x[row][col] != DISABLED) {
draw_sprite(TYPE_ENEMY_C-row,game.enemy_x[row][col],game.enemy_row_y[row]);
}
}
}
}
void draw_bunker() {
for (int b=0; b<BUNKERS; b++) {
memcpy(lcdBuffer+(RESX*1+BUNKER_X[b]),game.bunker+b,BUNKER_WIDTH);
}
}
void draw_shots() {
if (game.shot_x != 255) {
for (int length=0; length<=5; length++) {
lcdSetPixel(game.shot_x, game.shot_y+length, true);
}
}
for (int col = 0; col < ENEMY_COLUMNS; col++) {
if (game.shots_x[col] != DISABLED) {
for (int length=0; length<=5; length++) {
lcdSetPixel(game.shots_x[col], game.shots_y[col]+length,true);
}
}
}
}
void draw_status() {
for (int p = 0; p<game.alive; p++){
lcdSetPixel(p+1,1,true);
}
}
void draw_sprite(char type, char x, char y) {
font = &Font_Invaders;
switch(type) {
case TYPE_PLAYER:
DoChar(x,y-1,'P');
break;
case TYPE_ENEMY_A:
DoChar(x,y-1,game.step?'a':'A');
break;
case TYPE_ENEMY_B:
DoChar(x,y-1,game.step?'b':'B');
break;
case TYPE_ENEMY_C:
DoChar(x,y-1,game.step?'c':'C');
break;
case TYPE_UFO:
DoChar(x,y-1,'U');
break;
}
}
void draw_score() {
font = &Font_7x8;
DoInt(0,0,game.score);
DoInt(RESX-8,0,game.rokets);
font = &Font_Invaders;
DoChar(RESX-16, 0, 'P');
}
void check_end() {
if (game.killed) {
game.rokets--;
delayms(500);
game.player = POS_PLAYER_X;
for(int col=0; col<ENEMY_COLUMNS; col++) {
game.shots_x[col] = DISABLED;
}
game.killed = false;
}
if (game.alive == 0) {
delayms(500);
game.level++;
init_game();
screen_level();
}
}

View File

@ -1,5 +1,5 @@
MEMORY
{
sram(rwx): ORIGIN = 0x10002000 - 2048, LENGTH = 2048
sram(rwx): ORIGIN = 0x10002000 - 2548, LENGTH = 2548
}
INCLUDE ram.ld

View File

@ -5,7 +5,6 @@
#include "lcd/render.h"
#include "lcd/display.h"
#include "lcd/allfonts.h"
#include "usetable.h"
#define FIXSIZE 25
@ -38,7 +37,8 @@ void ram(void) {
struct mb {
long rmin, rmax, imin, imax;
bool dirty, dup, ddown, dleft, dright;
bool dirty, dup, ddown, dleft, dright, clickmark;
int presscount, presslimitzin, presslimitzout, zoomlevel, maxzoomin, maxzoomout;
} mandel;
void mandelInit() {
@ -50,12 +50,19 @@ void mandelInit() {
mandel.rmax = fixpt(1);
mandel.imin = fixpt(-2);
mandel.imax = fixpt(2);
mandel.presscount = 0;
mandel.presslimitzin = 40;
mandel.presslimitzout = 30;
mandel.zoomlevel = 0;
mandel.maxzoomin = 65;
mandel.maxzoomout = -12;
mandel.dirty = true;
mandel.dup = false;
mandel.ddown = false;
mandel.dleft = false;
mandel.dright = false;
mandel.clickmark = false;
}
void mandelMove() {
@ -67,7 +74,7 @@ void mandelMove() {
char key = getInputRaw();
if (key == BTN_LEFT) {
if (key == BTN_LEFT) {
mandel.imax -=is;
mandel.imin -=is;
mandel.dleft = true;
@ -79,23 +86,41 @@ void mandelMove() {
mandel.rmax += rs;
mandel.rmin += rs;
mandel.ddown = true;
} else if (key == BTN_UP) {
mandel.rmax -= rs;
mandel.rmin -= rs;
mandel.dup = true;
} else if (key == (BTN_ENTER + BTN_UP)) {
} else if (key == BTN_UP) {
mandel.rmax -= rs;
mandel.rmin -= rs;
mandel.dup = true;
} else if (key == BTN_ENTER) {
if (mandel.presscount < mandel.presslimitzin) {
mandel.presscount = mandel.presscount + 1;
}
} else if (key == BTN_NONE) {
if(mandel.presscount > 0 ) {
mandel.presscount = mandel.presscount - 1;
mandel.clickmark = true;
}
if (mandel.presscount == 0 ) {
mandel.clickmark = false;
}
}
if (mandel.presscount > mandel.presslimitzout && mandel.clickmark && key == BTN_ENTER && mandel.zoomlevel >= mandel.maxzoomout) {
mandel.imin = mandel.imin - (mandel.imax-mandel.imin)/10;
mandel.imax = mandel.imax + (mandel.imax-mandel.imin)/10;
mandel.rmin = mandel.rmin -(mandel.rmax-mandel.rmin)/10;
mandel.rmax = mandel.rmax +(mandel.rmax-mandel.rmin)/10;
mandel.dirty = true;
delayms(10);
mandel.zoomlevel = mandel.zoomlevel - 1 ;
}
else if (mandel.presscount == mandel.presslimitzin && key == BTN_ENTER && mandel.zoomlevel <= mandel.maxzoomin ) {
mandel.imin = mandel.imin + (mandel.imax-mandel.imin)/10;
mandel.imax = mandel.imax - (mandel.imax-mandel.imin)/10;
mandel.rmin = mandel.rmin +(mandel.rmax-mandel.rmin)/10;
mandel.rmax = mandel.rmax -(mandel.rmax-mandel.rmin)/10;
mandel.dirty = true;
} else if (key == (BTN_ENTER + BTN_DOWN)) {
mandel.imin = mandel.imin - (mandel.imax-mandel.imin)/10;
mandel.imax = mandel.imax + (mandel.imax-mandel.imin)/10;
mandel.rmin = mandel.rmin -(mandel.rmax-mandel.rmin)/10;
mandel.rmax = mandel.rmax +(mandel.rmax-mandel.rmin)/10;
mandel.dirty = true;
}
delayms(10);
mandel.zoomlevel = mandel.zoomlevel + 1 ;
}
}
void mandelPixel(int x, int y) {

View File

@ -18,6 +18,8 @@ my @symb;
open(Q,"<","l0dable/EXPORTS") || die "$!";
while(<Q>){
chomp;s/\r$//;
next if /^#/;
next if /^\s*$/;
push @symb,$_;
};
close(Q);
@ -47,18 +49,18 @@ sub wanted {
open(F,"<",$_) || die;
while(<F>){
chomp;s/\r$//;
if(m!^[^(]* ([\w]+)\s*\(.*\);\s*(//.*)?$!){
if(m!^[^(]* ([\w]+)\s*\(.*\);\s*(//.*)?(/\*[^/]*\*/)?$!){
$id=$1;
s/$id/(*)/;
s/;//;
s!//.*!!;
$types{$id}="*($_)";
$files{$id}=$File::Find::name;
}elsif (m!^\s*extern\s[^(]* ([\w]+)\s*(\[\w*\]\s*)?;\s*(//.*)?$!){
}elsif (m!^\s*extern\s[^(]* ([\w]+)\s*(\[[^]]*\]\s*)?;\s*(//.*)?(/\*[^/]*\*/)?$!){
$id=$1;
s/extern //;
my $star="*";
if( s/\[\w*\]//){
if( s/\[.*\]//){
$star="";
};
s/$id/*/;

340
firmware/l0dable/recvcard.c Normal file
View File

@ -0,0 +1,340 @@
#include <sysinit.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include "basic/basic.h"
#include "lcd/render.h"
#include "lcd/allfonts.h"
#include "basic/ecc.h"
#include "funk/nrf24l01p.h"
#include "filesystem/ff.h"
#include "filesystem/diskio.h"
#include "funk/filetransfer.h"
#include "lcd/print.h"
#include <string.h>
#include "funk/nrf24l01p.h"
#include "funk/filetransfer.h"
#include "funk/rftransfer.h"
#include "basic/basic.h"
#include "basic/xxtea.h"
#include "filesystem/ff.h"
#include "lcd/print.h"
#include "usetable.h"
uint8_t mac[5] = {1,2,3,2,1};
void ram(void)
{
if( sendKeys() )
return;
char priv[42];
UINT readbytes;
FIL file;
if( f_open(&file, "priv.key", FA_OPEN_EXISTING|FA_READ) ){
return;
}
if( f_read(&file, priv, 41, &readbytes) || readbytes != 41 ){
return;
}
f_close(&file);
priv[41] = 0;
uint8_t done = 0;
uint8_t key;
uint8_t k1[16], k2[16], rx[4*NUMWORDS], ry[4*NUMWORDS];
while( !done ){
lcdClear();
lcdPrintln("Receiving file");
lcdPrintln("Down=Abort");
lcdRefresh();
key = getInput();
delayms(20);
if( key == BTN_DOWN ){
return -1;
}
if( receiveR(rx,ry) )
continue;
lcdPrintln("Creating key");
lcdRefresh();
ECIES_decryptkeygen(rx, ry, k1, k2, priv);
if( filetransfer_receive(mac,(uint32_t*)k1) < 0 )
continue;
lcdPrintln("Right=OK");
lcdPrintln("Left=Retry");
lcdPrintln("Down=Abort");
lcdRefresh();
while(1){
key = getInput();
delayms(20);
if( key == BTN_LEFT ){
break;
}else if( key == BTN_RIGHT ){
done = 1;
break;
}else if( key == BTN_DOWN ){
return -1;
}
}
}
}
void sendPublicKey(void)
{
uint8_t exp[2 + 4*NUMWORDS + 2];
char buf[42];
UINT readbytes;
FIL file;
if( f_open(&file, "pubx.key", FA_OPEN_EXISTING|FA_READ) ){
return;
}
if( f_read(&file, buf, 41, &readbytes) || readbytes != 41 ){
return;
}
f_close(&file);
buf[41] = 0;
exp[0] = 'P';
bitstr_parse_export((char*)exp+2, buf);
exp[1] = 'X';
nrf_snd_pkt_crc(32, exp);
delayms(10);
if( f_open(&file, "puby.key", FA_OPEN_EXISTING|FA_READ) ){
return;
}
if( f_read(&file, buf, 41, &readbytes) || readbytes != 41 ){
return;
}
f_close(&file);
buf[41] = 0;
exp[1] = 'Y';
bitstr_parse_export((char*)exp+2, buf);
nrf_snd_pkt_crc(32, exp);
delayms(10);
}
int receiveKey(uint8_t type, uint8_t *x, uint8_t *y)
{
uint8_t buf[32];
uint8_t n;
n = nrf_rcv_pkt_time(1000, 32, buf);
if( n == 32 && buf[0] == type && buf[1] == 'X' ){
for(int i=0; i<NUMWORDS*4; i++)
x[i] = buf[i+2];
n = nrf_rcv_pkt_time(100, 32, buf);
if( n == 32 && buf[0] ==type && buf[1] == 'Y' ){
for(int i=0; i<NUMWORDS*4; i++)
y[i] = buf[i+2];
return 0;
}
}
return -1;
}
int receiveR(uint8_t *rx, uint8_t *ry)
{
return receiveKey('R',rx,ry);
}
void sendMac(void)
{
uint8_t buf[32];
buf[0] = 'M';
buf[1] = 'C';
buf[2] = mac[0];
buf[3] = mac[1];
buf[4] = mac[2];
buf[5] = mac[3];
buf[6] = mac[4];
nrf_snd_pkt_crc(32, buf);
delayms(10);
}
int sendKeys(void)
{
uint8_t done = 0;
char key;
while( !done ){
lcdClear();
lcdPrintln("Sending PUBKEY");lcdRefresh();
sendPublicKey();
sendMac();
lcdPrintln("Done");
lcdPrintln("Right=OK");
lcdPrintln("Left=Retry");
lcdPrintln("Down=Abort");
lcdRefresh();
while(1){
key = getInput();
delayms(20);
if( key == BTN_LEFT ){
break;
}else if( key == BTN_RIGHT ){
done = 1;
break;
}else if( key == BTN_DOWN ){
return -1;
}
}
}
return 0;
}
int filetransfer_receive(uint8_t *mac, uint32_t const k[4])
{
uint8_t buf[MAXSIZE+1];
uint16_t size;
uint8_t n;
UINT written = 0;
FIL file;
FRESULT res;
//uint8_t macbuf[5];
//nrf_get_rx_max(0,5,macbuf);
uint8_t metadata[32];
//nrf_set_rx_mac(0, 32, 5, mac);
n = nrf_rcv_pkt_time_encr(3000, 32, metadata, k);
if( n != 32 )
return 1; //timeout
//nrf_set_rx_mac(0, 32, 5, macbuf);
//lcdPrintln("got meta"); lcdRefresh();
metadata[19] = 0; //enforce termination
size = (metadata[20] << 8) | metadata[21];
if( size > MAXSIZE ) {lcdPrintln("too big"); lcdRefresh(); while(1);}
if( size > MAXSIZE ) return 1; //file to big
//if(fileexists(metadata)) return 1; //file already exists
//lcdPrint("open"); lcdPrintln((const char*)metadata); lcdRefresh();
res = f_open(&file, (const char*)metadata, FA_OPEN_ALWAYS|FA_WRITE);
//lcdPrintln("file opened"); lcdRefresh();
if( res ) {lcdPrintln("res"); lcdPrint(f_get_rc_string(res)); lcdRefresh(); while(1);}
if( res )
return res;
uint16_t wordcount = (size+3)/4;
//nrf_set_rx_mac(0, 32, 5, mac);
//lcdPrintln("get file"); lcdRefresh();
int fres = rftransfer_receive(buf, wordcount*4, 1000);
if( fres == -1 ){
lcdPrintln("checksum wrong");
}else if( fres == -2 ){
lcdPrintln("timeout");
}else{
//lcdPrintln("got file");
}
lcdRefresh();
if( fres < 0 )
return 1;
//nrf_set_rx_mac(0, 32, 5, macbuf);
xxtea_decode_words((uint32_t *)buf, wordcount, k);
res = f_write(&file, buf, size, &written);
f_close(&file);
if( res )
return res;
if( written != size )
return 1; //error while writing
lcdClear();
lcdPrintln("Received"); lcdPrintln((const char*)metadata); lcdRefresh();
return 0;
}
#define MAXPACKET 32
int16_t rftransfer_receive(uint8_t *buffer, uint16_t maxlen, uint16_t timeout)
{
uint8_t buf[MAXPACKET];
uint8_t state = 0;
uint16_t pos = 0, seq = 0, size = 0, rand = 0, crc = 0;
int n,i;
unsigned int currentTick = systickGetTicks();
unsigned int startTick = currentTick;
while(systickGetTicks() < (startTick+timeout) ){//this fails if either overflows
n = nrf_rcv_pkt_time(1000, MAXPACKET, buf);
switch(state){
case 0:
if( n == 32 && buf[0] == 'L' ){
size = (buf[1] << 8) | buf[2];
rand = (buf[3] << 8) | buf[4];
seq = 0;
pos = 0;
if( size <= maxlen ){
//lcdClear();
//lcdPrint("got l="); lcdPrintInt(size);
//lcdPrintln(""); lcdRefresh();
state = 1;
}
}
break;
case 1:
if( n == 32 && buf[0] == 'D' && ((buf[3]<<8)|buf[4])==rand ){
//lcdPrint("got d"); lcdRefresh();
if( seq == ((buf[1]<<8)|buf[2]) ){
//lcdPrintln(" in seq"); lcdRefresh();
for(i=5; i<n-2 && pos<size; i++,pos++){
buffer[pos] = buf[i];
}
seq++;
}
}
if( pos == size ){
//lcdPrintln("got all"); lcdRefresh();
crc = crc16(buffer, size);
state = 2;
}
break;
case 2:
if( n == 32 && buf[0] == 'C' && ((buf[3]<<8)|buf[4])==rand){
//lcdPrint("got crc"); lcdRefresh();
if( crc == ((buf[1]<<8)|buf[2]) ){
//lcdPrintln(" ok"); lcdRefresh();
return size;
}else{
//lcdPrintln(" nok"); lcdRefresh();
return -1;
}
}
break;
};
}
//lcdPrintln("Timeout"); lcdRefresh();
return -2;
}
#if 0
int ECIES_decryptkeygen(uint8_t *rx, uint8_t *ry,
uint8_t k1[16], uint8_t k2[16], const char *privkey)
{
elem_t Rx, Ry, Zx, Zy;
exp_t d;
bitstr_import(Rx, (char*)rx);
bitstr_import(Ry, (char*)ry);
if (ECIES_embedded_public_key_validation(Rx, Ry) < 0)
return -1;
bitstr_parse(d, privkey);
point_copy(Zx, Zy, Rx, Ry);
point_mult(Zx, Zy, d);
point_double(Zx, Zy); /* cofactor h = 2 on B163 */
if (point_is_zero(Zx, Zy))
return -1;
ECIES_kdf((char*)k1,(char*) k2, Zx, Rx, Ry);
return 0;
}
#endif

284
firmware/l0dable/sendcard.c Normal file
View File

@ -0,0 +1,284 @@
#include <sysinit.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include "basic/basic.h"
#include "lcd/render.h"
#include "lcd/allfonts.h"
#include "basic/ecc.h"
#include "funk/nrf24l01p.h"
#include "filesystem/ff.h"
#include "filesystem/diskio.h"
#include "funk/filetransfer.h"
#include "lcd/print.h"
#include <string.h>
#include "funk/nrf24l01p.h"
#include "funk/filetransfer.h"
#include "funk/rftransfer.h"
#include "basic/basic.h"
#include "basic/xxtea.h"
#include "filesystem/ff.h"
#include "funk/rftransfer.h"
#include "funk/nrf24l01p.h"
#include <basic/basic.h>
#include <basic/random.h>
#include <core/systick/systick.h>
//#include <lcd/print.h>
#include "usetable.h"
//#include "lcd/print.h"
uint8_t mac[5] = {1,2,3,2,1};
void ram(void)
{
char file[13];
selectFile(file,"TXT");
sendFile(file);
}
void sendR(uint8_t *rx, uint8_t *ry)
{
uint8_t exp[2 + 4*NUMWORDS + 2];
exp[0] = 'R';
for(int i=0; i<4*NUMWORDS; i++)
exp[2+i] = rx[i];
exp[1] = 'X';
nrf_snd_pkt_crc(32, exp);
delayms(10);
exp[1] = 'Y';
for(int i=0; i<4*NUMWORDS; i++)
exp[2+i] = ry[i];
nrf_snd_pkt_crc(32, exp);
delayms(10);
}
int receiveKey(uint8_t type, uint8_t *x, uint8_t *y)
{
uint8_t buf[32];
uint8_t n;
n = nrf_rcv_pkt_time(1000, 32, buf);
if( n == 32 && buf[0] == type && buf[1] == 'X' ){
for(int i=0; i<NUMWORDS*4; i++)
x[i] = buf[i+2];
n = nrf_rcv_pkt_time(100, 32, buf);
if( n == 32 && buf[0] ==type && buf[1] == 'Y' ){
for(int i=0; i<NUMWORDS*4; i++)
y[i] = buf[i+2];
return 0;
}
}
return -1;
}
int receivePublicKey(uint8_t *px, uint8_t *py)
{
return receiveKey('P',px,py);
}
void sendMac(void)
{
uint8_t buf[32];
buf[0] = 'M';
buf[1] = 'C';
buf[2] = mac[0];
buf[3] = mac[1];
buf[4] = mac[2];
buf[5] = mac[3];
buf[6] = mac[4];
nrf_snd_pkt_crc(32, buf);
delayms(10);
}
int receiveMac(uint8_t *mac)
{
uint8_t buf[32];
uint8_t n;
n = nrf_rcv_pkt_time(100, 32, buf);
if( n == 32 && buf[0] == 'M' && buf[1] == 'C' ){
for(int i=0; i<5; i++)
mac[i] = buf[i+2];
return 0;
}
return -1;
}
int receiveKeys(uint8_t *px, uint8_t *py, uint8_t *mac)
{
uint8_t done = 0;
char key;
while( !done ){
lcdClear();
lcdPrintln("Recv. PUBKEY");
lcdPrintln("Down=Abort");
lcdRefresh();
key = getInput();
delayms(20);
if( key == BTN_DOWN ){
return -1;
}
if( receivePublicKey(px,py) )
continue;
if( receiveMac(mac) )
continue;
lcdClear();
lcdPrintln("Got PUBKEY"); lcdRefresh();
lcdPrintln("Hash:");
lcdPrintln("Right=OK");
lcdPrintln("Left=Retry");
lcdPrintln("Down=Abort");
lcdRefresh();
while(1){
key = getInput();
delayms(20);
if( key == BTN_LEFT ){
break;
}else if( key == BTN_RIGHT ){
done = 1;
break;
}else if( key == BTN_DOWN ){
return -1;
}
}
}
return 0;
}
void sendFile(char *filename)
{
uint8_t px[4*NUMWORDS];
uint8_t py[4*NUMWORDS];
uint8_t mac[5];
if( receiveKeys(px, py, mac) )
return;
uint8_t done = 0;
uint8_t key;
uint8_t k1[16], k2[16], rx[4*NUMWORDS], ry[4*NUMWORDS];
lcdClear();
lcdPrintln("Creating key"); lcdRefresh();
ECIES_encyptkeygen(px, py, k1, k2, rx, ry);
while( !done ){
lcdPrintln("Sending file");lcdRefresh();
sendR(rx,ry);
delayms(3000);
filetransfer_send((uint8_t*)filename, 0, mac, (uint32_t*)k1);
lcdPrintln("Done");
lcdPrintln("Right=OK");
lcdPrintln("Left=Retry");
lcdRefresh();
while(1){
key = getInput();
delayms(20);
if( key == BTN_LEFT ){
break;
}else if( key == BTN_RIGHT ){
done = 1;
break;
}
}
lcdClear();
}
}
//TODO: use a proper MAC to sign the message
int filetransfer_send(uint8_t *filename, uint16_t size,
uint8_t *mac, uint32_t const k[4])
{
uint8_t buf[MAXSIZE];
FIL file;
FRESULT res;
UINT readbytes;
if( size > MAXSIZE )
return 1; //File to big
res=f_open(&file, (const char*)filename, FA_OPEN_EXISTING|FA_READ);
if( res )
return res;
//res = f_read(&file, (char *)buf, size, &readbytes);
for(uint16_t i=0; i<MAXSIZE; i++)
buf[i] = 0;
res = f_read(&file, (char *)buf, MAXSIZE, &readbytes);
size = readbytes;
if( res )
return res;
if( size != readbytes)
return 1; //Error while reading
uint16_t wordcount = (size+3)/4;
//uint8_t macbuf[5];
uint8_t metadata[32];
if( strlen((char*)filename) < 20 )
strcpy((char*)metadata, (char*)filename);
else
return 1; //File name too long
metadata[20] = size >> 8;
metadata[21] = size & 0xFF;
//nrf_get_tx_max(5,macbuf);
//nrf_set_tx_mac(5, mac);
nrf_snd_pkt_crc_encr(32, metadata, k);
delayms(20);
xxtea_encode_words((uint32_t *)buf, wordcount, k);
rftransfer_send(wordcount*4, buf);
//nrf_set_tx_mac(5, macbuf);
return 0;
}
#define MAXPACKET 32
void rftransfer_send(uint16_t size, uint8_t *data)
{
uint8_t buf[MAXPACKET];
buf[0] = 'L';
buf[1] = size >> 8;
buf[2] = size & 0xFF;
uint16_t rand = getRandom() & 0xFFFF;
buf[3] = rand >> 8;
buf[4] = rand & 0xFF;
nrf_snd_pkt_crc(32,buf); //setup packet
delayms(20);
uint16_t index = 0;
uint8_t i;
uint16_t crc = crc16(data,size);
while(size){
buf[0] = 'D';
buf[1] = index >> 8;
buf[2] = index & 0xFF;
buf[3] = rand >> 8;
buf[4] = rand & 0xFF;
for(i=5; i<MAXPACKET-2 && size>0; i++,size--){
buf[i] = *data++;
}
index++;
nrf_snd_pkt_crc(32,buf); //data packet
delayms(20);
}
buf[0] = 'C';
buf[1] = crc >> 8;
buf[2] = crc & 0xFF;
buf[3] = rand >> 8;
buf[4] = rand & 0xFF;
nrf_snd_pkt_crc(32,buf); //setup packet
delayms(20);
}

View File

@ -0,0 +1,3 @@
size_t strlen(const char *s);
char * strcpy(char * restrict dst, const char * restrict src);
void * memcpy(void *dst, const void *src, size_t len);

View File

@ -2,10 +2,10 @@
#include <render.h>
// Local function: Get next nibble.
int ctr=0; // offset for next nibble
int hilo=0; // 0= high nibble next, 1=low nibble next
static int ctr=0; // offset for next nibble
static int hilo=0; // 0= high nibble next, 1=low nibble next
const uint8_t * data;
char gnn(){ // Get next nibble
static char gnn(){ // Get next nibble
static int byte;
int val;
if(hilo==1)

View File

@ -216,6 +216,7 @@ void lcdDisplay(void) {
lcd_deselect();
}
void lcdRefresh() __attribute__ ((weak, alias ("lcdDisplay")));
inline void lcdInvert(void) {
GLOBAL(lcdinvert)=!GLOBAL(lcdinvert);

View File

@ -53,9 +53,6 @@ void lcdClear(){
lcdFill(0);
};
void lcdRefresh(){
lcdDisplay();
};
void lcdMoveCrsr(signed int dx,signed int dy){
x+=dx;

View File

@ -36,7 +36,8 @@ void ram(void) {
struct mb {
long rmin, rmax, imin, imax;
bool dirty, dup, ddown, dleft, dright;
bool dirty, dup, ddown, dleft, dright, clickmark;
int count, limitZIn, limitZOut;
} mandel;
void mandelInit() {
@ -48,12 +49,16 @@ void mandelInit() {
mandel.rmax = fixpt(1);
mandel.imin = fixpt(-2);
mandel.imax = fixpt(2);
mandel.count = 0;
mandel.limitZIn = 40;
mandel.limitZOut = 30;
mandel.dirty = true;
mandel.dup = false;
mandel.ddown = false;
mandel.dleft = false;
mandel.dright = false;
mandel.clickmark = false;
}
void mandelMove() {
@ -65,7 +70,7 @@ void mandelMove() {
char key = getInputRaw();
if (key == BTN_LEFT) {
if (key == BTN_LEFT) {
mandel.imax -=is;
mandel.imin -=is;
mandel.dleft = true;
@ -77,23 +82,37 @@ void mandelMove() {
mandel.rmax += rs;
mandel.rmin += rs;
mandel.ddown = true;
} else if (key == BTN_UP) {
mandel.rmax -= rs;
mandel.rmin -= rs;
mandel.dup = true;
} else if (key == (BTN_ENTER + BTN_UP)) {
mandel.imin = mandel.imin + (mandel.imax-mandel.imin)/10;
mandel.imax = mandel.imax - (mandel.imax-mandel.imin)/10;
mandel.rmin = mandel.rmin +(mandel.rmax-mandel.rmin)/10;
mandel.rmax = mandel.rmax -(mandel.rmax-mandel.rmin)/10;
mandel.dirty = true;
} else if (key == (BTN_ENTER + BTN_DOWN)) {
mandel.imin = mandel.imin - (mandel.imax-mandel.imin)/10;
} else if (key == BTN_UP) {
mandel.rmax -= rs;
mandel.rmin -= rs;
mandel.dup = true;
} else if (key == BTN_ENTER) {
if (mandel.count < mandel.limitZIn) {
mandel.count = mandel.count + 1;
}
} else if (key == BTN_NONE) {
if(mandel.count > 0 ) {
mandel.count = mandel.count - 1;
mandel.clickmark = true;
}
if (mandel.count == 0 ) {
mandel.clickmark = false;
}
}
if (mandel.count > mandel.limitZOut && mandel.clickmark && key == BTN_ENTER) {
mandel.imin = mandel.imin - (mandel.imax-mandel.imin)/10;
mandel.imax = mandel.imax + (mandel.imax-mandel.imin)/10;
mandel.rmin = mandel.rmin -(mandel.rmax-mandel.rmin)/10;
mandel.rmax = mandel.rmax +(mandel.rmax-mandel.rmin)/10;
mandel.dirty = true;
}
if (mandel.count == mandel.limitZIn && key == BTN_ENTER) {
mandel.imin = mandel.imin + (mandel.imax-mandel.imin)/10;
mandel.imax = mandel.imax - (mandel.imax-mandel.imin)/10;
mandel.rmin = mandel.rmin +(mandel.rmax-mandel.rmin)/10;
mandel.rmax = mandel.rmax -(mandel.rmax-mandel.rmin)/10;
mandel.dirty = true;
}
}
void mandelPixel(int x, int y) {

View File

@ -1,15 +1,13 @@
#include <sysinit.h>
#include <string.h>
#include "basic/basic.h"
#include "basic/random.h"
#include "lcd/render.h"
#include "lcd/display.h"
#include "lcd/allfonts.h"
void ReinvokeISP(void);
void EnableWatchdog(uint32_t ms);
void delayms(uint32_t ms);
/**************************************************************************/
#define POS_PLAYER_Y 60
#define POS_PLAYER_X RESX/2-3
@ -20,11 +18,11 @@ void delayms(uint32_t ms);
#define UFO_PROB 1024
#define TYPE_PLAYER 1
#define TYPE_PLAYER 1
#define TYPE_ENEMY_A 3
#define TYPE_ENEMY_B 2
#define TYPE_ENEMY_C 4
#define TYPE_UFO 5
#define TYPE_UFO 5
#define BUNKERS 3
#define BUNKER_WIDTH 10
@ -52,16 +50,19 @@ struct gamestate {
char key;
void init_game();
void init_enemy();
void check_end();
void move_ufo();
void move_shot();
void move_shots();
void move_player();
void move_enemay();
void move_enemy();
void draw_score();
void draw_bunker();
void draw_player();
void draw_enemy();
void draw_shots();
void draw_sprite(char type, char x, char y);
void draw_ufo();
void screen_intro();
void screen_gameover();
@ -150,14 +151,20 @@ void init_game(void) {
game.shot_y = 0;
game.alive = ENEMY_ROWS*ENEMY_COLUMNS;
game.move = 0;
game.direction = -1;
game.lastcol = ENEMY_COLUMNS-1;
if (getRandom()%2 == 0) {
game.direction = -1;
game.lastcol = ENEMY_COLUMNS-1;
} else {
game.direction = 1;
game.lastcol = 0;
}
game.killed = 0;
game.step = false;
game.ufo = DISABLED;
game.score = 0;
init_enemy();
for (char col=0; col<ENEMY_COLUMNS; col++){
for (int col=0; col<ENEMY_COLUMNS; col++){
game.shots_x[col] = DISABLED;
}
@ -217,21 +224,8 @@ void move_shot() {
game.shot_x = DISABLED;
return;
}
//check for collision with bunker
// for (int b=0; b<BUNKERS; b++) {
// if (game.shot_x>BUNKER_X[BUNKERS-1-b] &&
// game.shot_x<BUNKER_X[BUNKERS-1-b]+BUNKER_WIDTH &&
// game.shot_y<RESY-8 &&
// game.shot_y>RESY-16) {
// int offset = BUNKER_WIDTH - (game.shot_x-BUNKER_X[BUNKERS-1-b]);
// if (game.bunker[b][offset]!=0) {
// game.bunker[b][offset]&=game.bunker[b][offset]<<1;
// game.shot_x=DISABLED;
// }
// }
// }
if (check_bunker(game.shot_x,game.shot_y-5,1 ))
if (check_bunker(game.shot_x,game.shot_y-5,1 ))
game.shot_x=DISABLED;
//check for collision with enemy, kill enemy if
@ -242,12 +236,13 @@ void move_shot() {
game.enemy_x[row][col]=DISABLED;
game.shot_x = DISABLED;
game.alive--;
game.score++;
game.score+=(3-row)*10;
return;
}
}
}
}
//check for collision with ufo
if (game.ufo != DISABLED &&
game.shot_x>game.ufo &&
@ -255,7 +250,7 @@ void move_shot() {
game.shot_y<8) {
game.ufo = DISABLED;
game.score += 5;
game.score += 50;
}
game.shot_y -= 2;
@ -265,10 +260,10 @@ void move_shot() {
void move_shots() {
for (char col = 0; col<ENEMY_COLUMNS; col++){
for (int col = 0; col<ENEMY_COLUMNS; col++){
//No shot, maybe generate
if (game.shots_x[col] == DISABLED) {
for (char row = 0; row<ENEMY_ROWS; row++) {
for (int row = 0; row<ENEMY_ROWS; row++) {
if (game.enemy_x[row][col] != DISABLED) {
if(getRandom()%(game.alive*20/((game.level/3)+1))==0) {
game.shots_x[col] = game.enemy_x[row][col]+5;
@ -288,20 +283,7 @@ void move_shots() {
if (check_bunker(game.shots_x[col],game.shots_y[col],-1))
game.shots_x[col]=DISABLED;
// for (int b=0; b<BUNKERS; b++) {
// if (game.shots_x[col]>BUNKER_X[BUNKERS-1-b] &&
// game.shots_x[col]<BUNKER_X[BUNKERS-1-b]+BUNKER_WIDTH &&
// game.shots_y[col]<RESY-8 &&
// game.shots_y[col]>RESY-16) {
// int offset = BUNKER_WIDTH - (game.shots_x[col]-BUNKER_X[BUNKERS-1-b])-1;
// if (game.bunker[b][offset]!=0) {
// game.bunker[b][offset]&=game.bunker[b][offset]>>1;
// game.shots_x[col]=DISABLED;
// }
// }
// }
//check for collision with player
//check for collision with player
if (game.shots_y[col] >= RESY-13 &&
game.shots_x[col] > game.player+1 &&
game.shots_x[col] < game.player+6) {
@ -363,25 +345,12 @@ void move_enemy() {
game.killed = true;
}
check_bunker(pos,game.enemy_row_y[row]+8,-2);
//check for collision with bunker, dirty
// for (int b=0; b<BUNKERS; b++) {
// if (pos>=BUNKER_X[BUNKERS-1-b] &&
// pos<=BUNKER_X[BUNKERS-1-b]+BUNKER_WIDTH &&
// game.enemy_row_y[row]+8<RESY-8 &&
// game.enemy_row_y[row]+8>RESY-16) {
// int offset = BUNKER_WIDTH - (pos-BUNKER_X[BUNKERS-1-b]);
// if (game.bunker[b][offset]!=0) {
// game.bunker[b][offset]&=game.bunker[b][offset]>>2;
// }
// }
// }
//Are we at the beginning or end? Direction change
if((pos <=0 && game.direction != 1) ||
(pos >=RESX-10 && game.direction == 1)){
game.direction = (game.direction==1)?-1:1;
for (char r = 0; r<ENEMY_ROWS; r++) {
for (int r = 0; r<ENEMY_ROWS; r++) {
game.enemy_row_y[r]+=game.level>=23?4:2;
}
return;
@ -395,7 +364,6 @@ void move_enemy() {
}
void draw_player() {
//draw_sprite(50, 20);
draw_sprite(TYPE_PLAYER, game.player, POS_PLAYER_Y);
}
@ -427,7 +395,7 @@ void draw_shots() {
}
}
for (char col = 0; col < ENEMY_COLUMNS; col++) {
for (int col = 0; col < ENEMY_COLUMNS; col++) {
if (game.shots_x[col] != DISABLED) {
for (int length=0; length<=5; length++) {
lcdSetPixel(game.shots_x[col], game.shots_y[col]+length,true);