summaryrefslogtreecommitdiff
path: root/tools/solarmeter/solarmeter.c
blob: 9aace5b05b33b1b7edf8f9103f6b60fe6a5fcff3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdint.h>

// data via $1, output to STDOUT
// begin = 255 followed by 12 bytes  2*(4*i + 1*u) + PowerH:PowerL

int main (int argc, char* argv[])
{
  u_int8_t tmp = 0;
  u_int8_t buffer[12];
  u_int32_t power[2];

  int infile_fd = -1;  
  if (argc > 1) {
    infile_fd = open(argv[1], O_RDONLY);
    if(infile_fd < 0) {
      perror("open error");
      return 1;
    }
  } else {
    infile_fd = STDIN_FILENO;
  }
  
  while (read(infile_fd, &tmp, 1) == 1) {
    if (tmp == 255) {
      int ret = read(infile_fd, &buffer, sizeof(buffer));
      if(ret < 0) {
        perror("read error");
        return 2;
      } else if(!ret) {
        return 0;
      } else if(ret != sizeof(buffer)) {
        continue;
      }

      power[0] = (buffer[10]<<8) + buffer[11];
      printf("%5d (", power[0]);

      int i;
      for(i=0; i<10; i++)
        printf("%3d ", buffer[i]);

      power[1] = (((u_int16_t)buffer[0] + (u_int16_t)buffer[1] + (u_int16_t)buffer[2] + (u_int16_t)buffer[3]) * (u_int32_t)buffer[4]) +
              (((u_int16_t)buffer[5] + (u_int16_t)buffer[6] + (u_int16_t)buffer[7] + (u_int16_t)buffer[8]) * (u_int32_t)buffer[9]);
      int tmp = 0;
      if(power[1]%128 >= 64) tmp=1;
      power[1] /= 128;
      power[1] += tmp;
      printf(")  -> %s (%5d) \n", power[1] == power[0] ? "ok" : "xx", power[1]);
    }
  }
  printf("\n");

  return 0;
}