]> sigrok.org Git - libsigrok.git/blobdiff - src/libsigrok-internal.h
strutil: add text to float conversion which also gets precision from text
[libsigrok.git] / src / libsigrok-internal.h
index 378c86d41d4283a50d55aa0ab1fbbaaefdb5a57f..84e5faa538418fc5909d426074d41fa67162d19c 100644 (file)
@@ -74,6 +74,16 @@ static inline uint8_t read_u8(const uint8_t *p)
 }
 #define R8(x)  read_u8((const uint8_t *)(x))
 
+/**
+ * Read an 8 bits signed integer out of memory.
+ * @param x a pointer to the input memory
+ * @return the corresponding signed integer
+ */
+static inline int8_t read_i8(const uint8_t *p)
+{
+       return (int8_t)p[0];
+}
+
 /**
  * Read a 16 bits big endian unsigned integer out of memory.
  * @param x a pointer to the input memory
@@ -357,6 +367,28 @@ static inline float read_fltle(const uint8_t *p)
 }
 #define RLFL(x) read_fltle((const uint8_t *)(x))
 
+/**
+ * Read a 64 bits big endian float out of memory (double precision).
+ * @param x a pointer to the input memory
+ * @return the corresponding floating point value
+ */
+static inline double read_dblbe(const uint8_t *p)
+{
+       /*
+        * Implementor's note: Strictly speaking the "union" trick
+        * is not portable. But this phrase was found to work on the
+        * project's supported platforms, and serve well until a more
+        * appropriate phrase is found.
+        */
+       union { uint64_t u64; double flt; } u;
+       double f;
+
+       u.u64 = read_u64be(p);
+       f = u.flt;
+
+       return f;
+}
+
 /**
  * Read a 64 bits little endian float out of memory (double precision).
  * @param x a pointer to the input memory
@@ -519,6 +551,23 @@ static inline uint8_t read_u8_inc(const uint8_t **p)
        return v;
 }
 
+/**
+ * Read signed 8bit integer from raw memory, increment read position.
+ * @param[in, out] p Pointer into byte stream.
+ * @return Retrieved integer value, signed.
+ */
+static inline int8_t read_i8_inc(const uint8_t **p)
+{
+       int8_t v;
+
+       if (!p || !*p)
+               return 0;
+       v = read_i8(*p);
+       *p += sizeof(v);
+
+       return v;
+}
+
 /**
  * Read unsigned 16bit integer from raw memory (big endian format), increment read position.
  * @param[in, out] p Pointer into byte stream.
@@ -554,17 +603,34 @@ static inline uint16_t read_u16le_inc(const uint8_t **p)
 }
 
 /**
- * Read unsigned 32bit integer from raw memory (big endian format), increment read position.
+ * Read signed 16bit integer from raw memory (big endian format), increment read position.
  * @param[in, out] p Pointer into byte stream.
- * @return Retrieved integer value, unsigned.
+ * @return Retrieved integer value, signed.
  */
-static inline uint32_t read_u32be_inc(const uint8_t **p)
+static inline int16_t read_i16be_inc(const uint8_t **p)
 {
-       uint32_t v;
+       int16_t v;
 
        if (!p || !*p)
                return 0;
-       v = read_u32be(*p);
+       v = read_i16be(*p);
+       *p += sizeof(v);
+
+       return v;
+}
+
+/**
+ * Read signed 16bit integer from raw memory (little endian format), increment read position.
+ * @param[in, out] p Pointer into byte stream.
+ * @return Retrieved integer value, signed.
+ */
+static inline int16_t read_i16le_inc(const uint8_t **p)
+{
+       int16_t v;
+
+       if (!p || !*p)
+               return 0;
+       v = read_i16le(*p);
        *p += sizeof(v);
 
        return v;
@@ -587,6 +653,23 @@ static inline uint32_t read_u24le_inc(const uint8_t **p)
        return v;
 }
 
