From 1cd25f29a66e90a24c29f132b4a4614c643240b4 Mon Sep 17 00:00:00 2001 From: dreamsourcelabTAI Date: Tue, 11 Jan 2022 15:19:28 +0800 Subject: [PATCH] Add ascii display format for numberic value from the decode result --- DSView/pv/data/decode/AnnotationResTable.cpp | 33 +++++++++++++++----- DSView/pv/data/decode/AnnotationResTable.h | 3 +- DSView/pv/data/decode/annotation.cpp | 32 +++++++++++-------- DSView/pv/data/decode/annotation.h | 2 ++ DSView/pv/data/decode/decoderstatus.h | 6 ++-- DSView/pv/dock/protocoldock.cpp | 7 +++-- DSView/pv/dock/protocolitemlayer.cpp | 11 ++++--- DSView/pv/dsvdef.cpp | 9 ++++-- DSView/pv/dsvdef.h | 9 +++--- decoders/0-i2c/pd.py | 3 +- decoders/0-spi/pd.py | 4 +-- decoders/0-uart/pd.py | 10 +++--- decoders/1-i2c/pd.py | 5 ++- decoders/1-uart/pd.py | 8 +++-- libsigrokdecode4DSL/libsigrokdecode.h | 1 + libsigrokdecode4DSL/type_decoder.c | 9 ++++-- qtpro/DSView.pro.user | 2 +- 17 files changed, 97 insertions(+), 57 deletions(-) diff --git a/DSView/pv/data/decode/AnnotationResTable.cpp b/DSView/pv/data/decode/AnnotationResTable.cpp index f9be9e16..223a3409 100644 --- a/DSView/pv/data/decode/AnnotationResTable.cpp +++ b/DSView/pv/data/decode/AnnotationResTable.cpp @@ -24,11 +24,10 @@ #include "../../dsvdef.h" #define DECODER_MAX_DATA_BLOCK_LEN 25 -#define FORMAT_TMP_BUFFER_SIZE 100 const char g_bin_cvt_table[] = "0000000100100011010001010110011110001001101010111100110111101111"; -char g_bin_format_tmp_buffer[FORMAT_TMP_BUFFER_SIZE + 3]; -char g_oct_format_tmp_buffer[FORMAT_TMP_BUFFER_SIZE + 6]; +char g_bin_format_tmp_buffer[DECODER_MAX_DATA_BLOCK_LEN * 4 + 2]; +char g_oct_format_tmp_buffer[DECODER_MAX_DATA_BLOCK_LEN * 3 + 2]; char g_number_tmp_64[30]; char* bin2oct_string(char *buf, int size, const char *bin, int len){ @@ -111,7 +110,7 @@ int AnnotationResTable::MakeIndex(const std::string &key, AnnotationSourceItem* m_resourceTable.push_back(item); item->cur_display_format = -1; - item->is_numerical = false; + item->is_numeric = false; newItem = item; int dex = m_indexs.size(); @@ -135,9 +134,9 @@ const char* AnnotationResTable::format_numberic(const char *hex_str, int fmt) } //convert to bin format - char *buf = g_bin_format_tmp_buffer + FORMAT_TMP_BUFFER_SIZE; - *(buf + 1) = 0; //set the end flag - *buf = 0; + char *buf = g_bin_format_tmp_buffer + sizeof(g_bin_format_tmp_buffer) - 2; + buf[1] = 0; //set the end flag + buf[0] = 0; int len = strlen(data); //buffer is not enough @@ -178,11 +177,29 @@ const char* AnnotationResTable::format_numberic(const char *hex_str, int fmt) //64 bit integer if (fmt == DecoderDataFormat::dec && len * 4 <= 64){ - long long lv = bin2long_string(buf, len *4); + long long lv = bin2long_string(buf, len * 4); g_number_tmp_64[0] = 0; sprintf(g_number_tmp_64, "%lld", lv); return g_number_tmp_64; } + + //ascii + if (fmt == DecoderDataFormat::ascii && len < 30 - 3){ + if (len == 2){ + int lv = (int)bin2long_string(buf, len * 4); + //can display chars + if (lv >= 33 && lv <= 126){ + sprintf(g_number_tmp_64, "%c", (char)lv); + return g_number_tmp_64; + } + } + char * const wr_buf = g_number_tmp_64; + g_number_tmp_64[0] = '['; + strcpy(g_number_tmp_64 + 1, data); + g_number_tmp_64[len+1] = ']'; + g_number_tmp_64[len+2] = 0; + return g_number_tmp_64; + } return data; } diff --git a/DSView/pv/data/decode/AnnotationResTable.h b/DSView/pv/data/decode/AnnotationResTable.h index 3d6ea3ed..1cf98f73 100644 --- a/DSView/pv/data/decode/AnnotationResTable.h +++ b/DSView/pv/data/decode/AnnotationResTable.h @@ -28,8 +28,9 @@ struct AnnotationSourceItem { - bool is_numerical; + bool is_numeric; char str_number_hex[18]; //numerical value hex format string + long long numberic_value; std::vector src_lines; //the origin source string lines std::vector cvt_lines; //the converted to bin/hex/oct format string lines int cur_display_format; //current format as bin/ex/oct..., init with -1 diff --git a/DSView/pv/data/decode/annotation.cpp b/DSView/pv/data/decode/annotation.cpp index 87c97c65..6bd60bee 100755 --- a/DSView/pv/data/decode/annotation.cpp +++ b/DSView/pv/data/decode/annotation.cpp @@ -40,9 +40,11 @@ char sz_format_tmp_buf[50]; bool is_hex_number_str(const char *str) { char c = *str; + int len = 0; while (c) { + ++len; if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')){ c = *str; str++; @@ -50,8 +52,7 @@ bool is_hex_number_str(const char *str) } return false; } - - return true; + return len % 2 == 0 && len > 0; } namespace pv { @@ -96,19 +97,19 @@ Annotation::Annotation(const srd_proto_data *const pdata, DecoderStatus *status) resItem->src_lines.push_back(QString::fromUtf8(*annotations)); annotations++; } - - //get numerical data + + //get numeric data if (pda->str_number_hex[0]){ strcpy(resItem->str_number_hex, pda->str_number_hex); - resItem->is_numerical = true; + resItem->is_numeric = true; } else if (resItem->src_lines.size() == 1 && _type >= 100 && _type < 200){ if (is_hex_number_str(resItem->src_lines[0].toLatin1().data())){ - resItem->is_numerical = true; + resItem->is_numeric = true; } } - _status->m_bNumerical |= resItem->is_numerical; + _status->m_bNumeric |= resItem->is_numeric; } } @@ -126,11 +127,11 @@ Annotation::~Annotation() const std::vector& Annotation::annotations() const { assert(_status); - + AnnotationSourceItem &resItem = *annTable.GetItem(_resIndex); - - //get origin data, is not a numberical value - if (!resItem.is_numerical){ + + //get origin data, is not a numberic value + if (!resItem.is_numeric){ return resItem.src_lines; } @@ -167,8 +168,13 @@ const std::vector& Annotation::annotations() const } return resItem.cvt_lines; -} - +} + +bool Annotation::is_numberic() +{ + AnnotationSourceItem *resItem = annTable.GetItem(_resIndex); + return resItem->is_numeric; +} } // namespace decode } // namespace data diff --git a/DSView/pv/data/decode/annotation.h b/DSView/pv/data/decode/annotation.h index f2025776..629ea6fa 100755 --- a/DSView/pv/data/decode/annotation.h +++ b/DSView/pv/data/decode/annotation.h @@ -62,6 +62,8 @@ public: return _type; } + bool is_numberic(); + public: const std::vector& annotations() const; diff --git a/DSView/pv/data/decode/decoderstatus.h b/DSView/pv/data/decode/decoderstatus.h index d80780b3..8761bcc2 100644 --- a/DSView/pv/data/decode/decoderstatus.h +++ b/DSView/pv/data/decode/decoderstatus.h @@ -26,13 +26,13 @@ class DecoderStatus public: DecoderStatus() { - m_bNumerical = false; + m_bNumeric = false; m_format = 0; sdr_decoder_handle = NULL; } public: - bool m_bNumerical; //when decoder get any numerical data,it will be set + bool m_bNumeric; //when decoder get any numerical data,it will be set int m_format; //protocol format code void *sdr_decoder_handle; -}; \ No newline at end of file +}; diff --git a/DSView/pv/dock/protocoldock.cpp b/DSView/pv/dock/protocoldock.cpp index 9a49da9c..6081c64f 100755 --- a/DSView/pv/dock/protocoldock.cpp +++ b/DSView/pv/dock/protocoldock.cpp @@ -618,10 +618,12 @@ void ProtocolDock::search_pre() bool ann_valid; while(i < _str_list.size()) { QString nxt = _str_list.at(i); + do { ann_valid = decoder_stack->list_annotation(ann, col, row); row++; - }while(ann_valid && (ann.type() < 100 || ann.type() > 999)); + }while(ann_valid && !ann.is_numberic()); + QString source = ann.annotations().at(0); if (ann_valid && source.contains(nxt)) i++; @@ -686,10 +688,11 @@ void ProtocolDock::search_nxt() while(i < _str_list.size()) { QString nxt = _str_list.at(i); + do { ann_valid = decoder_stack->list_annotation(ann, col, row); row++; - }while(ann_valid && (ann.type() < 100 || ann.type() > 999)); + }while(ann_valid && !ann.is_numberic()); auto strlist = ann.annotations(); QString source = ann.annotations().at(0); diff --git a/DSView/pv/dock/protocolitemlayer.cpp b/DSView/pv/dock/protocolitemlayer.cpp index 784c2a19..863bbaa9 100644 --- a/DSView/pv/dock/protocolitemlayer.cpp +++ b/DSView/pv/dock/protocolitemlayer.cpp @@ -122,10 +122,13 @@ void ProtocolItemLayer::LoadFormatSelect(bool bSingle) m_bSetting = true; _format_combox->clear(); - _format_combox->addItem("hex"); - _format_combox->addItem("dec"); - _format_combox->addItem("oct"); - _format_combox->addItem("bin"); + if (!bSingle){ + _format_combox->addItem("hex"); + _format_combox->addItem("dec"); + _format_combox->addItem("oct"); + _format_combox->addItem("bin"); + } + _format_combox->addItem("ascii"); _format_combox->setCurrentIndex(0); m_bSetting = false; diff --git a/DSView/pv/dsvdef.cpp b/DSView/pv/dsvdef.cpp index 35c57072..113cac13 100644 --- a/DSView/pv/dsvdef.cpp +++ b/DSView/pv/dsvdef.cpp @@ -44,15 +44,18 @@ namespace DecoderDataFormat if (strcmp(name, "dec") == 0){ return (int)dec; } - if (strcmp(name, "hex") == 0){ + if (strcmp(name, "hex") == 0){ return (int)hex; } - if (strcmp(name, "oct") == 0){ + if (strcmp(name, "oct") == 0){ return (int)oct; } - if (strcmp(name, "bin") == 0){ + if (strcmp(name, "bin") == 0){ return (int)bin; } + if (strcmp(name, "ascii") == 0){ + return (int)ascii; + } return (int)hex; } } diff --git a/DSView/pv/dsvdef.h b/DSView/pv/dsvdef.h index fc209a79..13c61d51 100644 --- a/DSView/pv/dsvdef.h +++ b/DSView/pv/dsvdef.h @@ -49,10 +49,11 @@ namespace DecoderDataFormat { enum _data_format { - hex, - dec, - oct, - bin + hex=0, + dec=1, + oct=2, + bin=3, + ascii=4 }; int Parse(const char *name); diff --git a/decoders/0-i2c/pd.py b/decoders/0-i2c/pd.py index 8cb47ee0..dad9922d 100755 --- a/decoders/0-i2c/pd.py +++ b/decoders/0-i2c/pd.py @@ -188,8 +188,7 @@ class Decoder(srd.Decoder): self.putx([proto[cmd][0], w]) self.ss, self.es = self.ss_byte, self.samplenum - self.putx([proto[cmd][0], ['%s: %02X' % (proto[cmd][1], d), - '%s: %02X' % (proto[cmd][2], d), '%02X' % d]]) + self.putx([proto[cmd][0], ['%s: {$}' % proto[cmd][1], '%s: {$}' % proto[cmd][2], '{$}', d]]) # Done with this packet. self.bitcount = self.databyte = 0 diff --git a/decoders/0-spi/pd.py b/decoders/0-spi/pd.py index 4708a98d..2d397f02 100755 --- a/decoders/0-spi/pd.py +++ b/decoders/0-spi/pd.py @@ -151,9 +151,9 @@ class Decoder(srd.Decoder): # 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, [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, [self.mosidata]]) def reset_decoder_state(self): self.misodata = 0 if self.have_miso else None diff --git a/decoders/0-uart/pd.py b/decoders/0-uart/pd.py index fdb7c28b..9edf2254 100755 --- a/decoders/0-uart/pd.py +++ b/decoders/0-uart/pd.py @@ -218,11 +218,11 @@ class Decoder(srd.Decoder): if self.options['bit_order'] == 'msb-first': bits.reverse() self.datavalue = bitpack(bits) - - b = self.datavalue - formatted = self.format_value(b) - if formatted is not None: - self.putx([0, [formatted]]) + self.putx([0, [self.datavalue]]) + #b = self.datavalue + #formatted = self.format_value(b) + #if formatted is not None: + # self.putx([0, [formatted]]) self.databits = [] diff --git a/decoders/1-i2c/pd.py b/decoders/1-i2c/pd.py index fb6af6bb..a163eba4 100755 --- a/decoders/1-i2c/pd.py +++ b/decoders/1-i2c/pd.py @@ -213,11 +213,10 @@ class Decoder(srd.Decoder): if cmd.startswith('ADDRESS'): self.ss, self.es = self.samplenum, self.samplenum + self.bitwidth w = ['Write', 'Wr', 'W'] if self.wr else ['Read', 'Rd', 'R'] - self.putx([proto[cmd][0], w]) + self.putx([0, w]) self.ss, self.es = self.ss_byte, self.samplenum - self.putx([proto[cmd][0], ['%s: %02X' % (proto[cmd][1], d), - '%s: %02X' % (proto[cmd][2], d), '%02X' % d]]) + self.putx([proto[cmd][0], ['%s: {$}' % proto[cmd][1], '%s: {$}' % proto[cmd][2], '{$}', d]]) # Done with this packet. self.bitcount = self.databyte = 0 diff --git a/decoders/1-uart/pd.py b/decoders/1-uart/pd.py index 1984f4e1..f3d6181c 100755 --- a/decoders/1-uart/pd.py +++ b/decoders/1-uart/pd.py @@ -253,10 +253,12 @@ class Decoder(srd.Decoder): self.datavalue = bitpack(bits) self.putpx(['DATA', 0, (self.datavalue, self.databits)]) + self.putx([0, [self.datavalue]]) + b = self.datavalue - formatted = self.format_value(b) - if formatted is not None: - self.putx([0, [formatted]]) + #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]) diff --git a/libsigrokdecode4DSL/libsigrokdecode.h b/libsigrokdecode4DSL/libsigrokdecode.h index 9bc23973..a399de18 100755 --- a/libsigrokdecode4DSL/libsigrokdecode.h +++ b/libsigrokdecode4DSL/libsigrokdecode.h @@ -348,6 +348,7 @@ struct srd_proto_data_annotation { int ann_class; int ann_type; char str_number_hex[18]; //numerical value hex format string + long long numberic_value; char **ann_text; //text string lines }; struct srd_proto_data_binary { diff --git a/libsigrokdecode4DSL/type_decoder.c b/libsigrokdecode4DSL/type_decoder.c index 86599c6b..6199652b 100755 --- a/libsigrokdecode4DSL/type_decoder.c +++ b/libsigrokdecode4DSL/type_decoder.c @@ -53,7 +53,7 @@ static void release_annotation(struct srd_proto_data_annotation *pda) g_strfreev(pda->ann_text); } -static int py_parse_ann_data(PyObject *list_obj, char ***out_strv, int list_size, char *hex_str_buf) +static int py_parse_ann_data(PyObject *list_obj, char ***out_strv, int list_size, char *hex_str_buf, long long *numberic_value) { PyObject *py_item, *py_bytes; char **strv, *str; @@ -93,7 +93,8 @@ static int py_parse_ann_data(PyObject *list_obj, char ***out_strv, int list_size if (py_numobj != NULL){ lv = PyLong_AsLongLong(py_numobj); - sprintf(hex_str_buf, "%02llX", lv); + sprintf(hex_str_buf, "%02llX", lv); + *numberic_value = lv; } //have no text, only one numberical @@ -209,7 +210,9 @@ static int convert_annotation(struct srd_decoder_inst *di, PyObject *obj, pda->str_number_hex[0] = 0; ann_text = NULL; - if (py_parse_ann_data(py_tmp, &ann_text, ann_size, pda->str_number_hex) != SRD_OK) { + pda->numberic_value = 0; + + if (py_parse_ann_data(py_tmp, &ann_text, ann_size, pda->str_number_hex, &pda->numberic_value) != SRD_OK) { srd_err("Protocol decoder %s submitted annotation list, but " "second element was malformed.", di->decoder->name); goto err; diff --git a/qtpro/DSView.pro.user b/qtpro/DSView.pro.user index 6fe947fc..e06307fe 100644 --- a/qtpro/DSView.pro.user +++ b/qtpro/DSView.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId