RocketLogger  1.1.4
meter.c
Go to the documentation of this file.
1 
32 #include "meter.h"
33 
35 char* channel_units[NUM_CHANNELS] = {"mA", "uA", "V", "V",
36  "mA", "uA", "V", "V"};
38 uint32_t channel_scales[NUM_CHANNELS] = {1000000, 100000, 100000000, 100000000,
39  1000000, 100000, 100000000, 100000000};
43 
47 void meter_init(void) {
48  // init ncurses mode
49  initscr();
50  // hide cursor
51  curs_set(0);
52 
53  mvprintw(1, 1, "Starting RocketLogger Meter ...");
54  refresh();
55 }
56 
60 void meter_stop(void) { endwin(); }
61 
68 void meter_print_buffer(struct rl_conf* conf, void* buffer_addr,
69  uint32_t sample_size) {
70 
71  // clear screen
72  erase();
73 
74  // counter variables
75  uint32_t j = 0;
76  uint32_t k = 0;
77  uint32_t l = 0;
78  uint32_t i = 0; // currents
79  uint32_t v = 0; // voltages
80 
81  uint32_t num_channels = count_channels(conf->channels);
82 
83  // data
84  int64_t value = 0;
85  int32_t dig_data[2];
86  int32_t channel_data[num_channels];
87 
88  // number of samples to average
89  uint32_t avg_number = conf->sample_rate / conf->update_rate;
90 
91  // read digital channels
92  dig_data[0] = (int32_t)(*((int8_t*)(buffer_addr)));
93  dig_data[1] = (int32_t)(*((int8_t*)(buffer_addr + 1)));
94  buffer_addr += PRU_DIG_SIZE;
95 
96  // read, average and scale values (if channel selected)
97  for (j = 0; j < NUM_CHANNELS; j++) {
98  if (conf->channels[j] == CHANNEL_ENABLED) {
99  value = 0;
100  if (sample_size == 4) {
101  for (l = 0; l < avg_number; l++) {
102  value += *((int32_t*)(buffer_addr + sample_size * j +
103  l * (NUM_CHANNELS * sample_size +
104  PRU_DIG_SIZE)));
105  }
106  } else {
107  for (l = 0; l < avg_number; l++) {
108  value += *((int16_t*)(buffer_addr + sample_size * j +
109  l * (NUM_CHANNELS * sample_size +
110  PRU_DIG_SIZE)));
111  }
112  }
113  value = value / (int64_t)avg_number;
114  channel_data[k] =
115  (int32_t)(((int32_t)value + calibration.offsets[j]) *
116  calibration.scales[j]);
117  k++;
118  }
119  }
120 
121  // display values
122  mvprintw(1, 28, "RocketLogger Meter");
123 
124  for (j = 0; j < NUM_CHANNELS; j++) {
125  if (conf->channels[j] == CHANNEL_ENABLED) {
126  if (is_current(j)) {
127  // current
128  mvprintw(i * 2 + 5, 10, "%s:", channel_names[j]);
129  mvprintw(i * 2 + 5, 15, "%12.6f%s",
130  ((float)channel_data[v + i]) / channel_scales[j],
131  channel_units[j]);
132  i++;
133  } else {
134  // voltage
135  mvprintw(v * 2 + 5, 55, "%s:", channel_names[j]);
136  mvprintw(v * 2 + 5, 60, "%9.6f%s",
137  ((float)channel_data[v + i]) / channel_scales[j],
138  channel_units[j]);
139  v++;
140  }
141  }
142  }
143 
144  // display titles, range information
145  if (i > 0) { // currents
146  mvprintw(3, 10, "Currents:");
147 
148  // display range information
149  mvprintw(3, 33, "Low range:");
150  if ((dig_data[0] & I1L_VALID_BIT) > 0) {
151  mvprintw(5, 33, "I1L invalid");
152  } else {
153  mvprintw(5, 33, "I1L valid");
154  }
155  if ((dig_data[1] & I2L_VALID_BIT) > 0) {
156  mvprintw(11, 33, "I2L invalid");
157  } else {
158  mvprintw(11, 33, "I2L valid");
159  }
160  }
161 
162  if (v > 0) { // voltages
163  mvprintw(3, 55, "Voltages:");
164  }
165 
166  // digital inputs
167  if (conf->digital_inputs > 0) {
168  mvprintw(20, 10, "Digital Inputs:");
169 
170  j = 0;
171  for (; j < 3; j++) {
172  mvprintw(20 + 2 * j, 30, "%s:", digital_input_names[j]);
173  mvprintw(20 + 2 * j, 38, "%d",
174  (dig_data[0] & digital_input_bits[j]) > 0);
175  }
176  for (; j < 6; j++) {
177  mvprintw(20 + 2 * (j - 3), 50, "%s:", digital_input_names[j]);
178  mvprintw(20 + 2 * (j - 3), 58, "%d",
179  (dig_data[1] & digital_input_bits[j]) > 0);
180  }
181  }
182 
183  refresh();
184 }
int offsets[NUM_CHANNELS]
Channel offsets (in bit)
Definition: types.h:305
#define NUM_DIGITAL_INPUTS
Number of RocketLogger digital channels.
Definition: types.h:110
#define DIGIN1_BIT
Definition: types.h:126
double scales[NUM_CHANNELS]
Channel scalings.
Definition: types.h:307
void meter_print_buffer(struct rl_conf *conf, void *buffer_addr, uint32_t sample_size)
Definition: meter.c:68
#define I2L_VALID_BIT
Definition: types.h:125
char * channel_units[NUM_CHANNELS]
Analog channel units.
Definition: meter.c:35
void meter_init(void)
Definition: meter.c:47
#define DIGIN6_BIT
Definition: types.h:131
int digital_inputs
En-/disable digital inputs.
Definition: types.h:265
#define I1L_VALID_BIT
Definition: types.h:124
const uint32_t digital_input_bits[NUM_DIGITAL_INPUTS]
Digital input bit location in binary data.
Definition: meter.c:41
#define DIGIN5_BIT
Definition: types.h:130
Definition: types.h:247
#define NUM_CHANNELS
Maximum number of RocketLogger channels.
Definition: types.h:104
#define CHANNEL_ENABLED
Channel sampling enabled.
Definition: types.h:223
#define PRU_DIG_SIZE
Size of PRU digital information in bytes.
Definition: types.h:114
#define DIGIN2_BIT
Definition: types.h:127
int channels[NUM_CHANNELS]
Channels to sample.
Definition: types.h:261
const char * digital_input_names[NUM_DIGITAL_INPUTS]
Digital input names.
Definition: file_handling.c:45
int is_current(int index)
Definition: util.c:53
int update_rate
Data update rate.
Definition: types.h:257
void meter_stop(void)
Definition: meter.c:60
uint32_t channel_scales[NUM_CHANNELS]
Analog channel scales.
Definition: meter.c:38
int sample_rate
Sampling rate.
Definition: types.h:253
int count_channels(int channels[NUM_CHANNELS])
Definition: util.c:80
int8_t num_channels
Number of channels sampled.
Definition: rl_server.c:68
#define DIGIN3_BIT
Definition: types.h:128
#define DIGIN4_BIT
Definition: types.h:129
const char * channel_names[NUM_CHANNELS]
Channel names.
Definition: file_handling.c:42
struct rl_calibration calibration
Calibration data.
Definition: types.h:335