can now flash to eeprom

This commit is contained in:
Jay Robson 2024-08-28 13:10:06 +10:00
parent 4ade94e285
commit 575b03337e
6 changed files with 68 additions and 58 deletions

View File

@ -33,10 +33,10 @@ unsigned binary::process(std::ostream& dst, const char* path) {
return notes_added; return notes_added;
} }
bool binary::process_all(std::string& dst, int argc, char **argv) { bool binary::process_all(std::string& dst, int start, int argc, char **argv) {
if(argc > 2) { if(argc > start) {
std::stringstream ss; std::stringstream ss;
for(int i = 2; i < argc; i++) { for(int i = start; i < argc; i++) {
binary::process(ss, argv[i]); binary::process(ss, argv[i]);
} }
binary::finalize(ss); binary::finalize(ss);
@ -44,7 +44,7 @@ bool binary::process_all(std::string& dst, int argc, char **argv) {
return true; return true;
} }
else if(argc == 2) { else if(argc == start) {
dst = std::string(std::istreambuf_iterator<char>(std::cin), std::istreambuf_iterator<char>()); dst = std::string(std::istreambuf_iterator<char>(std::cin), std::istreambuf_iterator<char>());
return true; return true;
} }

View File

@ -6,7 +6,7 @@
namespace binary { namespace binary {
unsigned process(std::ostream& dst, const char* path); 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); void finalize(std::ostream& dst);
} }

View File

@ -2,6 +2,7 @@
#include "device.hpp" #include "device.hpp"
#include <cassert> #include <cassert>
#include <cstring>
#include <fcntl.h> #include <fcntl.h>
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
@ -14,8 +15,13 @@ Device::Device(const char* path) {
cfsetspeed(&tio, B57600); cfsetspeed(&tio, B57600);
cfmakeraw(&tio); cfmakeraw(&tio);
tio.c_cflag |= CRTSCTS;
assert(tcsetattr(fd, TCSANOW, &tio) == 0); 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() { Device::~Device() {

View File

@ -8,11 +8,9 @@ struct Device {
enum Mode { enum Mode {
e_none, e_none,
e_ready,
e_local, e_local,
e_serial, e_stream,
e_read, e_flash,
e_jump,
}; };
Device(const char* path); Device(const char* path);
@ -32,6 +30,8 @@ struct Device {
write(buff, len); write(buff, len);
} }
bool fingerprint_valid;
private: private:
int fd; int fd;
}; };

View File

@ -29,7 +29,7 @@ int main(int argc, char** argv) {
std::string data; 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"; std::cerr << "Usage: " << argv[0] << " varname [midifile ...]?\n\n";
return 1; return 1;
} }

View File

@ -1,6 +1,7 @@
#include "binary.hpp" #include "binary.hpp"
#include "device.hpp" #include "device.hpp"
#include <cstring>
#include <iostream> #include <iostream>
#include <ostream> #include <ostream>
#include <string> #include <string>
@ -13,17 +14,28 @@ static void display_progress(unsigned long at, unsigned long size) {
struct winsize w; struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
static unsigned long screen_at_last = -1;
static unsigned percent_at_last = -1;
unsigned cols = w.ws_col - 8; 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 << "["; std::cout << "[";
unsigned long j = at * cols / size;
for(unsigned long i = 0; i < cols; i++) { for(unsigned long i = 0; i < cols; i++) {
char ch; char ch;
if(i > j) { if(i > screen_at) {
ch = ' '; ch = ' ';
} else if(i == j) { } else if(i == screen_at) {
ch = '>'; ch = '>';
} else { } else {
ch = '='; ch = '=';
@ -31,59 +43,51 @@ static void display_progress(unsigned long at, unsigned long size) {
std::cout << ch; std::cout << ch;
} }
std::cout << "] " << (at * 100 / size) << "%\r" << std::flush; std::cout << "] " << percent_at << "%\r" << std::flush;
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
std::string data; std::string data;
if(!binary::process_all(data, argc, argv)) { if(argc < 3 || argv[1][0] != '-' || argv[1][1] == '\0' || argv[1][2] != '\0') {
std::cerr << "Usage: " << argv[0] << " device [midifile ...]?\n\n"; 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; return 1;
} }
std::cout << "loaded: " << data.length() << " bytes" << std::endl; std::cout << "loaded: " << data.length() << " bytes" << std::endl;
unsigned at = 0; Device device(argv[2]);
Device device(argv[1]);
device.put(Device::Mode::e_serial);
for(;;) { if(device.fingerprint_valid) {
Device::Mode mode = (Device::Mode)device.get(); std::cout << "device: connected" << std::endl;
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 { } else {
device.write(data.c_str() + at, len); std::cerr << "error: invalid fingerprint" << std::endl;
at += len; return 1;
} }
break;
}}
display_progress(at, data.length()); device.put(mode);
for(int i = 0; i < data.length(); i++) {
device.put(data[i]);
display_progress(i, data.length());
} }
} }