diff --git a/lib/fluxsource/catweasel.cc b/lib/fluxsource/catweasel.cc new file mode 100644 index 00000000..2598c8cf --- /dev/null +++ b/lib/fluxsource/catweasel.cc @@ -0,0 +1,36 @@ +#include "lib/globals.h" +#include "lib/fluxmap.h" +#include "lib/bytes.h" +#include "lib/fluxsource/catweasel.h" + +std::unique_ptr decodeCatweaselData( + const Bytes& bytes, nanoseconds_t clock) +{ + std::unique_ptr fluxmap(new Fluxmap); + uint32_t pending = 0; + bool oldindex = true; + const uint8_t* ptr = bytes.begin(); + while (ptr != bytes.end()) + { + uint32_t b = *ptr++; + bool index = !!(b & 0x80); + b &= 0x7f; + if (b == 0x7f) + { + pending += 0x7f; + continue; + } + b += pending; + pending = 0; + + double interval_ns = b * clock; + fluxmap->appendInterval(interval_ns / NS_PER_TICK); + fluxmap->appendPulse(); + + if (index && !oldindex) + fluxmap->appendIndex(); + oldindex = index; + } + + return fluxmap; +} diff --git a/lib/fluxsource/catweasel.h b/lib/fluxsource/catweasel.h new file mode 100644 index 00000000..07c1464f --- /dev/null +++ b/lib/fluxsource/catweasel.h @@ -0,0 +1,10 @@ +#ifndef CATWEASEL_H +#define CATWEASEL_H + +class Fluxmap; +class Bytes; + +extern std::unique_ptr decodeCatweaselData( + const Bytes& bytes, nanoseconds_t clock); + +#endif diff --git a/lib/fluxsource/dmkfluxsource.cc b/lib/fluxsource/dmkfluxsource.cc new file mode 100644 index 00000000..5d526335 --- /dev/null +++ b/lib/fluxsource/dmkfluxsource.cc @@ -0,0 +1,77 @@ +#include "lib/globals.h" +#include "lib/fluxmap.h" +#include "lib/fluxsource/fluxsource.pb.h" +#include "lib/fluxsource/fluxsource.h" +#include "lib/fluxsource/catweasel.h" +#include "lib/proto.h" +#include "lib/logger.h" +#include +#include + +class DmkFluxSourceIterator : public FluxSourceIterator +{ +public: + DmkFluxSourceIterator(const std::string& path, int track, int side): + _path(path), + _track(track), + _side(side) + { + } + + bool hasNext() const override + { + std::string p = getPath(); + return std::filesystem::exists(getPath()); + } + + std::unique_ptr next() override + { + std::string path = getPath(); + log("DMK: reading {}", path); + std::ifstream ifstream(getPath(), std::ios::binary); + if (!ifstream) + return std::make_unique(); + _count++; + + Bytes bytes(ifstream); + return decodeCatweaselData(bytes, 1e9 / 7080500.0); + } + +private: + const std::string getPath() const + { + return fmt::format( + "{}/C_S{:01}T{:02}.{:03}", _path, _side, _track, _count); + } + +private: + const std::string _path; + const int _track; + const int _side; + int _count = 0; +}; + +class DmkFluxSource : public FluxSource +{ +public: + DmkFluxSource(const DmkFluxSourceProto& config): _path(config.directory()) + { + } + +public: + std::unique_ptr readFlux(int track, int side) override + { + return std::make_unique(_path, track, side); + } + + void recalibrate() override {} + +private: + const std::string _path; +}; + +std::unique_ptr FluxSource::createDmkFluxSource( + const DmkFluxSourceProto& config) +{ + return std::unique_ptr(new DmkFluxSource(config)); +}