| 72 | } |
| 73 | |
| 74 | void CanSignalEncoderPrivate::encodeSignal(const QString& name, const QVariant& val) |
| 75 | { |
| 76 | auto nameSplit = name.split('_'); |
| 77 | |
| 78 | if (nameSplit.size() < 2) { |
| 79 | cds_error("Wrong signal name: {}", name.toStdString()); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | uint32_t id = nameSplit[0].toUInt(nullptr, 16); |
| 84 | |
| 85 | const CANmessages_t::value_type* msgDesc = nullptr; |
| 86 | if (_db.getDb().count(id) > 0) { |
| 87 | auto iter = _db.getDb().find(id); |
| 88 | |
| 89 | if (iter != _db.getDb().end()) { |
| 90 | msgDesc = &(*iter); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | QString sigName = name.mid(name.indexOf("_") + 1); |
| 95 | |
| 96 | if (!msgDesc) { |
| 97 | cds_warn("Msg '{:03x}' not found in DB", id); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | for (auto& sig : msgDesc->second) { |
| 102 | if (sig.signal_name == sigName.toStdString()) { |
| 103 | if (_rawCache[id].size() < (int)msgDesc->first.dlc) { |
| 104 | cds_info("Setting up new cache for {:03x} msg", id); |
| 105 | // Set cache for the first time |
| 106 | _rawCache[id].fill(0, msgDesc->first.dlc); |
| 107 | } |
| 108 | |
| 109 | // Calculate how many bits are used already before this signal. Calculations are different for |
| 110 | // little and big endian. Good overview on how big endian signals are aligned can be found |
| 111 | // here: https://github.com/eerimoq/cantools#the-dump-subcommand |
| 112 | uint8_t bitsBefore = 0; |
| 113 | bool littleEndian = sig.byteOrder == 1; |
| 114 | uint8_t msgSize = _rawCache[id].size(); |
| 115 | |
| 116 | if (littleEndian) { |
| 117 | bitsBefore = sig.startBit; |
| 118 | } else { |
| 119 | bitsBefore = beTransTable[sig.startBit]; |
| 120 | } |
| 121 | |
| 122 | if (bitsBefore + sig.signalSize > (msgSize * 8)) { |
| 123 | cds_error("Invalid signal or message size - startBit {}, sigSize {}, bitsBefore {}, payload size {}, " |
| 124 | "littleEndian: {}", |
| 125 | sig.startBit, sig.signalSize, bitsBefore, msgSize, littleEndian); |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | signalToRaw(id, sig, val, msgDesc->first.updateCycle); |
| 130 | return; |
| 131 | } |