Skip to content

Commit

Permalink
信頼度導入
Browse files Browse the repository at this point in the history
  • Loading branch information
Blue-Crescent committed Feb 14, 2024
1 parent 0ec7418 commit 9bd229d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 60 deletions.
83 changes: 33 additions & 50 deletions src/JJYReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,69 +58,21 @@ time_t JJYReceiver::clock_tick(){
return globaltime;
}

int JJYReceiver::distance(const volatile uint8_t* arr1,volatile uint8_t* arr2, int size) {
int hammingDistance = 0;
uint8_t temp;
for (uint8_t i = 0; i < size; i++) {
temp = ~(arr1[i] ^ arr2[i]);
for(uint8_t j = 0; j < 8; j++){
if (((temp >> j) & 0x1) == 1) {
hammingDistance++;
}
}
}
return hammingDistance;
}

int JJYReceiver::max_of_three(uint8_t a, uint8_t b, uint8_t c) {
return (a > b) ? ((a > c) ? 0 : 2) : ((b > c) ? 1 : 2);
}
void JJYReceiver::clear(volatile uint8_t* sampling, int length){
for (uint8_t i = 0; i < length; i++) {
sampling[i] = 0;
}
}


void JJYReceiver::shift_in(uint8_t data,volatile uint8_t* sampling, int length){
uint8_t carry;
for (int i = 0; i < length; i++) {
if(i==0) carry = data;
uint16_t temp = sampling[i];
temp = (temp << 1) | carry;
carry = (temp > 0xFF);
sampling[i] = temp & 0xFF;
}
}

bool JJYReceiver::timeCheck(){
int compare[6][2] = {{0, 1}, {0, 2}, {1, 0}, {1, 2}, {2, 0}, {2, 1}};
uint8_t min1,min2;
for (int i = 0; i < 6; i++) {
min1 = ((jjydata[compare[i][0]].bits.min >> 5) & 0x7) * 10 + (jjydata[compare[i][0]].bits.min & 0x0f) + 1;
min2 = ((jjydata[compare[i][1]].bits.min >> 5) & 0x7) * 10 + (jjydata[compare[i][1]].bits.min & 0x0f) + 2;
if (jjydata[compare[i][0]].bits.year == jjydata[compare[i][1]].bits.year &&
jjydata[compare[i][0]].bits.doyh == jjydata[compare[i][1]].bits.doyh &&
jjydata[compare[i][0]].bits.doyl == jjydata[compare[i][1]].bits.doyl &&
jjydata[compare[i][0]].bits.hour == jjydata[compare[i][1]].bits.hour &&
abs(min1 - min2) <= 2)
{
last_jjydata[0] = (min2 > min1) ? jjydata[compare[i][1]] : jjydata[compare[i][0]];
state = TIMEVALID;
power(false);
return true;
}
}
return false;
}

time_t JJYReceiver::get_time() {
return globaltime;
}

time_t JJYReceiver::get_time(uint8_t index) {
return updateTimeInfo(jjydata,index,1);
}