+/**
+ * Read unsigned 32bit integer from raw memory (big endian format), increment read position.
+ * @param[in, out] p Pointer into byte stream.
+ * @return Retrieved integer value, unsigned.
+ */
+static inline uint32_t read_u32be_inc(const uint8_t **p)
+{
+       uint32_t v;
+
+       if (!p || !*p)
+               return 0;
+       v = read_u32be(*p);
+       *p += sizeof(v);
+
+       return v;
+}
+
 /**
  * Read unsigned 32bit integer from raw memory (little endian format), increment read position.
  * @param[in, out] p Pointer into byte stream.
@@ -604,6 +687,40 @@ static inline uint32_t read_u32le_inc(const uint8_t **p)
        return v;
 }
 
+/**
+ * Read signed 32bit integer from raw memory (big endian format), increment read position.
+ * @param[in, out] p Pointer into byte stream.
+ * @return Retrieved integer value, signed.
+ */
+static inline int32_t read_i32be_inc(const uint8_t **p)
+{
+       int32_t v;
+
+       if (!p || !*p)
+               return 0;
+       v = read_i32be(*p);
+       *p += sizeof(v);
+
+       return v;
+}
+
+/**
+ * Read signed 32bit integer from raw memory (little endian format), increment read position.
+ * @param[in, out] p Pointer into byte stream.
+ * @return Retrieved integer value, signed.
+ */
+static inline int32_t read_i32le_inc(const uint8_t **p)
+{
+       int32_t v;
+
+       if (!p || !*p)
+               return 0;
+       v = read_i32le(*p);
+       *p += sizeof(v);
+
+       return v;
+}
+
 /**
  * Read unsigned 64bit integer from raw memory (big endian format), increment read position.
  * @param[in, out] p Pointer into byte stream.
@@ -638,6 +755,23 @@ static inline uint64_t read_u64le_inc(const uint8_t **p)
        return v;
 }
 
+/**
+ * Read 32bit float from raw memory (big endian format), increment read position.
+ * @param[in, out] p Pointer into byte stream.
+ * @return Retrieved float value.
+ */
+static inline float read_fltbe_inc(const uint8_t **p)
+{
+       float v;
+
+       if (!p || !*p)
+               return 0;
+       v = read_fltbe(*p);
+       *p += sizeof(v);
+
+       return v;
+}
+
 /**
  * Read 32bit float from raw memory (little endian format), increment read position.
  * @param[in, out] p Pointer into byte stream.
@@ -655,6 +789,23 @@ static inline float read_fltle_inc(const uint8_t **p)
        return v;
 }
 
+/**
+ * Read 64bit float from raw memory (big endian format), increment read position.
+ * @param[in, out] p Pointer into byte stream.
+ * @return Retrieved float value.
+ */
+static inline double read_dblbe_inc(const uint8_t **p)
+{
+       double v;
+
+       if (!p || !*p)
+               return 0;
+       v = read_dblbe(*p);
+       *p += sizeof(v);
+
+       return v;
+}
+
 /**
  * Read 64bit float from raw memory (little endian format), increment read position.
  * @param[in, out] p Pointer into byte stream.
@@ -1608,6 +1759,8 @@ SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_grou
 SR_PRIV int std_dummy_set_params(struct sr_serial_dev_inst *serial,
        int baudrate, int bits, int parity, int stopbits,
        int flowcontrol, int rts, int dtr);
+SR_PRIV int std_dummy_set_handshake(struct sr_serial_dev_inst *serial,
+       int rts, int dtr);
 
 /*--- resource.c ------------------------------------------------------------*/
 
@@ -1633,6 +1786,7 @@ SR_PRIV int sr_atoi(const char *str, int *ret);
 SR_PRIV int sr_atod(const char *str, double *ret);
 SR_PRIV int sr_atof(const char *str, float *ret);
 SR_PRIV int sr_atod_ascii(const char *str, double *ret);
+SR_PRIV int sr_atod_ascii_digits(const char *str, double *ret, int *digits);
 SR_PRIV int sr_atof_ascii(const char *str, float *ret);
 
 SR_PRIV GString *sr_hexdump_new(const uint8_t *data, const size_t len);
@@ -1670,6 +1824,8 @@ enum {
 };
 
 typedef gboolean (*packet_valid_callback)(const uint8_t *buf);
+typedef int (*packet_valid_len_callback)(void *st,
+       const uint8_t *p, size_t l, size_t *pl);
 
 typedef GSList *(*sr_ser_list_append_t)(GSList *devs, const char *name,
                const char *desc);
@@ -1692,15 +1848,17 @@ SR_PRIV int serial_set_read_chunk_cb(struct sr_serial_dev_inst *serial,
                serial_rx_chunk_callback cb, void *cb_data);
 SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
                int bits, int parity, int stopbits, int flowcontrol, int rts, int dtr);
+SR_PRIV int serial_set_handshake(struct sr_serial_dev_inst *serial,
+               int rts, int dtr);
 SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial,
                const char *paramstr);
 SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
                int *buflen, gint64 timeout_ms);
 SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
-                                uint8_t *buf, size_t *buflen,
-                                size_t packet_size,
-                                packet_valid_callback is_valid,
-                                uint64_t timeout_ms);
+               uint8_t *buf, size_t *buflen,
+               size_t packet_size, packet_valid_callback is_valid,
+               packet_valid_len_callback is_valid_len, size_t *return_size,
+               uint64_t timeout_ms);
 SR_PRIV int sr_serial_extract_options(GSList *options, const char **serial_device,
                                      const char **serial_options);
 SR_PRIV int serial_source_add(struct sr_session *session,
@@ -1732,6 +1890,8 @@ struct ser_lib_functions {
        int (*set_params)(struct sr_serial_dev_inst *serial,
                        int baudrate, int bits, int parity, int stopbits,
                        int flowcontrol, int rts, int dtr);
+       int (*set_handshake)(struct sr_serial_dev_inst *serial,
+                       int rts, int dtr);
        int (*setup_source_add)(struct sr_session *session,
                        struct sr_serial_dev_inst *serial,
                        int events, int timeout,
@@ -2175,6 +2335,20 @@ SR_PRIV gboolean sr_brymen_bm25x_packet_valid(const uint8_t *buf);
 SR_PRIV int sr_brymen_bm25x_parse(const uint8_t *buf, float *floatval,
                             struct sr_datafeed_analog *analog, void *info);
 
+/*--- dmm/bm52x.c -----------------------------------------------------------*/
+
+#define BRYMEN_BM52X_PACKET_SIZE 24
+#define BRYMEN_BM52X_DISPLAY_COUNT 2
+
+struct brymen_bm52x_info { size_t ch_idx; };
+
+#ifdef HAVE_SERIAL_COMM
+SR_PRIV int sr_brymen_bm52x_packet_request(struct sr_serial_dev_inst *serial);
+#endif
+SR_PRIV gboolean sr_brymen_bm52x_packet_valid(const uint8_t *buf);
+SR_PRIV int sr_brymen_bm52x_parse(const uint8_t *buf, float *floatval,
+               struct sr_datafeed_analog *analog, void *info);
+
 /*--- dmm/bm86x.c -----------------------------------------------------------*/
 
 #define BRYMEN_BM86X_PACKET_SIZE 24