Skip to content

Commit

Permalink
受信部分の最適化
Browse files Browse the repository at this point in the history
  • Loading branch information
Blue-Crescent committed Feb 19, 2024
1 parent fc77de3 commit 9cdebc9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
35 changes: 13 additions & 22 deletions src/JJYReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ bool JJYReceiver::timeCheck(){
for (int i = 0; i < 6; i++) {
min[0] = ((jjydata[compare[i][0]].bits.min >> 5) & 0x7) * 10 + (jjydata[compare[i][0]].bits.min & 0x0f) + 1;
min[1] = ((jjydata[compare[i][1]].bits.min >> 5) & 0x7) * 10 + (jjydata[compare[i][1]].bits.min & 0x0f) + 2;
hour00[0] = min[0] ? 1 : 0;
hour00[1] = min[1] ? 1 : 0;
hour00[0] = ( min[0] == 0 ) ? 1 : 0;
hour00[1] = ( min[1] == 0 ) ? 1 : 0;
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 &&
Expand Down Expand Up @@ -133,12 +133,8 @@ time_t JJYReceiver::getTime() {
if(timeavailable == -1) return -1;
temp_time = updateTimeInfo(jjydata,timeavailable,1);
timeavailable = -1;
switch(mode){
case HASTY:
if(operation == BOOT){
temp_time = updateTimeInfo(jjydata,rcvcnt,1);
globaltime = temp_time;
}
switch(reliability){
case 1:
return temp_time;
break;
}
Expand All @@ -147,6 +143,9 @@ time_t JJYReceiver::getTime() {
globaltime = updateTimeInfo(last_jjydata,0,1);
state = TIMETICK;
received_time = globaltime;
break;
default:
return received_time;
}
return received_time;
}
Expand All @@ -161,10 +160,10 @@ void JJYReceiver::delta_tick(){
data = digitalRead(datapin)==HIGH ? 1 : 0;
shift_in(data, sampling, N);
sampleindex++;
if(sampleindex == 100){
if(95 < sampleindex){
sampleindex = 0;
clear(sampling, N);
}else if(sampleindex == 90){ // クロックが揺らぐので100sampleしっかりないため少し間引く
}else if(sampleindex == 94){
#ifdef DEBUG_BUILD
debug2();
#endif
Expand Down Expand Up @@ -225,22 +224,16 @@ void JJYReceiver::delta_tick(){
}

void JJYReceiver::jjy_receive(){
unsigned long time = millis();
unsigned long window;
if(state == TIMEVALID) return;
bool data = digitalRead(datapin); // ピンの状態を読み取る
if (data == LOW) {
if(monitorpin != -1) digitalWrite(monitorpin,LOW);
window = time - fallingtime[0];
if(990 < window){
if(sampleindex < 20){
sampleindex = 0;
clear(sampling,N);
}
fallingtime[1] = fallingtime[0];
fallingtime[0] = time;
}else{
if(monitorpin != -1) digitalWrite(monitorpin,HIGH);

}
}
uint8_t JJYReceiver::freq(uint8_t freq){
Expand Down Expand Up @@ -289,12 +282,8 @@ void JJYReceiver::monitor(int pin){
monitorpin = pin;
}

void JJYReceiver::begin(uint8_t updatemode){
mode = static_cast<MODE>(updatemode);
init();
}
void JJYReceiver::begin(){
begin(NORMAL);
init();
}

void JJYReceiver::stop(){
Expand Down Expand Up @@ -362,6 +351,8 @@ void JJYReceiver::debug(){
case TIMEVALID:
DEBUG_PRINT("TIMEVALID");
break;
default:
break;
}
DEBUG_PRINT(" ");
DEBUG_PRINT((int)jjypayloadlen[jjystate]);
Expand Down
21 changes: 8 additions & 13 deletions src/JJYReceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
#include <Arduino.h>
#include <stdint.h>

//#define DEBUG_BUILD
//#define DEBUG_ESP32
#define DEBUG_BUILD
#define DEBUG_ESP32

#ifdef DEBUG_BUILD
# define DEBUG_PRINT(...) Serial.print(__VA_ARGS__);
Expand Down Expand Up @@ -78,9 +78,7 @@ typedef union {
} JJYData;

class JJYReceiver {
enum OPERATION {BOOT,OPERATION};
enum MODE {HASTY=1,NORMAL=0,CONSERVATIVE=2};
enum STATE {INIT,RECEIVE,TIMEVALID,TIMETICK};
enum STATE {INIT,RECEIVE,TIMEVALID,TIMETICK};
enum JJYSTATE {JJY_INIT=-1,JJY_MIN=0,JJY_HOUR=1,JJY_DOYH=2,JJY_DOYL=3,JJY_YEAR=4,JJY_WEEK=5};

public:
Expand All @@ -89,15 +87,13 @@ class JJYReceiver {
JJYData last_jjydata[1];
volatile enum STATE state = INIT;
volatile enum JJYSTATE jjystate = JJY_INIT;
volatile enum OPERATION operation = BOOT;
//volatile enum MODE mode = NORMAL;
volatile enum MODE mode = HASTY;
volatile uint8_t rcvcnt = 0;
volatile uint8_t rcvcnt = 0;
volatile unsigned long fallingtime[2];
volatile const int8_t datapin,selpin,ponpin;
volatile int8_t monitorpin = -1;
volatile uint8_t frequency = 0;
volatile uint8_t markercount = 0;
volatile uint8_t reliability = 0;
volatile uint8_t quality = 0;

volatile uint8_t tick = 0;
Expand All @@ -107,9 +103,9 @@ class JJYReceiver {
volatile uint8_t sampleindex = 0;
volatile uint8_t sampling [N];
volatile int8_t timeavailable = -1;
volatile const uint8_t CONST_PM [N] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF0,0x00,0x00,0x00};
volatile const uint8_t CONST_H [N] = {0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
volatile const uint8_t CONST_L [N] = {0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
volatile const uint8_t CONST_PM [N] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF0,0x00};
volatile const uint8_t CONST_H [N] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00};
volatile const uint8_t CONST_L [N] = {0xFF,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

volatile time_t globaltime = 0;
volatile time_t received_time = -1;
Expand All @@ -126,7 +122,6 @@ class JJYReceiver {
void shift_in(uint8_t data,volatile uint8_t* sampling, int length);
void clear(volatile uint8_t* sampling, int length);
void begin();
void begin(uint8_t mode);
void stop();
bool power(bool powerstate);
bool power();
Expand Down

0 comments on commit 9cdebc9

Please sign in to comment.