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