]> sigrok.org Git - libsigrok.git/blob - hardware/gmc-mh-1x-2x/protocol.h
8f1c7c15a0f5c20aba27ee78cb852f1f69ea7da8
[libsigrok.git] / hardware / gmc-mh-1x-2x / protocol.h
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /** @file
21  *  Gossen Metrawatt Metrahit 1x/2x drivers
22  *  @private
23  */
24
25 #ifndef LIBSIGROK_HARDWARE_GMC_MH_1X_2X_PROTOCOL_H
26 #define LIBSIGROK_HARDWARE_GMC_MH_1X_2X_PROTOCOL_H
27
28 #include <stdint.h>
29 #include <glib.h>
30 #include "libsigrok.h"
31 #include "libsigrok-internal.h"
32
33 #define LOG_PREFIX "gmc-mh-1x-2x"
34
35 #define GMC_BUFSIZE  266
36
37 /** Message ID bits 4, 5 */
38 #define MSGID_MASK  0x30 /**< Mask to get message ID bits */
39 #define MSGID_INF   0x00 /**< Start of message with device info */
40 #define MSGID_D10   0x10 /**< Start of data message, non-displayed intermediate */
41 #define MSGID_DTA   0x20 /**< Start of data message, displayed, averaged */
42 #define MSGID_DATA  0x30 /**< Data byte in message */
43
44 #define MSGC_MASK   0x0f  /**< Mask to get message byte contents in send mode */
45
46 #define MSGSRC_MASK 0xc0 /**< Mask to get bits related to message source */
47
48 #define bc(x) (x & MSGC_MASK) /**< Get contents from a byte */
49
50 #define MASK_6BITS  0x3f /**< Mask lower six bits. */
51
52 /**
53  * Internal multimeter model codes. In opposite to the multimeter models from
54  * protocol (see decode_model()), these codes allow working with ranges.
55  */
56 enum model {
57         METRAHIT_NONE           = 0,  /**< Value for uninitialized variable */
58         METRAHIT_12S            = 12,
59         METRAHIT_13S14A         = 13,
60         METRAHIT_14S            = 14,
61         METRAHIT_15S            = 15,
62         METRAHIT_16S            = 16,
63         METRAHIT_16I            = 17,
64         METRAHIT_16X = METRAHIT_16I,  /**< All Metrahit 16 */
65         /* A Metrahit 17 exists, but seems not to have an IR interface. */
66         METRAHIT_18S            = 18,
67         METRAHIT_2X             = 20, /**< For model type comparisons */
68         METRAHIT_22SM           = 22,
69         METRAHIT_23S            = 23,
70         METRAHIT_24S            = 24,
71         METRAHIT_25S            = 25,
72         METRAHIT_26SM           = 26,
73         METRAHIT_28S            = 28,
74         METRAHIT_29S            = 29,
75 };
76
77 /** Get model string from sigrok-internal model code. */
78 SR_PRIV const char *gmc_model_str(enum model mcode);
79
80 /** Private, per-device-instance driver context. */
81 struct dev_context {
82         /* Model-specific information */
83         enum model model;       /**< Model code. */
84
85         /* Acquisition settings */
86         uint64_t limit_samples; /**< Target number of samples */
87         uint64_t limit_msec;    /**< Target sampling time */
88
89         /* Opaque pointer passed in by frontend. */
90         void *cb_data;
91
92         /* Operational state */
93         gboolean settings_ok;   /**< Settings msg received yet. */
94         int msg_type;   /**< Message type (MSGID_INF, ...). */
95         int msg_len;    /**< Message lengh (valid when msg, curr. type known).*/
96         int mq;         /**< Measured quantity */
97         int unit;               /**< Measured unit */
98         uint64_t mqflags;       /**< Measured quantity flags */
99         float value;            /**< Measured value */
100         float scale;            /**< Scale for value. */
101         int8_t scale1000;   /**< Additional scale factor 1000^x. */
102         gboolean vmains_29S;    /**< Measured ctmv is V mains (29S only). */
103
104         /* Temporary state across callbacks */
105         uint64_t num_samples;   /**< Current #samples for limit_samples */
106         GTimer *elapsed_msec;   /**< Used for sampling with limit_msec  */
107         uint8_t buf[GMC_BUFSIZE];       /**< Buffer for read callback */
108         int buflen;                     /**< Data len in buf */
109 };
110
111 SR_PRIV int gmc_mh_1x_2x_receive_data(int fd, int revents, void *cb_data);
112 SR_PRIV int gmc_decode_model_sm(uint8_t mcode);
113
114 #endif