From 575b03337efeb257a5b188eee3e428495ab150d5 Mon Sep 17 00:00:00 2001 From: Jay Robson Date: Wed, 28 Aug 2024 13:10:06 +1000 Subject: [PATCH] can now flash to eeprom --- src/binary.cpp | 8 ++-- src/binary.hpp | 2 +- src/device.cpp | 6 +++ src/device.hpp | 8 ++-- src/header.cpp | 2 +- src/streamer.cpp | 100 ++++++++++++++++++++++++----------------------- 6 files changed, 68 insertions(+), 58 deletions(-) diff --git a/src/binary.cpp b/src/binary.cpp index 978d620..d24415b 100644 --- a/src/binary.cpp +++ b/src/binary.cpp @@ -33,10 +33,10 @@ unsigned binary::process(std::ostream& dst, const char* path) { return notes_added; } -bool binary::process_all(std::string& dst, int argc, char **argv) { - if(argc > 2) { +bool binary::process_all(std::string& dst, int start, int argc, char **argv) { + if(argc > start) { std::stringstream ss; - for(int i = 2; i < argc; i++) { + for(int i = start; i < argc; i++) { binary::process(ss, argv[i]); } binary::finalize(ss); @@ -44,7 +44,7 @@ bool binary::process_all(std::string& dst, int argc, char **argv) { return true; } - else if(argc == 2) { + else if(argc == start) { dst = std::string(std::istreambuf_iterator(std::cin), std::istreambuf_iterator()); return true; } diff --git a/src/binary.hpp b/src/binary.hpp index 8d8ee50..f12c0d9 100644 --- a/src/binary.hpp +++ b/src/binary.hpp @@ -6,7 +6,7 @@ namespace binary { unsigned process(std::ostream& dst, const char* path); - bool process_all(std::string& dst, int argc, char** argv); + bool process_all(std::string& dst, int start, int argc, char** argv); void finalize(std::ostream& dst); } diff --git a/src/device.cpp b/src/device.cpp index e7c3daa..3149b0c 100644 --- a/src/device.cpp +++ b/src/device.cpp @@ -2,6 +2,7 @@ #include "device.hpp" #include +#include #include #include #include @@ -14,8 +15,13 @@ Device::Device(const char* path) { cfsetspeed(&tio, B57600); cfmakeraw(&tio); + tio.c_cflag |= CRTSCTS; assert(tcsetattr(fd, TCSANOW, &tio) == 0); + + char buff[8]; + read(buff, sizeof(buff)); + fingerprint_valid = (std::memcmp(buff, "\x19\xc1\x6c\x19\x5b\x6f\xd2\x44", sizeof(buff)) == 0); } Device::~Device() { diff --git a/src/device.hpp b/src/device.hpp index a5ab56b..1b301de 100644 --- a/src/device.hpp +++ b/src/device.hpp @@ -8,11 +8,9 @@ struct Device { enum Mode { e_none, - e_ready, e_local, - e_serial, - e_read, - e_jump, + e_stream, + e_flash, }; Device(const char* path); @@ -32,6 +30,8 @@ struct Device { write(buff, len); } + bool fingerprint_valid; + private: int fd; }; diff --git a/src/header.cpp b/src/header.cpp index b489281..ef2db9b 100644 --- a/src/header.cpp +++ b/src/header.cpp @@ -29,7 +29,7 @@ int main(int argc, char** argv) { std::string data; - if(!binary::process_all(data, argc, argv)) { + if(!binary::process_all(data, 2, argc, argv)) { std::cerr << "Usage: " << argv[0] << " varname [midifile ...]?\n\n"; return 1; } diff --git a/src/streamer.cpp b/src/streamer.cpp index 590ef8c..6350cb8 100644 --- a/src/streamer.cpp +++ b/src/streamer.cpp @@ -1,6 +1,7 @@ #include "binary.hpp" #include "device.hpp" +#include #include #include #include @@ -13,17 +14,28 @@ static void display_progress(unsigned long at, unsigned long size) { struct winsize w; ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); - unsigned cols = w.ws_col - 8; - - std::cout << "["; - unsigned long j = at * cols / size; + static unsigned long screen_at_last = -1; + static unsigned percent_at_last = -1; + + unsigned cols = w.ws_col - 8; + unsigned long screen_at = at * cols / size; + unsigned percent_at = (at * 100 / size); + + if(screen_at == screen_at_last && percent_at == percent_at_last) { + return; + } + + screen_at_last = screen_at; + percent_at_last = percent_at; + + std::cout << "["; for(unsigned long i = 0; i < cols; i++) { char ch; - if(i > j) { + if(i > screen_at) { ch = ' '; - } else if(i == j) { + } else if(i == screen_at) { ch = '>'; } else { ch = '='; @@ -31,59 +43,51 @@ static void display_progress(unsigned long at, unsigned long size) { std::cout << ch; } - std::cout << "] " << (at * 100 / size) << "%\r" << std::flush; + std::cout << "] " << percent_at << "%\r" << std::flush; } int main(int argc, char** argv) { std::string data; - if(!binary::process_all(data, argc, argv)) { - std::cerr << "Usage: " << argv[0] << " device [midifile ...]?\n\n"; + if(argc < 3 || argv[1][0] != '-' || argv[1][1] == '\0' || argv[1][2] != '\0') { + goto error_help; + } + + Device::Mode mode; + + switch(argv[1][1]) { + case 'f': + mode = Device::Mode::e_flash; + break; + case 's': + mode = Device::Mode::e_stream; + break; + default: + goto error_help; + } + + if(!binary::process_all(data, 3, argc, argv)) { +error_help: + std::cerr << "Usage: " << argv[0] << " -[s|f] device [midifile ...]?\n\n"; return 1; } std::cout << "loaded: " << data.length() << " bytes" << std::endl; - unsigned at = 0; - Device device(argv[1]); - device.put(Device::Mode::e_serial); - - for(;;) { - Device::Mode mode = (Device::Mode)device.get(); - switch(mode) { - default: { - std::cout << CLEAR_LINE << "device: unknown (" << mode << ")" << std::endl; - break; - } - case Device::Mode::e_ready: { - device.put(Device::Mode::e_serial); - std::cout << CLEAR_LINE << "device: ready" << std::endl; - break; - } - case Device::Mode::e_jump: { - at = (device.get() & 0xff); - at = (at << 8) | (device.get() & 0xff); - at = (at << 8) | (device.get() & 0xff); - at = (at << 8) | (device.get() & 0xff); - break; - } - case Device::Mode::e_read: { - unsigned len = (device.get() & 0xff); - if(len + at > data.length()) { - unsigned l = data.length() - at; - device.write(data.c_str() + at, l); - for(int i = 0; i < len - l; i++) { - device.put(0); - } - at += l; - } else { - device.write(data.c_str() + at, len); - at += len; - } - break; - }} + Device device(argv[2]); - display_progress(at, data.length()); + if(device.fingerprint_valid) { + std::cout << "device: connected" << std::endl; + } else { + std::cerr << "error: invalid fingerprint" << std::endl; + return 1; + } + + device.put(mode); + + for(int i = 0; i < data.length(); i++) { + device.put(data[i]); + display_progress(i, data.length()); } }