2 * This file is part of the libsigrok project.
4 * Copyright (C) 2016 George Hopkins <george-hopkins@null.net>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 static int send_command(const struct sr_dev_inst *sdi, uint16_t command)
25 struct sr_serial_dev_inst *serial;
28 buffer[0] = command >> 8;
31 if (!(serial = sdi->conn))
34 if (serial_write_nonblocking(serial, (const void *)buffer, 2) != 2)
40 static void send_data(const struct sr_dev_inst *sdi, float sample)
42 struct dev_context *devc;
43 struct sr_datafeed_packet packet;
44 struct sr_datafeed_analog analog;
45 struct sr_analog_encoding encoding;
46 struct sr_analog_meaning meaning;
47 struct sr_analog_spec spec;
51 sr_analog_init(&analog, &encoding, &meaning, &spec, 1);
52 meaning.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
53 meaning.mqflags = devc->cur_mqflags;
54 meaning.unit = SR_UNIT_DECIBEL_SPL;
55 meaning.channels = sdi->channels;
56 analog.num_samples = 1;
57 analog.data = &sample;
58 packet.type = SR_DF_ANALOG;
59 packet.payload = &analog;
60 sr_session_send(sdi, &packet);
63 if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
64 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi);
67 static void process_measurement(const struct sr_dev_inst *sdi)
69 struct dev_context *devc;
74 if (devc->buffer[3] & (1 << 0)) {
75 devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C;
76 devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_A;
78 devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A;
79 devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_C;
82 if (devc->buffer[3] & (1 << 1)) {
83 devc->cur_mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_S;
84 devc->cur_mqflags &= ~SR_MQFLAG_SPL_TIME_WEIGHT_F;
86 devc->cur_mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_F;
87 devc->cur_mqflags &= ~SR_MQFLAG_SPL_TIME_WEIGHT_S;
90 devc->cur_meas_range = devc->buffer[4] & 3;
92 if (devc->buffer[4] & (1 << 2)) {
93 devc->cur_mqflags |= SR_MQFLAG_MAX;
94 devc->cur_mqflags &= ~SR_MQFLAG_MIN;
95 } else if (devc->buffer[4] & (1 << 3)) {
96 devc->cur_mqflags |= SR_MQFLAG_MIN;
97 devc->cur_mqflags &= ~SR_MQFLAG_MAX;
99 devc->cur_mqflags &= ~SR_MQFLAG_MIN;
100 devc->cur_mqflags &= ~SR_MQFLAG_MAX;
103 value = devc->buffer[1] << 8 | devc->buffer[2];
104 send_data(sdi, value / 10.0);
107 static void process_byte(const struct sr_dev_inst *sdi, const unsigned char c)
109 struct dev_context *devc;
114 if (devc->buffer_len < BUFFER_SIZE) {
115 devc->buffer[devc->buffer_len++] = c;
117 for (i = 1; i < BUFFER_SIZE; i++)
118 devc->buffer[i - 1] = devc->buffer[i];
119 devc->buffer[BUFFER_SIZE - 1] = c;
120 if (devc->buffer[0] == 0x7f && devc->buffer[BUFFER_SIZE - 1] == 0x00) {
121 process_measurement(sdi);
122 devc->buffer_len = 0;
127 SR_PRIV int pce_322a_receive_data(int fd, int revents, void *cb_data)
129 const struct sr_dev_inst *sdi;
130 struct dev_context *devc;
131 struct sr_serial_dev_inst *serial;
136 if (!(sdi = cb_data))
139 if (!(devc = sdi->priv))
142 if (!(serial = sdi->conn))
145 if (revents == G_IO_IN) {
146 if (serial_read_nonblocking(serial, &c, 1) != 1)
148 process_byte(sdi, c);
154 SR_PRIV int pce_322a_connect(const struct sr_dev_inst *sdi)
156 return send_command(sdi, CMD_CONNECT);
159 SR_PRIV int pce_322a_disconnect(const struct sr_dev_inst *sdi)
161 return send_command(sdi, CMD_DISCONNECT);
164 SR_PRIV uint64_t pce_322a_weight_freq_get(const struct sr_dev_inst *sdi)
166 struct dev_context *devc;
170 return devc->cur_mqflags & (SR_MQFLAG_SPL_FREQ_WEIGHT_A | SR_MQFLAG_SPL_FREQ_WEIGHT_C);
173 SR_PRIV int pce_322a_weight_freq_set(const struct sr_dev_inst *sdi, uint64_t freqw)
175 struct dev_context *devc;
179 if (devc->cur_mqflags & freqw)
182 return send_command(sdi, CMD_TOGGLE_WEIGHT_FREQ);
185 SR_PRIV uint64_t pce_322a_weight_time_get(const struct sr_dev_inst *sdi)
187 struct dev_context *devc;
191 return devc->cur_mqflags & (SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_TIME_WEIGHT_S);
194 SR_PRIV int pce_322a_weight_time_set(const struct sr_dev_inst *sdi, uint64_t timew)
196 struct dev_context *devc;
200 if (devc->cur_mqflags & timew)
203 return send_command(sdi, CMD_TOGGLE_WEIGHT_TIME);
206 SR_PRIV int pce_322a_meas_range_get(const struct sr_dev_inst *sdi,
207 uint64_t *low, uint64_t *high)
209 struct dev_context *devc;
213 switch (devc->cur_meas_range) {
214 case MEAS_RANGE_30_130:
218 case MEAS_RANGE_30_80:
222 case MEAS_RANGE_50_100:
226 case MEAS_RANGE_80_130:
237 SR_PRIV int pce_322a_meas_range_set(const struct sr_dev_inst *sdi,
238 uint64_t low, uint64_t high)
240 struct dev_context *devc;
246 if (low == 30 && high == 130)
247 range = MEAS_RANGE_30_130;
248 else if (low == 30 && high == 80)
249 range = MEAS_RANGE_30_80;
250 else if (low == 50 && high == 100)
251 range = MEAS_RANGE_50_100;
252 else if (low == 80 && high == 130)
253 range = MEAS_RANGE_80_130;
257 while (range != devc->cur_meas_range) {
258 ret = send_command(sdi, CMD_TOGGLE_MEAS_RANGE);
261 range = (range - 1) & 3;
267 SR_PRIV int pce_322a_power_off(const struct sr_dev_inst *sdi)
269 return send_command(sdi, CMD_POWER_OFF);