#include #include #include #include "basic/basic.h" #include "basic/byteorder.h" #include "lcd/lcd.h" #include "lcd/print.h" #include "funk/nrf24l01p.h" #include #define MESH_CHANNEL 85 #define MESH_MAC "MESHB" /**************************************************************************/ uint32_t const meshkey[4] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; #define MESHBUFSIZE 10 #define MESHPKTSIZE 32 typedef struct { uint8_t pkt[32]; char flags; } MPKT; MPKT meshbuffer[MESHBUFSIZE]; #define MF_FREE (0) #define MF_USED (1<<0) time_t _timet=0; // Timezones suck. Currently we only do it all in localtime. // I know it's broken. Sorry time_t getSeconds(void){ return _timet+(getTimer()*SYSTICKSPEED/1000); }; // ********************************************************************** void m_init(void){ nrf_init(); struct NRF_CFG config = { .channel= MESH_CHANNEL, .txmac= MESH_MAC, .nrmacs=1, .mac0= MESH_MAC, .maclen ="\x20", // XXX: MESHPKTSIZE }; nrf_config_set(&config); for(int i=0;itm_sec = dayclock % 60; timep->tm_min = (dayclock % 3600) / 60; timep->tm_hour = dayclock / 3600; timep->tm_wday = (dayno + 4) % 7; /* day 0 was a thursday */ while (dayno >= YEARSIZE(year)) { dayno -= YEARSIZE(year); year++; } timep->tm_year = year - YEAR0; timep->tm_yday = dayno; timep->tm_mon = 0; while (dayno >= _ytab[LEAPYEAR(year)][timep->tm_mon]) { dayno -= _ytab[LEAPYEAR(year)][timep->tm_mon]; timep->tm_mon++; } timep->tm_mday = dayno + 1; timep->tm_isdst = 0; return timep; } void m_recv(void){ __attribute__ ((aligned (4))) uint8_t buf[32]; int len; int recvend=5000/SYSTICKSPEED+getTimer(); m_cleanup(); nrf_rcv_pkt_start(); getInputWaitRelease(); do{ len=nrf_rcv_pkt_poll_dec(sizeof(buf),buf,meshkey); // Receive if(len<=0){ delayms_power(10); continue; }; int i; for(i=0;i_timet) // Do not live in the past. _timet = toff; lcdPrintln("Got T"); lcdPrintInt(getSeconds());lcdNl(); }else if (buf[0]>='A' && buf[0] <'T'){ // Truncate ascii packets. meshbuffer[i].pkt[MESHPKTSIZE-3]=0; }; lcdRefresh(); }while(getTimer()tm_hour); lcdPrint(":"); lcdPrintInt(tm->tm_min); lcdPrint(":"); lcdPrintInt(tm->tm_sec); lcdNl(); lcdPrintInt(tm->tm_mday); lcdPrint("."); lcdPrintInt(tm->tm_mon+1); lcdPrint("."); lcdPrintInt(tm->tm_year+YEAR0); lcdNl(); lcdRefresh(); delayms(50); }while ((getInputRaw())==BTN_NONE); }; void m_send(void){ int ctr=0; __attribute__ ((aligned (4))) uint8_t buf[32]; int status; lcdClear(); // Update [T]ime packet uint32touint8p(getSeconds(),meshbuffer[0].pkt+2); for (int i=0;i