Skip to content

Commit

Permalink
added progress bar, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rnayabed committed May 30, 2023
1 parent cceb26e commit a42f98a
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 39 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,22 @@ NOTE: you cannot use --aries and --xmodem-block-size / --serial* arguments (exce
## Note

vegadude does not play well with sniffer programs like [interceptty](https://github.com/geoffmeyers/interceptty), *for now*.

## License

vegadude is licensed to [GNU General Public License v3.0](https://github.com/rnayabed/vegadude/blob/master/LICENSE).

```
vegadude - Utility application to upload programs on CDAC's VEGA Microprocessor powered boards.
Copyright (C) 2023 Debayan Sutradhar (rnayabed)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
```

11 changes: 4 additions & 7 deletions device.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#include "device.h"

Device::Device()
{}

bool Device::read(unsigned char* bytes)
bool Device::read(unsigned char* byte)
{
return read(bytes, 1);
return read(std::span(byte, 1));
}

bool Device::write(const unsigned char* bytes)
bool Device::write(const unsigned char* byte)
{
return write(bytes, 1);
return write(std::span(byte, 1));
}
9 changes: 4 additions & 5 deletions device.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
class Device
{
public:
Device();

bool read(unsigned char* bytes);
bool write(const unsigned char* bytes);
virtual bool read(std::span<unsigned char> bytes) = 0;
virtual bool write(std::span<const unsigned char> bytes) = 0;

virtual bool read(unsigned char* bytes, size_t size) = 0;
virtual bool write(const unsigned char* bytes, size_t size) = 0;
bool read(unsigned char* byte);
bool write(const unsigned char* byte);
};

#endif // DEVICE_H
17 changes: 17 additions & 0 deletions logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ bool Logger::setup(std::filesystem::path& filePath)
return m_log;
}

void Logger::showProgress(const std::string& message, const float &ratio)
{
constexpr int32_t barSize = 50;
int32_t completeSize = ratio * barSize;
int32_t remainingSize = barSize - completeSize;

std::cerr << "\rProgress [";
for (int32_t i = 0; i < completeSize; i++)
std::cerr << "#";
for (int32_t i = 0; i < remainingSize; i++)
std::cerr << "-";
std::cerr << "] - "
<< static_cast<int>(ratio * 100)
<< "% - "
<< message;
}

void Logger::close()
{
get() << NewLine;
Expand Down
3 changes: 3 additions & 0 deletions logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Logger

static Logger& get();
bool setup(std::filesystem::path& filePath);

void showProgress(const std::string& message, const float& ratio);

void close();

constexpr static char NewLine {'\n'};
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ int main(int argc, char** argv)
<< "Read Timeout (in milliseconds): " << serialReadTimeout << Logger::NewLine
<< "XMODEM Block Size " << xmodemBlockSize << Logger::NewLine
<< "XMODEM Max Retry: " << xmodemMaxRetry << Logger::NewLine
<< "================================================" << Logger::NewLine;
<< "================================================" << Logger::NewLine << Logger::NewLine;

SerialDevice device{targetPath, dp, serialReadTimeout};

Expand Down
14 changes: 9 additions & 5 deletions serialdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ bool SerialDevice::open()

COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = m_readTimeout;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;

if (SetCommTimeouts(m_winHandle, &timeouts) == FALSE)
{
Expand All @@ -292,9 +296,9 @@ bool SerialDevice::close()
return false;
}

bool SerialDevice::read(unsigned char *bytes, size_t size)
bool SerialDevice::read(std::span<unsigned char> bytes)
{
if (ReadFile(m_winHandle, bytes, size, NULL, NULL))
if (ReadFile(m_winHandle, bytes.data(), bytes.size(), NULL, NULL))
{
m_error = Error::NONE;
return true;
Expand All @@ -306,12 +310,12 @@ bool SerialDevice::read(unsigned char *bytes, size_t size)
}
}

bool SerialDevice::write(const unsigned char *bytes, size_t size)
bool SerialDevice::write(std::span<const unsigned char> bytes)
{
unsigned long written;
bool result = WriteFile(m_winHandle, bytes, size, &written, NULL);
bool result = WriteFile(m_winHandle, bytes.data(), bytes.size(), &written, NULL);

if (result && size != written)
if (result && bytes.size() == written)
{
m_error = Error::NONE;
return true;
Expand Down
4 changes: 2 additions & 2 deletions serialdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class SerialDevice : public Device
const Error& error();
std::string errorStr();

bool read(unsigned char* bytes, size_t size);
bool write(const unsigned char* bytes, size_t size);
bool read(std::span<unsigned char> bytes);
bool write(std::span<const unsigned char> bytes);

bool open();
bool close();
Expand Down
42 changes: 23 additions & 19 deletions xmodem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <fstream>
#include <istream>
#include <iostream>
#include <cmath>

#include "logger.h"

Expand Down Expand Up @@ -53,16 +54,17 @@ std::string XModem::errorStr()

bool XModem::upload(const std::filesystem::path &filePath, const bool& startAfterUpload)
{
unsigned char blockNumber1 = 1;
unsigned char blockNumber1;
unsigned char blockNumber2;
unsigned char crcBlock[2];

size_t g = 0;
size_t currentBlock;
int32_t currentTry = 0;

std::ifstream file{filePath, std::ios_base::in | std::ios_base::binary};
size_t fileSize = std::filesystem::file_size(filePath);
size_t noOfBlocks = std::ceil(float(fileSize) / float(m_blockSize));

std::cout << "FILE PATH" << filePath << "\nm_blockSize:" << m_blockSize << std::endl;
std::ifstream file{filePath, std::ios_base::in | std::ios_base::binary};

if(!file.is_open())
{
Expand All @@ -82,33 +84,26 @@ bool XModem::upload(const std::filesystem::path &filePath, const bool& startAfte
return false;
}

if (currentTry >= m_maxRetry)
{
m_error = Error::MAX_RETRY_SURPASSED;
return false;
}

currentTry++;

Logger::get() << "READ: " << +rb << Logger::NewLine;

if (rb != XModem::NAK)
{
if (rb == XModem::C)
{
blockNumber1 = 0;
currentBlock = 1;
currentTry = 0;
file.seekg(0);
}
else if (rb == XModem::ACK)
{

if (blockNumber1 == 255)
blockNumber1 = 0;
else
blockNumber1++;

currentTry = 0;

g++;
currentBlock++;
}
else if (rb == XModem::CAN)
{
Expand All @@ -127,6 +122,7 @@ bool XModem::upload(const std::filesystem::path &filePath, const bool& startAfte
if (startAfterUpload)
if (!m_device.write(&CR)) break;

Logger::get() << Logger::NewLine;
file.close();
m_error = Error::NONE;
return true;
Expand All @@ -135,7 +131,6 @@ bool XModem::upload(const std::filesystem::path &filePath, const bool& startAfte
{
file.read(reinterpret_cast<char*>(fileBlocks.data()), m_blockSize);

std::cout << "file.gcount(): " << file.gcount() << std::endl;
if (file.gcount() != m_blockSize)
{
for (int32_t i = file.gcount(); i < m_blockSize; i++)
Expand All @@ -144,6 +139,14 @@ bool XModem::upload(const std::filesystem::path &filePath, const bool& startAfte
}
}

if (currentTry >= m_maxRetry)
{
m_error = Error::MAX_RETRY_SURPASSED;
return false;
}

currentTry++;

blockNumber2 = 255 - blockNumber1;
uint16_t crc = CRC::generateCRC16CCITT(fileBlocks);
crcBlock[0] = crc >> 8;
Expand All @@ -152,10 +155,11 @@ bool XModem::upload(const std::filesystem::path &filePath, const bool& startAfte
if (!m_device.write(&XModem::SOH) ||
!m_device.write(&blockNumber1) ||
!m_device.write(&blockNumber2) ||
!m_device.write(fileBlocks.data(), fileBlocks.size()) ||
!m_device.write(crcBlock, 2)) break;
!m_device.write(fileBlocks) ||
!m_device.write(std::span(crcBlock, 2))) break;

Logger::get() << "Sent block " << g << Logger::NewLine;
Logger::get().showProgress("Sent block " + std::to_string(currentBlock) + "/" + std::to_string(noOfBlocks),
(float(currentBlock)/float(noOfBlocks)));
}

m_error = Error::DEVICE_RELATED;
Expand Down

0 comments on commit a42f98a

Please sign in to comment.