Skip to content

Commit

Permalink
HastyUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
Blue-Crescent committed Feb 15, 2024
1 parent c2f70f0 commit 357a1f6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 47 deletions.
45 changes: 16 additions & 29 deletions src/JJYReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,14 @@ time_t JJYReceiver::getTime() {
switch(state){
case RECEIVE: // Intermediate update (1st receive update)
if(timeavailable == -1) return -1;
timeavailable = -1;
switch(reliability){
case LENGTHOK:
if(mode == REALTIME && state == UNKNOWN){
globaltime = updateTimeInfo(jjydata,timeavailable,1);
}
break;
case MINPARITYOK:
if(mode == REALTIME && state == UNKNOWN){
globaltime = updateTimeInfo(jjydata,timeavailable,1);
}
case HOURPARITYOK:
if(mode == REALTIME && state == UNKNOWN){
globaltime = updateTimeInfo(jjydata,timeavailable,1);
}
break;
}
if(mode == REALTIME && state == UNKNOWN && jjystate == JJY_WEEK && jjypayloadlen[JJY_WEEK] == 0){ // Yearの反映
globaltime = updateTimeInfo(jjydata,timeavailable,1);
if(mode == HASTYUPDATE){
hastyupdate();
}
timeavailable = -1;
return -1;
case TIMEVALID:
globaltime = updateTimeInfo(last_jjydata,0,1);
reliability = NONFIRST;
state = TIMETICK;
received_time = globaltime;
}
Expand Down Expand Up @@ -149,12 +134,10 @@ void JJYReceiver::delta_tick(){
rcvcnt = (rcvcnt + 1) % VERIFYLOOP;
if(lencheck(jjypayloadlen)){ // Payload Length check
settime(rcvcnt);
if(minCheck(rcvcnt) == false) break;
if(hourCheck(rcvcnt) == false) break;
if(!minCheck(rcvcnt)) break;
if(hourCheck(rcvcnt)) break;
timeCheck();
timeavailable = rcvcnt;
reliability = static_cast<RELIABILITY>((reliability < LENGTHOK) ? LENGTHOK : reliability);
DEBUG_PRINTLN("LENGTH OK") ;
}
#ifdef DEBUG_BUILD
debug3();
Expand All @@ -171,11 +154,9 @@ void JJYReceiver::delta_tick(){
settime(rcvcnt);
if(minCheck(rcvcnt)){ // Parity check passed. Time might be valid.
timeavailable = rcvcnt;
reliability = static_cast<RELIABILITY>((reliability < LENGTHOK) ? MINPARITYOK : reliability);
}
if(hourCheck(rcvcnt)){ // Parity check passed. Time might be valid.
timeavailable = rcvcnt;
reliability = static_cast<RELIABILITY>((reliability < LENGTHOK) ? HOURPARITYOK : reliability);
}
}
DEBUG_PRINT("P");
Expand Down Expand Up @@ -272,6 +253,11 @@ void JJYReceiver::stop(){
// ***********************************************************************************************
// NON SPECIFIC FUNCTION
// ***********************************************************************************************
void JJYReceiver::rotateArray(int8_t shift, uint16_t* array, uint8_t size) {
for (uint8_t i = 0; i < size; i++) {
array[i] = (array[i] + shift + size) % size;
}
}

//timeinfo.tm_yday = // Day of the year is not implmented in Arduino time.h
void JJYReceiver::calculateDate(uint16_t year, uint8_t dayOfYear,volatile uint8_t *month,volatile uint8_t *day) {
Expand All @@ -288,13 +274,14 @@ void JJYReceiver::calculateDate(uint16_t year, uint8_t dayOfYear,volatile uint8_
*day = dayOfYear;
}

bool JJYReceiver::isEvenParity(uint8_t d) {
uint8_t JJYReceiver::isEvenParity(uint8_t d) {
d ^= d >> 4; // Half bits EXOR
d ^= d >> 2; // Half bits EXOR
d ^= d >> 1; // Half bits EXOR
return !(d & 1);
return (uint8_t)!(d & 1);
}


int JJYReceiver::distance(const volatile uint8_t* arr1,volatile uint8_t* arr2, int size) {
int hammingDistance = 0;
uint8_t temp;
Expand Down Expand Up @@ -349,8 +336,8 @@ void JJYReceiver::debug(){
}
DEBUG_PRINT(":");
switch(state){
case NEVER:
DEBUG_PRINT("NEVER");
case INIT:
DEBUG_PRINT("INIT");
break;
case RECEIVE:
DEBUG_PRINT("RECEIVE");
Expand Down
59 changes: 41 additions & 18 deletions src/JJYReceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ typedef union {
} JJYData;

class JJYReceiver {
enum MODE {REALTIME,NORMAL,CONSERVATIVE};
enum STATE {NEVER,RECEIVE,TIMEVALID,TIMETICK};
enum RELIABILITY {UNKNOWN=-1,MINPARITYOK,HOURPARITYOK,LENGTHOK,DATETIMEVALID};
enum MODE {HASTYUPDATE,ONETIME,CONSERVATIVE};
enum STATE {INIT,RECEIVE,TIMEVALID,TIMETICK};
enum RELIABILITY {NONFIRST=-2,UNKNOWN=0x0,MINPARITYOK=0x1,HOURPARITYOK=0x2,LENGTHOK=0x4}; // UNKNOWN = 1st reception, NONFIRST = twice~ reception
enum JJYSTATE {JJY_INIT=-1,JJY_MIN=0,JJY_HOUR=1,JJY_DOYH=2,JJY_DOYL=3,JJY_YEAR=4,JJY_WEEK=5};

public:
volatile uint8_t jjypayloadlen[6] = {0,0,0,0,0,0};
JJYData jjydata[VERIFYLOOP];
JJYData last_jjydata[1];
volatile enum STATE state = NEVER;
volatile enum MODE mode = NORMAL;
volatile enum STATE state = INIT;
volatile enum MODE mode = HASTYUPDATE;

volatile enum JJYSTATE jjystate = JJY_INIT;
volatile uint8_t jjyposition= 0;
Expand Down Expand Up @@ -134,7 +134,8 @@ class JJYReceiver {
time_t getTime();
time_t get_time();
time_t get_time(uint8_t index);
bool isEvenParity(uint8_t d);
uint8_t isEvenParity(uint8_t d);
void rotateArray(int8_t shift, uint16_t* array, uint8_t size);
void calculateDate(uint16_t parity, uint8_t dayOfYear,volatile uint8_t *month,volatile uint8_t *day);
int distance(const volatile uint8_t* arr1,volatile uint8_t* arr2, int size);
int max_of_three(uint8_t a, uint8_t b, uint8_t c);
Expand Down Expand Up @@ -169,6 +170,7 @@ class JJYReceiver {
}

void init(){
reliability = UNKNOWN;
state = RECEIVE;
clear(sampling,N);
jjydata[0].bits.hour = 25;
Expand Down Expand Up @@ -209,31 +211,36 @@ class JJYReceiver {
return false;
}


bool minCheck(uint8_t index){
if (jjypayloadlen[JJY_MIN] != 8) {
DEBUG_PRINT(" minLEN NG ");
return false;
}
bool min = (jjydata[index].bits.PA2_m == isEvenParity((uint8_t)jjydata[index].bits.min));
if(min){
DEBUG_PRINT(" min OK")
bool parity = isEvenParity(jjydata[index].bits.min);
if(parity){
DEBUG_PRINT(" min OK ");
}else{
DEBUG_PRINT(" min NG")
DEBUG_PRINT(" min NG ");
DEBUG_PRINT(parity);
}
return min;
reliability = static_cast<RELIABILITY>(reliability | MINPARITYOK);
return jjydata[index].bits.PA2_m == parity;
}
bool hourCheck(uint8_t index){
if (jjypayloadlen[JJY_HOUR] != 9) {
DEBUG_PRINT(" hourLEN NG ");
return false;
}
bool hour = (jjydata[index].bits.PA1_h == isEvenParity((uint8_t)(0x6F & jjydata[index].bits.hour)));
if(hour){
DEBUG_PRINT("hour OK")
bool parity = isEvenParity(jjydata[index].bits.hour);
if(parity){
DEBUG_PRINT(" hour OK ");
}else{
DEBUG_PRINT("hour NG")
DEBUG_PRINT(" hour NG ");
DEBUG_PRINT(parity);
}
reliability = static_cast<RELIABILITY>(reliability | HOURPARITYOK);
DEBUG_PRINTLN("");
return hour;
return jjydata[index].bits.PA1_h == parity;
}
bool lencheck(volatile uint8_t* arr) {
if (arr[0] != 8) {
Expand All @@ -244,9 +251,25 @@ class JJYReceiver {
return false;
}
}
reliability = static_cast<RELIABILITY>(reliability | LENGTHOK);
return true;
}

void hastyupdate(){
DEBUG_PRINT("HASTY:"); DEBUG_PRINT(reliability); DEBUG_PRINT(" "); DEBUG_PRINT(jjystate); DEBUG_PRINT(" "); DEBUG_PRINT(timeavailable);
if(reliability == NONFIRST) return;
if(reliability > 0){
switch(jjystate){
case JJY_YEAR:
globaltime = updateTimeInfo(jjydata,timeavailable,1);
DEBUG_PRINTLN("HASTY UPDATE");
break;
}
}
if(jjystate == JJY_WEEK && jjypayloadlen[JJY_YEAR] == 9){ // Yearの反映
globaltime = updateTimeInfo(jjydata,timeavailable,1);
DEBUG_PRINTLN("HASTY UPDATE");
}
}
};
#endif

Expand Down

0 comments on commit 357a1f6

Please sign in to comment.