RocketLogger  1.0
meter.c
Go to the documentation of this file.
1 
5 #include "meter.h"
6 
8 char* channel_units[NUM_CHANNELS] = {"mA","uA","V","V","mA","uA","V","V"};
10 uint32_t channel_scales[NUM_CHANNELS] = {1000000, 100000, 100000000, 100000000,1000000, 100000, 100000000, 100000000};
13 
17 void meter_init(void) {
18  // init ncurses mode
19  initscr();
20  // hide cursor
21  curs_set(0);
22 
23  mvprintw(1, 1, "Starting RocketLogger Meter ...");
24  refresh();
25 }
26 
30 void meter_stop(void) {
31  endwin();
32 }
33 
40 void meter_print_buffer(struct rl_conf* conf, void* buffer_addr, uint32_t sample_size) {
41 
42  // clear screen
43  erase();
44 
45  // counter variables
46  uint32_t j = 0;
47  uint32_t k = 0;
48  uint32_t l = 0;
49  uint32_t i = 0; // currents
50  uint32_t v = 0; // voltages
51 
52  uint32_t num_channels = count_channels(conf->channels);
53 
54  // data
55  int64_t value = 0;
56  int32_t dig_data[2];
57  int32_t channel_data[num_channels];
58 
59  // number of samples to average
60  uint32_t avg_number = conf->sample_rate / conf->update_rate;
61 
62 
63  // read digital channels
64  dig_data[0] = (int32_t) (*((int8_t *) (buffer_addr)));
65  dig_data[1] = (int32_t) (*((int8_t *) (buffer_addr + 1)));
66  buffer_addr += PRU_DIG_SIZE;
67 
68 
69  // read, average and scale values (if channel selected)
70  for(j=0; j<NUM_CHANNELS; j++) {
71  if(conf->channels[j] == CHANNEL_ENABLED) {
72  value = 0;
73  if(sample_size == 4) {
74  for(l=0; l<avg_number; l++) {
75  value += *( (int32_t *) (buffer_addr + sample_size*j + l*(NUM_CHANNELS*sample_size + PRU_DIG_SIZE)) );
76  }
77  } else {
78  for(l=0; l<avg_number; l++) {
79  value += *( (int16_t *) (buffer_addr + sample_size*j + l*(NUM_CHANNELS*sample_size + PRU_DIG_SIZE)) );
80  }
81  }
82  value = value / (int64_t)avg_number;
83  channel_data[k] = (int32_t) (( (int32_t) value + calibration.offsets[j] ) * calibration.scales[j]);
84  k++;
85  }
86  }
87 
88  // display values
89  mvprintw(1, 28, "RocketLogger Meter");
90 
91  for(j=0; j<NUM_CHANNELS; j++) {
92  if(conf->channels[j] == CHANNEL_ENABLED) {
93  if(is_current(j)) {
94  // current
95  mvprintw(i*2 + 5, 10, "%s:", channel_names[j]);
96  mvprintw(i*2 + 5, 15, "%12.6f%s", ((float) channel_data[v+i]) / channel_scales[j], channel_units[j]);
97  i++;
98  } else {
99  // voltage
100  mvprintw(v*2 + 5, 55, "%s:", channel_names[j]);
101  mvprintw(v*2 + 5, 60, "%9.6f%s", ((float) channel_data[v+i]) / channel_scales[j], channel_units[j]);
102  v++;
103  }
104  }
105 
106  }
107 
108  // display titles, range information
109  if(i > 0) { // currents
110  mvprintw(3, 10, "Currents:");
111 
112  // display range information
113  mvprintw(3, 33, "Low range:");
114  if((dig_data[0] & I1L_VALID_BIT) > 0) {
115  mvprintw(5, 33, "I1L invalid");
116  } else {
117  mvprintw(5, 33, "I1L valid");
118  }
119  if((dig_data[1] & I2L_VALID_BIT) > 0) {
120  mvprintw(11, 33, "I2L invalid");
121  } else {
122  mvprintw(11, 33, "I2L valid");
123  }
124  }
125 
126  if(v > 0) { // voltages
127  mvprintw(3, 55, "Voltages:");
128  }
129 
130  // digital inputs
131  if(conf->digital_inputs > 0) {
132  mvprintw(20, 10, "Digital Inputs:");
133 
134  j=0;
135  for( ; j<3; j++) {
136  mvprintw(20 + 2*j, 30, "%s:", digital_input_names[j]);
137  mvprintw(20 + 2*j, 38, "%d", (dig_data[0] & digital_input_bits[j]) > 0);
138  }
139  for( ; j<6; j++) {
140  mvprintw(20 + 2*(j-3), 50, "%s:", digital_input_names[j]);
141  mvprintw(20 + 2*(j-3), 58, "%d", (dig_data[1] & digital_input_bits[j]) > 0);
142  }
143  }
144 
145  refresh();
146 }
int offsets[NUM_CHANNELS]
Channel offsets (in bit)
Definition: types.h:254
#define NUM_DIGITAL_INPUTS
Number of RocketLogger digital channels.
Definition: types.h:84
#define DIGIN1_BIT
Definition: types.h:100
double scales[NUM_CHANNELS]
Channel scalings.
Definition: types.h:256
void meter_print_buffer(struct rl_conf *conf, void *buffer_addr, uint32_t sample_size)
Definition: meter.c:40
#define I2L_VALID_BIT
Definition: types.h:99
char * channel_units[NUM_CHANNELS]
Analog channel units.
Definition: meter.c:8
void meter_init(void)
Definition: meter.c:17
#define DIGIN6_BIT
Definition: types.h:105
int digital_inputs
En-/disable digital inputs.
Definition: types.h:216
#define I1L_VALID_BIT
Definition: types.h:98
const uint32_t digital_input_bits[NUM_DIGITAL_INPUTS]
Digital input bit location in binary data.
Definition: meter.c:12
#define DIGIN5_BIT
Definition: types.h:104
Definition: types.h:202
#define NUM_CHANNELS
Maximum number of RocketLogger channels.
Definition: types.h:78
#define CHANNEL_ENABLED
Channel sampling enabled.
Definition: types.h:178
#define PRU_DIG_SIZE
Size of PRU digital information in bytes.
Definition: types.h:88
#define DIGIN2_BIT
Definition: types.h:101
int channels[NUM_CHANNELS]
Channels to sample.
Definition: types.h:212
const char * digital_input_names[NUM_DIGITAL_INPUTS]
Digital input names.
Definition: file_handling.c:10
int is_current(int index)
Definition: util.c:13
int update_rate
Data update rate.
Definition: types.h:208
void meter_stop(void)
Definition: meter.c:30
uint32_t channel_scales[NUM_CHANNELS]
Analog channel scales.
Definition: meter.c:10
int sample_rate
Sampling rate.
Definition: types.h:206
int count_channels(int channels[NUM_CHANNELS])
Definition: util.c:39
int8_t num_channels
Number of channels sampled.
Definition: rl_server.c:39
#define DIGIN3_BIT
Definition: types.h:102
#define DIGIN4_BIT
Definition: types.h:103
const char * channel_names[NUM_CHANNELS]
Channel names.
Definition: file_handling.c:8
struct rl_calibration calibration
Calibration data.
Definition: types.h:284