can now flash to eeprom
This commit is contained in:
parent
4ade94e285
commit
575b03337e
|
@ -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<char>(std::cin), std::istreambuf_iterator<char>());
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#include "device.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
@ -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() {
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
|
||||
#include "binary.hpp"
|
||||
#include "device.hpp"
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
@ -13,17 +14,28 @@ static void display_progress(unsigned long at, unsigned long size) {
|
|||
|
||||
struct winsize 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 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 << "[";
|
||||
|
||||
unsigned long j = at * cols / size;
|
||||
|
||||
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);
|
||||
Device device(argv[2]);
|
||||
|
||||
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;
|
||||
}}
|
||||
if(device.fingerprint_valid) {
|
||||
std::cout << "device: connected" << std::endl;
|
||||
} else {
|
||||
std::cerr << "error: invalid fingerprint" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue