diff --git a/firmware/Makefile b/firmware/Makefile index 732c824..349b4ff 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -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 diff --git a/firmware/Makefile.inc b/firmware/Makefile.inc index 855d011..69097c0 100644 --- a/firmware/Makefile.inc +++ b/firmware/Makefile.inc @@ -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. diff --git a/firmware/SERIAL_DOKU b/firmware/SERIAL_DOKU index 4c28ee7..d67b459 100644 --- a/firmware/SERIAL_DOKU +++ b/firmware/SERIAL_DOKU @@ -4,6 +4,8 @@ Ck: set encryptionkey Ct: set txmac Cc: set channel Ct: set rxmac(0) +Cl: set rxlens +Ce: sec encrpytion on/off s : send packet s+ : send packet 10 times diff --git a/firmware/applications/final.c b/firmware/applications/final.c index bedce8c..cce4e3b 100644 --- a/firmware/applications/final.c +++ b/firmware/applications/final.c @@ -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){ diff --git a/firmware/applications/final/config.c b/firmware/applications/final/config.c index a0054d7..444c8ea 100644 --- a/firmware/applications/final/config.c +++ b/firmware/applications/final/config.c @@ -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(); diff --git a/firmware/applications/final/flame.c b/firmware/applications/final/flame.c index 3943458..a9ce06a 100644 --- a/firmware/applications/final/flame.c +++ b/firmware/applications/final/flame.c @@ -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; diff --git a/firmware/applications/final/mesh.c b/firmware/applications/final/mesh.c index 84b4784..82ffc73 100644 --- a/firmware/applications/final/mesh.c +++ b/firmware/applications/final/mesh.c @@ -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"); - - 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; diff --git a/firmware/applications/final/util.c b/firmware/applications/final/util.c index fb4fbe7..aa99c61 100644 --- a/firmware/applications/final/util.c +++ b/firmware/applications/final/util.c @@ -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){ diff --git a/firmware/applications/flame.c b/firmware/applications/flame.c index 2831505..80bd060 100644 --- a/firmware/applications/flame.c +++ b/firmware/applications/flame.c @@ -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; diff --git a/firmware/applications/mkfirmware.pl b/firmware/applications/mkfirmware.pl index 2c465b6..5817b67 100755 --- a/firmware/applications/mkfirmware.pl +++ b/firmware/applications/mkfirmware.pl @@ -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$_();!; }; diff --git a/firmware/applications/s.c b/firmware/applications/s.c index e6d4910..f047c80 100644 --- a/firmware/applications/s.c +++ b/firmware/applications/s.c @@ -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; +}; diff --git a/firmware/applications/serial/serial.c b/firmware/applications/serial/serial.c index f27dada..287001a 100644 --- a/firmware/applications/serial/serial.c +++ b/firmware/applications/serial/serial.c @@ -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); diff --git a/firmware/basic/ecc.h b/firmware/basic/ecc.h index d946cd3..05b1d3b 100644 --- a/firmware/basic/ecc.h +++ b/firmware/basic/ecc.h @@ -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)) diff --git a/firmware/basic/keyin.c b/firmware/basic/keyin.c index a895a0f..cc4cecb 100644 --- a/firmware/basic/keyin.c +++ b/firmware/basic/keyin.c @@ -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) diff --git a/firmware/basic/random.h b/firmware/basic/random.h index 3ff72db..87c640e 100644 --- a/firmware/basic/random.h +++ b/firmware/basic/random.h @@ -1,6 +1,6 @@ #ifndef _RANDOM_H_ #define _RANDOM_H_ - +#include void randomInit(void); uint32_t getRandom(void); diff --git a/firmware/basic/stringin.c b/firmware/basic/stringin.c index cce6e3f..0e5bfa5 100644 --- a/firmware/basic/stringin.c +++ b/firmware/basic/stringin.c @@ -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--; diff --git a/firmware/basic/xxtea.h b/firmware/basic/xxtea.h index da93394..1d0ffe0 100644 --- a/firmware/basic/xxtea.h +++ b/firmware/basic/xxtea.h @@ -1,8 +1,8 @@ #ifndef _XXTEA_H_ #define _XXTEA_H_ +#include -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]); diff --git a/firmware/filesystem/execute.c b/firmware/filesystem/execute.c index 9b0aa91..3ae2a86 100644 --- a/firmware/filesystem/execute.c +++ b/firmware/filesystem/execute.c @@ -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); diff --git a/firmware/funk/mesh.c b/firmware/funk/mesh.c index e69a798..e15656d 100644 --- a/firmware/funk/mesh.c +++ b/firmware/funk/mesh.c @@ -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]; }; diff --git a/firmware/funk/nrf24l01p.c b/firmware/funk/nrf24l01p.c index 8c3c464..383df03 100644 --- a/firmware/funk/nrf24l01p.c +++ b/firmware/funk/nrf24l01p.c @@ -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; }; diff --git a/firmware/l0dable/EXPORTS b/firmware/l0dable/EXPORTS index 05a3962..c25d631 100644 --- a/firmware/l0dable/EXPORTS +++ b/firmware/l0dable/EXPORTS @@ -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 diff --git a/firmware/l0dable/debug.c b/firmware/l0dable/debug.c index 0ba5381..4686da0 100644 --- a/firmware/l0dable/debug.c +++ b/firmware/l0dable/debug.c @@ -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(); } diff --git a/firmware/l0dable/invaders.c b/firmware/l0dable/invaders.c new file mode 100644 index 0000000..8fef54c --- /dev/null +++ b/firmware/l0dable/invaders.c @@ -0,0 +1,491 @@ +#include +#include + +#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; colBUNKER_X[BUNKERS-1-b] && + xposRESY-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; + 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= game.shot_y && game.enemy_row_y[row]+6 < game.shot_y+7) { + for(int col = 0; col= 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= 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=RESX-10 && game.direction == 1)){ + game.direction = (game.direction==1)?-1:1; + for (int r = 0; r=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 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) { diff --git a/firmware/l0dable/mktable.pl b/firmware/l0dable/mktable.pl index cc96e87..ad54420 100755 --- a/firmware/l0dable/mktable.pl +++ b/firmware/l0dable/mktable.pl @@ -18,6 +18,8 @@ my @symb; open(Q,"<","l0dable/EXPORTS") || die "$!"; while(){ chomp;s/\r$//; + next if /^#/; + next if /^\s*$/; push @symb,$_; }; close(Q); @@ -47,18 +49,18 @@ sub wanted { open(F,"<",$_) || die; while(){ 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/*/; diff --git a/firmware/l0dable/recvcard.c b/firmware/l0dable/recvcard.c new file mode 100644 index 0000000..d19da8d --- /dev/null +++ b/firmware/l0dable/recvcard.c @@ -0,0 +1,340 @@ +#include +#include +#include +#include +#include +#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 +#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 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 +#include +#include +#include +#include +#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 +#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 +#include +#include +//#include + +#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 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> 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; i0; 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); +} + diff --git a/firmware/l0dable/system-include-hack.h b/firmware/l0dable/system-include-hack.h new file mode 100644 index 0000000..4f8f217 --- /dev/null +++ b/firmware/l0dable/system-include-hack.h @@ -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); diff --git a/firmware/lcd/decoder.c b/firmware/lcd/decoder.c index 7073ee4..539b8d2 100644 --- a/firmware/lcd/decoder.c +++ b/firmware/lcd/decoder.c @@ -2,10 +2,10 @@ #include // 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) diff --git a/firmware/lcd/display.c b/firmware/lcd/display.c index 5922e8e..d3fb731 100644 --- a/firmware/lcd/display.c +++ b/firmware/lcd/display.c @@ -216,6 +216,7 @@ void lcdDisplay(void) { lcd_deselect(); } +void lcdRefresh() __attribute__ ((weak, alias ("lcdDisplay"))); inline void lcdInvert(void) { GLOBAL(lcdinvert)=!GLOBAL(lcdinvert); diff --git a/firmware/lcd/print.c b/firmware/lcd/print.c index 6f6ac75..46adf84 100644 --- a/firmware/lcd/print.c +++ b/firmware/lcd/print.c @@ -53,9 +53,6 @@ void lcdClear(){ lcdFill(0); }; -void lcdRefresh(){ - lcdDisplay(); -}; void lcdMoveCrsr(signed int dx,signed int dy){ x+=dx; diff --git a/firmware/loadable/mandelbrot.c b/firmware/loadable/mandelbrot.c index 2d9ae16..f66b691 100644 --- a/firmware/loadable/mandelbrot.c +++ b/firmware/loadable/mandelbrot.c @@ -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) { diff --git a/firmware/loadable/spaceinvaders.c b/firmware/loadable/spaceinvaders.c index c4f6457..fbcf38a 100644 --- a/firmware/loadable/spaceinvaders.c +++ b/firmware/loadable/spaceinvaders.c @@ -1,15 +1,13 @@ #include +#include #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; colBUNKER_X[BUNKERS-1-b] && -// game.shot_xRESY-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; colBUNKER_X[BUNKERS-1-b] && -// game.shots_x[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=BUNKER_X[BUNKERS-1-b] && - // pos<=BUNKER_X[BUNKERS-1-b]+BUNKER_WIDTH && - // game.enemy_row_y[row]+8RESY-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=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);