Parse 128 bit numberic value accurately

This commit is contained in:
dreamsourcelabTAI 2022-02-07 13:46:49 +08:00
parent cd449045b0
commit c7a3fb40a4
8 changed files with 37 additions and 117 deletions

View File

@ -76,7 +76,9 @@ Annotation::Annotation(const srd_proto_data *const pdata, DecoderStatus *status)
char **annotations = pda->ann_text;
while(annotations && *annotations) {
key.append(*annotations, strlen(*annotations));
if ((*annotations)[0] != '\n'){
key.append(*annotations, strlen(*annotations));
}
annotations++;
}
@ -92,7 +94,9 @@ Annotation::Annotation(const srd_proto_data *const pdata, DecoderStatus *status)
if (resItem != NULL){
char **annotations = pda->ann_text;
while(annotations && *annotations) {
resItem->src_lines.push_back(QString::fromUtf8(*annotations));
if ((*annotations)[0] != '\n'){
resItem->src_lines.push_back(QString::fromUtf8(*annotations));
}
annotations++;
}
@ -101,14 +105,6 @@ Annotation::Annotation(const srd_proto_data *const pdata, DecoderStatus *status)
strcpy(resItem->str_number_hex, pda->str_number_hex);
resItem->is_numeric = true;
}
/*
//disable auto convert to numberic format
else if (resItem->src_lines.size() == 1 && _type >= 100 && _type < 200){
if (is_hex_number_str(resItem->src_lines[0].toLatin1().data())){
resItem->is_numeric = true;
}
}
*/
_status->m_bNumeric |= resItem->is_numeric;
}

View File

@ -26,12 +26,12 @@
#include <vector>
#include <QString>
#define DECODER_MAX_DATA_BLOCK_LEN 25
#define DECODER_MAX_DATA_BLOCK_LEN 35
struct AnnotationSourceItem
{
bool is_numeric;
char str_number_hex[18]; //numerical value hex format string
char str_number_hex[DECODER_MAX_DATA_BLOCK_LEN]; //numerical value hex format string
long long numberic_value;
std::vector<QString> src_lines; //the origin source string lines
std::vector<QString> cvt_lines; //the converted to bin/hex/oct format string lines

4
libsigrokdecode4DSL/decoders/0-spi/pd.py Normal file → Executable file
View File

@ -151,9 +151,9 @@ class Decoder(srd.Decoder):
# Dataword annotations.
if self.have_miso:
self.put(ss, es, self.out_ann, [0, [self.misodata]])
self.put(ss, es, self.out_ann, [0, ['@%02X' % self.misodata]])
if self.have_mosi:
self.put(ss, es, self.out_ann, [1, [self.mosidata]])
self.put(ss, es, self.out_ann, [1, ['@%02X' % self.mosidata]])
def reset_decoder_state(self):
self.misodata = 0 if self.have_miso else None

52
libsigrokdecode4DSL/decoders/0-uart/pd.py Normal file → Executable file
View File

@ -218,12 +218,8 @@ class Decoder(srd.Decoder):
if self.options['bit_order'] == 'msb-first':
bits.reverse()
self.datavalue = bitpack(bits)
self.putx([0, [self.datavalue]])
#b = self.datavalue
#formatted = self.format_value(b)
#if formatted is not None:
# self.putx([0, [formatted]])
self.putx([0, ['@%02X' % self.datavalue]])
self.databits = []
# Advance to either reception of the parity bit, or reception of
@ -231,49 +227,7 @@ class Decoder(srd.Decoder):
self.state = 'GET PARITY BIT'
if self.options['parity_type'] == 'none':
self.state = 'GET STOP BITS'
def format_value(self, v):
# Format value 'v' according to configured options.
# Reflects the user selected kind of representation, as well as
# the number of data bits in the UART frames.
fmt, bits = self.options['format'], self.options['num_data_bits']
# Assume "is printable" for values from 32 to including 126,
# below 32 is "control" and thus not printable, above 127 is
# "not ASCII" in its strict sense, 127 (DEL) is not printable,
# fall back to hex representation for non-printables.
if fmt == 'ascii':
if v in range(32, 126 + 1):
return chr(v)
hexfmt = "[{:02X}]" if bits <= 8 else "[{:03X}]"
return hexfmt.format(v)
# Mere number to text conversion without prefix and padding
# for the "decimal" output format.
if fmt == 'dec':
return "{:d}".format(v)
# Padding with leading zeroes for hex/oct/bin formats, but
# without a prefix for density -- since the format is user
# specified, there is no ambiguity.
if fmt == 'hex':
digits = (bits + 4 - 1) // 4
fmtchar = "X"
elif fmt == 'oct':
digits = (bits + 3 - 1) // 3
fmtchar = "o"
elif fmt == 'bin':
digits = bits
fmtchar = "b"
else:
fmtchar = None
if fmtchar is not None:
fmt = "{{:0{:d}{:s}}}".format(digits, fmtchar)
return fmt.format(v)
return None
def get_parity_bit(self, signal):
self.paritybit = signal

10
libsigrokdecode4DSL/decoders/1-spi/pd.py Normal file → Executable file
View File

@ -195,11 +195,11 @@ class Decoder(srd.Decoder):
for bit in self.mosibits:
self.put(bit[1], bit[2], self.out_ann, [3, ['%d' % bit[0]]])
# Dataword annotations.
# Dataword annotations.
if self.have_miso:
self.put(ss, es, self.out_ann, [0, ['%02X' % self.misodata]])
self.put(ss, es, self.out_ann, [0, ['@%02X' % self.misodata]])
if self.have_mosi:
self.put(ss, es, self.out_ann, [1, ['%02X' % self.mosidata]])
self.put(ss, es, self.out_ann, [1, ['@%02X' % self.mosidata]])
def reset_decoder_state(self):
self.misodata = 0 if self.have_miso else None
@ -289,10 +289,10 @@ class Decoder(srd.Decoder):
elif self.ss_transfer != -1:
if self.have_miso:
self.put(self.ss_transfer, self.samplenum, self.out_ann,
[5, [' '.join(format(x.val, '02X') for x in self.misobytes)]])
[5, [' '.join('@' + format(x.val, '02X') for x in self.misobytes)]])
if self.have_mosi:
self.put(self.ss_transfer, self.samplenum, self.out_ann,
[6, [' '.join(format(x.val, '02X') for x in self.mosibytes)]])
[6, [' '.join('@' + format(x.val, '02X') for x in self.mosibytes)]])
self.put(self.ss_transfer, self.samplenum, self.out_python,
['TRANSFER', self.mosibytes, self.misobytes])

