SHARE
TWEET

Untitled

a guest Jan 22nd, 2020 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <Wire.h>
  3. #include "ds3231.h"
  4.  
  5. #define BUFF_MAX 128
  6.  
  7. uint8_t Time[8];
  8. char recv[BUFF_MAX];
  9. unsigned int recv_size = 0;
  10. unsigned long prev, interval = 5000;
  11.  
  12. void parse_cmd(char *cmd, int cmdsize);
  13.  
  14. void setup()
  15. {
  16.     Serial.begin(9600);
  17.     Wire.begin();
  18.     DS3231_init(DS3231_CONTROL_INTCN);
  19.     memset(recv, 0, BUFF_MAX);
  20.     Serial.println("GET time");
  21. }
  22.  
  23. void loop()
  24. {
  25.     char in;
  26.     char buff[BUFF_MAX];
  27.     unsigned long now = millis();
  28.     struct ts t;
  29.  
  30.     // show time once in a while
  31.     if ((now - prev > interval) && (Serial.available() <= 0)) {
  32.         DS3231_get(&t);
  33.  
  34.         // there is a compile time option in the library to include unixtime support
  35. #ifdef CONFIG_UNIXTIME
  36. #ifdef __AVR__
  37.         snprintf(buff, BUFF_MAX, "%d.%02d.%02d %02d:%02d:%02d %ld", t.year,
  38. #error AVR
  39. #else
  40.         snprintf(buff, BUFF_MAX, "%d.%02d.%02d %02d:%02d:%02d %d", t.year,
  41. #endif
  42.              t.mon, t.mday, t.hour, t.min, t.sec, t.unixtime);
  43. #else
  44.         snprintf(buff, BUFF_MAX, "%d.%02d.%02d %02d:%02d:%02d", t.year,
  45.              t.mon, t.mday, t.hour, t.min, t.sec);
  46. #endif
  47.  
  48.         Serial.println(buff);
  49.         prev = now;
  50.     }
  51.  
  52.     if (Serial.available() > 0) {
  53.         in = Serial.read();
  54.  
  55.         if ((in == 10 || in == 13) && (recv_size > 0)) {
  56.             parse_cmd(recv, recv_size);
  57.             recv_size = 0;
  58.             recv[0] = 0;
  59.         } else if (in < 48 || in > 122) {;       // ignore ~[0-9A-Za-z]
  60.         } else if (recv_size > BUFF_MAX - 2) {   // drop lines that are too long
  61.             // drop
  62.             recv_size = 0;
  63.             recv[0] = 0;
  64.         } else if (recv_size < BUFF_MAX - 2) {
  65.             recv[recv_size] = in;
  66.             recv[recv_size + 1] = 0;
  67.             recv_size += 1;
  68.         }
  69.  
  70.     }
  71. }
  72.  
  73. void parse_cmd(char *cmd, int cmdsize)
  74. {
  75.     uint8_t i;
  76.     uint8_t reg_val;
  77.     char buff[BUFF_MAX];
  78.     struct ts t;
  79.  
  80.     //snprintf(buff, BUFF_MAX, "cmd was '%s' %d\n", cmd, cmdsize);
  81.     //Serial.print(buff);
  82.  
  83.     // TssmmhhWDDMMYYYY aka set time
  84.     if (cmd[0] == 84 && cmdsize == 16) {
  85.         //T355720619112011
  86.         t.sec = inp2toi(cmd, 1);
  87.         t.min = inp2toi(cmd, 3);
  88.         t.hour = inp2toi(cmd, 5);
  89.         t.wday = cmd[7] - 48;
  90.         t.mday = inp2toi(cmd, 8);
  91.         t.mon = inp2toi(cmd, 10);
  92.         t.year = inp2toi(cmd, 12) * 100 + inp2toi(cmd, 14);
  93.         DS3231_set(t);
  94.         Serial.println("OK");
  95.     } else if (cmd[0] == 49 && cmdsize == 1) {  // "1" get alarm 1
  96.         DS3231_get_a1(&buff[0], 59);
  97.         Serial.println(buff);
  98.     } else if (cmd[0] == 50 && cmdsize == 1) {  // "2" get alarm 1
  99.         DS3231_get_a2(&buff[0], 59);
  100.         Serial.println(buff);
  101.     } else if (cmd[0] == 51 && cmdsize == 1) {  // "3" get aging register
  102.         Serial.print("aging reg is ");
  103.         Serial.println(DS3231_get_aging(), DEC);
  104.     } else if (cmd[0] == 65 && cmdsize == 9) {  // "A" set alarm 1
  105.         DS3231_set_creg(DS3231_CONTROL_INTCN | DS3231_CONTROL_A1IE);
  106.         //ASSMMHHDD
  107.         for (i = 0; i < 4; i++) {
  108.             Time[i] = (cmd[2 * i + 1] - 48) * 10 + cmd[2 * i + 2] - 48; // ss, mm, hh, dd
  109.         }
  110.         uint8_t flags[5] = { 0, 0, 0, 0, 0 };
  111.         DS3231_set_a1(Time[0], Time[1], Time[2], Time[3], flags);
  112.         DS3231_get_a1(&buff[0], 59);
  113.         Serial.println(buff);
  114.     } else if (cmd[0] == 66 && cmdsize == 7) {  // "B" Set Alarm 2
  115.         DS3231_set_creg(DS3231_CONTROL_INTCN | DS3231_CONTROL_A2IE);
  116.         //BMMHHDD
  117.         for (i = 0; i < 4; i++) {
  118.             Time[i] = (cmd[2 * i + 1] - 48) * 10 + cmd[2 * i + 2] - 48; // mm, hh, dd
  119.         }
  120.         uint8_t flags[5] = { 0, 0, 0, 0 };
  121.         DS3231_set_a2(Time[0], Time[1], Time[2], flags);
  122.         DS3231_get_a2(&buff[0], 59);
  123.         Serial.println(buff);
  124.     } else if (cmd[0] == 67 && cmdsize == 1) {  // "C" - get temperature register
  125.         Serial.print("temperature reg is ");
  126.         Serial.println(DS3231_get_treg(), DEC);
  127.     } else if (cmd[0] == 68 && cmdsize == 1) {  // "D" - reset status register alarm flags
  128.         reg_val = DS3231_get_sreg();
  129.         reg_val &= B11111100;
  130.         DS3231_set_sreg(reg_val);
  131.     } else if (cmd[0] == 70 && cmdsize == 1) {  // "F" - custom fct
  132.         reg_val = DS3231_get_addr(0x5);
  133.         Serial.print("orig ");
  134.         Serial.print(reg_val,DEC);
  135.         Serial.print("month is ");
  136.         Serial.println(bcdtodec(reg_val & 0x1F),DEC);
  137.     } else if (cmd[0] == 71 && cmdsize == 1) {  // "G" - set aging status register
  138.         DS3231_set_aging(0);
  139.     } else if (cmd[0] == 83 && cmdsize == 1) {  // "S" - get status register
  140.         Serial.print("status reg is ");
  141.         Serial.println(DS3231_get_sreg(), DEC);
  142.     } else {
  143.         Serial.print("unknown command prefix ");
  144.         Serial.println(cmd[0]);
  145.         Serial.println(cmd[0], DEC);
  146.     }
  147. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top