Skip to content

Commit

Permalink
データ長のチェック導入
Browse files Browse the repository at this point in the history
  • Loading branch information
Blue-Crescent committed Feb 9, 2024
1 parent 941078c commit d3cb7fc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 44 deletions.
38 changes: 13 additions & 25 deletions src/JJYReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ JJYReceiver::clear(uint8_t* sampling, int length){
}
}


int JJYReceiver::shift_in(uint8_t data, uint8_t* sampling, int length){
uint8_t carry;
for (int i = 0; i < length; i++) {
Expand All @@ -77,8 +78,8 @@ time_t JJYReceiver::getTime() {
if( diff1 <= 2 ) reliability++;
if( diff2 <= 2 ) reliability++;
if( diff3 <= 2 ) reliability++;

if( reliability >= 3){
DEBUG_PRINT("RELIABILITY:"); DEBUG_PRINTLN(matchcnt);
if( reliability >= 1){
power(false);
if(state != TIMEVALID)
globaltime = localtime[0];
Expand Down Expand Up @@ -129,19 +130,20 @@ JJYReceiver::delta_tick(){
case 2: // PM
markercount++;
if(markercount==2){
settime(rcvcnt);
getTime();
//timeinfo.tm_sec = 1;
//localtime[rcvcnt]= mktime(&timeinfo);
//if(state == TIMEVALID){
// rotateArray((jjypayloadcnt),jjypayload,6);
if(settime(rcvcnt)){
rcvcnt = (rcvcnt + 1) % VERIFYLOOP;
getTime();
}else{
rotateArray((jjypayloadcnt),jjypayload,6);
}
// rotateArray((jjypayloadcnt),testarray,6);
// for(uint8_t i; i<6; i++){
// update_time(jjystate);
// }
//}
//debug3();
rcvcnt = (rcvcnt + 1) % VERIFYLOOP;
#ifdef DEBUG_BUILD
debug3();
#endif
jjypayloadcnt=0;
jjystate = JJY_MIN;
for (uint8_t i = 0; i < 6; i++){
Expand Down Expand Up @@ -261,19 +263,6 @@ int JJYReceiver::calculateDate(uint16_t year, uint8_t dayOfYear, uint8_t *month,
*day = dayOfYear;
//(*month)++;
}
bool JJYReceiver::calculateParity(uint8_t value, uint8_t bitLength, uint8_t expectedParity) {
byte count = 0;
for (uint8_t i = 0; i < bitLength; i++) {
if (value & (1 << i)) {
count++;
}
}
if((count % 2 == 0 ? 0 : 1) == expectedParity){ // Parity OK
return 0;
}else{// Parity Error
return 1;
}
}

// ***********************************************************************************************
// DEBUG FUNCTION
Expand Down Expand Up @@ -387,8 +376,7 @@ int JJYReceiver::debug4(){
marker = (rcvcnt == 2) ? "*" : " ";
DEBUG_PRINT(marker + str); // Print current localtime.
DEBUG_PRINT(" =>"); // Print current localtime.
time_t resulttime = getTime();
str = String(ctime(&resulttime));
str = String(ctime(&globaltime));
DEBUG_PRINT(str); // Print current localtime.
}
#endif
51 changes: 32 additions & 19 deletions src/JJYReceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <Arduino.h>
#include <stdint.h>

#define DEBUG_BUILD
//#define DEBUG_BUILD

#ifdef DEBUG_BUILD
#include <SoftwareSerial.h>
Expand All @@ -20,7 +20,6 @@ extern SoftwareSerial debugSerial;
#endif

#define N 13
//enum STATE {INIT,BITSYNC,RECEIVE,DECODE,TIME,PACKETFORMED,SECSYNCED};
enum STATE {INIT,RECEIVE,DATAVALID,TIMEVALID,STOP};
enum JJYSTATE {JJY_INIT=-1,JJY_MIN=0,JJY_HOUR=1,JJY_DOYH=2,JJY_DOYL=3,JJY_YEAR=4,JJY_WEEK=5};
typedef union {
Expand Down Expand Up @@ -119,23 +118,26 @@ class JJYReceiver {
#endif

private:
void settime(uint8_t index){
jjydata[index].bits.year =(uint8_t) 0x00FF & jjypayload[JJY_YEAR];
jjydata[index].bits.doyh =(uint16_t) 0x007F & jjypayload[JJY_DOYH];
jjydata[index].bits.doyl =(uint8_t) ((0x01E0 & jjypayload[JJY_DOYL]) >> 5);
jjydata[index].bits.hour =(uint16_t) 0x006F & jjypayload[JJY_HOUR];
jjydata[index].bits.min =(uint8_t) 0x00FF & jjypayload[JJY_MIN];

uint16_t year = (((jjydata[index].bits.year & 0xf0) >> 4) * 10 + (jjydata[index].bits.year & 0x0f)) + 2000;
timeinfo.tm_year = year - 1900; //
//timeinfo.tm_yday = // Day of the year is not implmented in Arduino time.h
uint16_t yday = ((((jjydata[index].bits.doyh >> 5) & 0x0002)) * 100) + (((jjydata[index].bits.doyh & 0x000f)) * 10) + jjydata[index].bits.doyl;
calculateDate(year, yday ,&timeinfo.tm_mon, &timeinfo.tm_mday);
timeinfo.tm_hour = ((jjydata[index].bits.hour >> 5) & 0x3) * 10 + (jjydata[index].bits.hour & 0x0f) ; //
timeinfo.tm_min = ((jjydata[index].bits.min >> 5) & 0x7) * 10 + (jjydata[index].bits.min & 0x0f) + 1; //
timeinfo.tm_sec = 1; //

localtime[index]= mktime(&timeinfo);
bool settime(uint8_t index){
if(lencheck(jjypayloadlen)){
jjydata[index].bits.year =(uint8_t) 0x00FF & jjypayload[JJY_YEAR];
jjydata[index].bits.doyh =(uint16_t) 0x007F & jjypayload[JJY_DOYH];
jjydata[index].bits.doyl =(uint8_t) ((0x01E0 & jjypayload[JJY_DOYL]) >> 5);
jjydata[index].bits.hour =(uint16_t) 0x006F & jjypayload[JJY_HOUR];
jjydata[index].bits.min =(uint8_t) 0x00FF & jjypayload[JJY_MIN];

uint16_t year = (((jjydata[index].bits.year & 0xf0) >> 4) * 10 + (jjydata[index].bits.year & 0x0f)) + 2000;
timeinfo.tm_year = year - 1900; //
//timeinfo.tm_yday = // Day of the year is not implmented in Arduino time.h
uint16_t yday = ((((jjydata[index].bits.doyh >> 5) & 0x0002)) * 100) + (((jjydata[index].bits.doyh & 0x000f)) * 10) + jjydata[index].bits.doyl;
calculateDate(year, yday ,&timeinfo.tm_mon, &timeinfo.tm_mday);
timeinfo.tm_hour = ((jjydata[index].bits.hour >> 5) & 0x3) * 10 + (jjydata[index].bits.hour & 0x0f) ; //
timeinfo.tm_min = ((jjydata[index].bits.min >> 5) & 0x7) * 10 + (jjydata[index].bits.min & 0x0f) + 1; //
timeinfo.tm_sec = 2; //
localtime[index]= mktime(&timeinfo);
return true;
}
return false;
}
void init(){
state = RECEIVE;
Expand All @@ -144,6 +146,17 @@ class JJYReceiver {
}
power(true);
}
bool lencheck(uint8_t arr[]) {
if (arr[0] != 8) {
return false;
}
for (int i = 1; i < 5; i++) { // except WEEK
if (arr[i] != 9) {
return false;
}
}
return true;
}

};
#endif
Expand Down

0 comments on commit d3cb7fc

Please sign in to comment.