50
libsigrokdecode4DSL/decoders/1-uart/pd.py Normal file → Executable file
View File

@ -253,13 +253,10 @@ class Decoder(srd.Decoder):
self.datavalue = bitpack(bits)
self.putpx(['DATA', 0, (self.datavalue, self.databits)])
self.putx([0, [self.datavalue]])
self.putx([0, ['@%02X' % self.datavalue]])
b = self.datavalue
#formatted = self.format_value(b)
#if formatted is not None:
# self.putx([0, [formatted]])
bdata = b.to_bytes(self.bw, byteorder='big')
self.putbin([0, bdata])
self.putbin([1, bdata])
@ -271,48 +268,7 @@ class Decoder(srd.Decoder):
self.state = 'GET PARITY BIT'
if self.options['parity_type'] == 'none':
self.state = 'GET STOP BITS'
def format_value(self, v):
# Format value 'v' according to configured options.
# Reflects the user selected kind of representation, as well as
# the number of data bits in the UART frames.
fmt, bits = self.options['format'], self.options['num_data_bits']
# Assume "is printable" for values from 32 to including 126,
# below 32 is "control" and thus not printable, above 127 is
# "not ASCII" in its strict sense, 127 (DEL) is not printable,
# fall back to hex representation for non-printables.
if fmt == 'ascii':
if v in range(32, 126 + 1):
return chr(v)
hexfmt = "[{:02X}]" if bits <= 8 else "[{:03X}]"
return hexfmt.format(v)
# Mere number to text conversion without prefix and padding
# for the "decimal" output format.
if fmt == 'dec':
return "{:d}".format(v)
# Padding with leading zeroes for hex/oct/bin formats, but
# without a prefix for density -- since the format is user
# specified, there is no ambiguity.
if fmt == 'hex':
digits = (bits + 4 - 1) // 4
fmtchar = "X"
elif fmt == 'oct':
digits = (bits + 3 - 1) // 3
fmtchar = "o"
elif fmt == 'bin':
digits = bits
fmtchar = "b"
else:
fmtchar = None
if fmtchar is not None:
fmt = "{{:0{:d}{:s}}}".format(digits, fmtchar)
return fmt.format(v)
return None
def get_parity_bit(self, signal):
self.paritybit = signal

View File

@ -25,6 +25,8 @@
#include <stdint.h>
#include <glib.h>
#define DECODE_NUM_HEX_MAX_LEN 35
#ifdef __cplusplus
extern "C" {
#endif
@ -347,7 +349,7 @@ struct srd_proto_data {
struct srd_proto_data_annotation {
int ann_class;
int ann_type;
char str_number_hex[18]; //numerical value hex format string
char str_number_hex[DECODE_NUM_HEX_MAX_LEN]; //numerical value hex format string
long long numberic_value;
char **ann_text; //text string lines
};

View File

@ -22,6 +22,7 @@
#include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
#include "libsigrokdecode.h"
#include <inttypes.h>
#include <string.h>
/** @cond PRIVATE */
extern SRD_PRIV GSList *sessions;
@ -66,6 +67,7 @@ static int py_parse_ann_data(PyObject *list_obj, char ***out_strv, int list_size
PyObject *py_numobj = NULL;
int i;
long long lv;
int nstr;
gstate = PyGILState_Ensure();
@ -91,7 +93,7 @@ static int py_parse_ann_data(PyObject *list_obj, char ***out_strv, int list_size
goto err;
}
//vet numberic value
//get numberic value
if (py_numobj != NULL){
lv = PyLong_AsLongLong(py_numobj);
sprintf(hex_str_buf, "%02llX", lv);
@ -122,6 +124,16 @@ static int py_parse_ann_data(PyObject *list_obj, char ***out_strv, int list_size
if (!str)
goto err;
//check numberic field value
if (str[0]== '@'){
nstr = strlen(str) - 1;
if (nstr > 0 && nstr < DECODE_NUM_HEX_MAX_LEN){
strcpy(hex_str_buf, str + 1);
str[0] = '\n'; //set ignore flag
str[1] = 0;
}
}
strv[i] = str;
}