#include "datasource.h" #include "helpers.h" static constexpr unsigned DATASIZE = 0x2000; static const uint8_t VoltageRangeTable [] = { S100mV, S200mV, S500mV, S1V, S2V, S5V, S10V, S20V, S50V, }; DataSource::DataSource(QObject * p) : QObject (p), usart (QString("/dev/serial/by-id/usb-Kizarm_Labs._USB_Osciloscope_00001-if00")), trigerSettings () { in_buffer = new char [DATASIZE]; packet_buf = new char [DATASIZE]; packet_cnt = 0; state = StateIdle; catching = true; connect (&usart, SIGNAL (readyRead()), this, SLOT (read_data())); usart.open (QIODevice::ReadWrite); usart.write("?\r\n", 3); } DataSource::~DataSource() { usart.close(); delete [] in_buffer; delete [] packet_buf; } void DataSource::Start() { catching = true; } void DataSource::read_data() { const long numRead = usart.read(in_buffer, DATASIZE); if (numRead == 0) return; // qDebug ("readen = %ld", numRead); parse_input (in_buffer, numRead); } void DataSource::parse_input(const char * data, const long len) { for (long i=0; i= (int) DATASIZE) { packet_cnt = 0; qDebug ("Buffer overflow"); } break; } } } void DataSource::parse_header() { if (packet_cnt != 4) return; packet_buf [4] = '\0'; QString h (packet_buf); bool ok = false; const unsigned hdr = h.toUInt(&ok, 16); if (!ok) return; header.common = hdr; //qDebug ("header:%04X", hdr); if (header.bits.trig_flg) { emit PaketTriggered (); } } static uint8_t from_hex (const char * ptr) { uint8_t result = 0u; for (unsigned n=0; n<2u; n++) { result *= 0x10; const char c = ptr [n]; const uint8_t v = c > '9' ? c - 'A' + 10 : c - '0'; result += v; } return result; } static uint8_t find_index (uint8_t n) { unsigned m = sizeof (VoltageRangeTable); for (unsigned i=0u; i> 1; int k = 0; for (int n=0; n ChA, ChB; if (state != StateData) return; bool ok = false; int k=0; for (int n=0; n (n); send_trig_mode(); } void DataSource::SendTrigerEdge(int n) { trigerSettings.rising = n ? true : false; send_trig_mode(); } void DataSource::SendTrigerChan(int n) { trigerSettings.channel = static_cast (n); send_trig_mode(); } void DataSource::send_trig_mode () { RcvdHeader hdr; hdr.common = 0u; hdr.bits.destinat = DEST_TRIG; hdr.bits.cmd_type = TRIGGER_CMD_MODE; TriggerModeUnion tmu; tmu.common = 0u; tmu.bits.mode = trigerSettings.mode; tmu.bits.channel = trigerSettings.channel; tmu.bits.rissing = trigerSettings.rising ? 1u : 0u; hdr.bits.cmd_value = tmu.common; const unsigned len = 64; char buffer [len]; int r = snprintf(buffer, len, "$%04X\r\n", (int) hdr.common); buffer [r] = '\0'; usart.write (buffer, r); qDebug ("%d:%s", r, strip_eol(buffer)); } void DataSource::SettingChanged(int n) { const MOVE_ITEMS items = static_cast(n); RcvdHeader hdr; hdr.common = 0u; hdr.bits.destinat = DEST_TRIG; switch (items) { case MOVE_VALUE: hdr.bits.cmd_type = TRIGGER_CMD_VALUE; hdr.bits.cmd_value = trigerSettings.value; break; case MOVE_OFSET: hdr.bits.cmd_type = TRIGGER_CMD_OFSET; hdr.bits.cmd_value = trigerSettings.offset; break; default : break; } const unsigned len = 64; char buffer [len]; int r = snprintf(buffer, len, "$%04X\r\n", (int) hdr.common); buffer [r] = '\0'; usart.write (buffer, r); // qDebug ("%d::%d:%s", n, r, strip_eol(buffer)); } void DataSource::SendBaseRange (int n) { RcvdHeader hdr; hdr.common = 0u; hdr.bits.destinat = DEST_BASE; hdr.bits.cmd_value = n; const unsigned len = 64; char buffer [len]; int r = snprintf(buffer, len, "$%04X\r\n", (int) hdr.common); buffer [r] = '\0'; usart.write (buffer, r); qDebug ("%d:%s", r, strip_eol(buffer)); } void DataSource::SendVoltage(int ch, int n) { const int k = (unsigned) n >= sizeof (VoltageRangeTable) ? (sizeof (VoltageRangeTable) - 1u) : n; uint8_t v = VoltageRangeTable [k]; RcvdHeader hdr; hdr.common = 0u; hdr.bits.destinat = ch == 0 ? DEST_CHA : DEST_CHB; hdr.bits.cmd_value = v; const unsigned len = 64; char buffer [len]; int r = snprintf(buffer, len, "$%04X\r\n", (int) hdr.common); buffer [r] = '\0'; usart.write (buffer, r); qDebug ("%d:%s", r, strip_eol(buffer)); }