time_t JJYReceiver::getTime() {
switch(state){
case INIT:
Expand Down Expand Up @@ -184,7 +136,8 @@ void JJYReceiver::delta_tick(){
if(markercount==2){
rcvcnt = (rcvcnt + 1) % VERIFYLOOP;
if(settime(rcvcnt)){
timeCheck();
datetimeCheck();
timeCheck(rcvcnt);
timeavailable = rcvcnt;
}
#ifdef DEBUG_BUILD
Expand Down Expand Up @@ -252,6 +205,7 @@ uint8_t JJYReceiver::freq(uint8_t freq){
bool JJYReceiver::power(){
return (digitalRead(ponpin) == HIGH && digitalRead(selpin) == HIGH) ? false : true;
}

bool JJYReceiver::power(bool powerstate){
// PDN1(SEL) PDN2(PON)
// 0 0 freq2 40kHz
Expand Down Expand Up @@ -289,6 +243,10 @@ void JJYReceiver::stop(){
state = TIMETICK;
}

// ***********************************************************************************************
// NON SPECIFIC FUNCTION
// ***********************************************************************************************

//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) {
uint8_t daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Expand All @@ -304,6 +262,31 @@ void JJYReceiver::calculateDate(uint16_t year, uint8_t dayOfYear,volatile uint8_
*day = dayOfYear;
}

bool 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);
}

int JJYReceiver::distance(const volatile uint8_t* arr1,volatile uint8_t* arr2, int size) {
int hammingDistance = 0;
uint8_t temp;
for (uint8_t i = 0; i < size; i++) {
temp = ~(arr1[i] ^ arr2[i]);
for(uint8_t j = 0; j < 8; j++){
if (((temp >> j) & 0x1) == 1) {
hammingDistance++;
}
}
}
return hammingDistance;
}

int JJYReceiver::max_of_three(uint8_t a, uint8_t b, uint8_t c) {
return (a > b) ? ((a > c) ? 0 : 2) : ((b > c) ? 1 : 2);
}

// ***********************************************************************************************
// DEBUG FUNCTION
// ***********************************************************************************************
Expand Down
60 changes: 50 additions & 10 deletions src/JJYReceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@

const int N = 12;
typedef union {
uint8_t datetime[8];
uint8_t datetime[8]; //uint64_tがlgt8f328では使えなかったため。本当はshift演算で入れたかった
struct {
uint8_t dummy : 4;
uint8_t P0 : 1;
uint8_t space3 : 4;
uint8_t leap : 2;
Expand All @@ -62,8 +61,9 @@ typedef union {
uint8_t year : 8;
uint8_t su2 : 1 ;
uint8_t P4 : 1;
uint8_t su1 : 1 ;
uint8_t parity : 2 ;
uint8_t SU1 : 1 ;
uint8_t PA1_h: 1 ;
uint8_t PA2_m: 1 ;
uint8_t space2 : 2 ;
uint8_t doyl : 4 ;
uint8_t P3 : 1 ;
Expand Down Expand Up @@ -119,22 +119,21 @@ class JJYReceiver {
void jjy_receive();
time_t clock_tick();
void delta_tick();
void shift_in(uint8_t data,volatile uint8_t* sampling, int length);
void clear(volatile uint8_t* sampling, int length);
void begin();
void stop();
bool power(bool powerstate);
bool power();
uint8_t freq(uint8_t freq);
void monitor(int pin);
void calculateDate(uint16_t year, 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);
bool calculateParity(uint8_t value, uint8_t bitLength, uint8_t expectedParity);
bool timeCheck();
time_t getTime();
time_t get_time();
time_t get_time(uint8_t index);
bool isEvenParity(uint8_t d);
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);
bool calculateParity(uint8_t value, uint8_t bitLength, uint8_t expectedParity);
#ifdef DEBUG_BUILD
void debug();
void debug2();
Expand All @@ -152,6 +151,8 @@ class JJYReceiver {
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];
jjydata[index].bits.PA1_h =(uint8_t) ((0x0004 & jjypayload[JJY_DOYL]) >> 2);
jjydata[index].bits.PA2_m =(uint8_t) ((0x0002 & jjypayload[JJY_DOYL]) >> 1);
timeinfo.tm_sec = 1; //
return true;
}
Expand All @@ -165,6 +166,7 @@ class JJYReceiver {
timeinfo.tm_min = ((jjydata[index].bits.min >> 5) & 0x7) * 10 + (jjydata[index].bits.min & 0x0f) + offset; //
return mktime(&timeinfo);
}

void init(){
state = RECEIVE;
clear(sampling,N);
Expand All @@ -173,6 +175,7 @@ class JJYReceiver {
jjydata[2].bits.hour = 27;
power(true);
}

bool lencheck(volatile uint8_t* arr) {
if (arr[0] != 8) {
return false;
Expand All @@ -185,6 +188,43 @@ class JJYReceiver {
return true;
}

void shift_in(uint8_t data,volatile uint8_t* sampling, int length){
uint8_t carry;
for (int i = 0; i < length; i++) {
if(i==0) carry = data;
uint16_t temp = sampling[i];
temp = (temp << 1) | carry;
carry = (temp > 0xFF);
sampling[i] = temp & 0xFF;
}
}

bool datetimeCheck(){
int compare[6][2] = {{0, 1}, {0, 2}, {1, 0}, {1, 2}, {2, 0}, {2, 1}};
uint8_t min1,min2;
for (int i = 0; i < 6; i++) {
min1 = ((jjydata[compare[i][0]].bits.min >> 5) & 0x7) * 10 + (jjydata[compare[i][0]].bits.min & 0x0f) + 1;
min2 = ((jjydata[compare[i][1]].bits.min >> 5) & 0x7) * 10 + (jjydata[compare[i][1]].bits.min & 0x0f) + 2;
if (jjydata[compare[i][0]].bits.year == jjydata[compare[i][1]].bits.year &&
jjydata[compare[i][0]].bits.doyh == jjydata[compare[i][1]].bits.doyh &&
jjydata[compare[i][0]].bits.doyl == jjydata[compare[i][1]].bits.doyl &&
jjydata[compare[i][0]].bits.hour == jjydata[compare[i][1]].bits.hour &&
abs(min1 - min2) <= 2)
{
last_jjydata[0] = (min2 > min1) ? jjydata[compare[i][1]] : jjydata[compare[i][0]];
state = TIMEVALID;
power(false);
return true;
}
}
return false;
}

bool timeCheck(uint8_t index){
bool hour = (jjydata[index].bits.PA1_h == isEvenParity((uint8_t)jjydata[index].bits.hour));
bool min = (jjydata[index].bits.PA2_m == isEvenParity((uint8_t)jjydata[index].bits.min));
return hour && min;
}
};
#endif

Expand Down

0 comments on commit 9bd229d

Please sign in to comment.