Downloader.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * TI Voxel Lib component.
  3. *
  4. * Copyright (c) 2014 Texas Instruments Inc.
  5. */
  6. #ifndef VOXEL_DOWNLOADER_H
  7. #define VOXEL_DOWNLOADER_H
  8. #include "Device.h"
  9. #include <fstream>
  10. #include <USBIO.h>
  11. #include "Logger.h"
  12. namespace Voxel
  13. {
  14. /**
  15. * \defgroup IO I/O classes
  16. * @{
  17. */
  18. class VOXEL_EXPORT Downloader
  19. {
  20. public:
  21. typedef Function<void(float)> ProgressFunctionType;
  22. protected:
  23. DevicePtr _device;
  24. LoggerOutStream _outStream;
  25. ProgressFunctionType _progressFunction;
  26. virtual bool _locateFile(String &file);
  27. float _progress = 0;
  28. inline void _setProgress(float progress /* percentage */)
  29. {
  30. _progress = (progress > 100?100:progress);
  31. if(_progress < 0)
  32. _progress = 0;
  33. if(_progressFunction)
  34. _progressFunction(progress);
  35. }
  36. inline void _setProgressIncrement(float progress /* percentage */)
  37. {
  38. _setProgress(_progress + progress);
  39. }
  40. public:
  41. Downloader(DevicePtr device): _device(device) {}
  42. virtual bool download(const String &file) = 0;
  43. inline void setLogCallback(LoggerOutStream::LoggerOutStreamFunctionType f) { _outStream.setOutputFunction(f); }
  44. inline void removeLogCallback() { _outStream.setOutputFunction(nullptr); }
  45. inline void setProgressFunction(ProgressFunctionType f) { _progressFunction = f; }
  46. inline void removeProgressFunction() { _progressFunction = nullptr; }
  47. inline float getProgress() { return _progress; }
  48. virtual ~Downloader() {}
  49. };
  50. typedef Ptr<Downloader> DownloaderPtr;
  51. class VOXEL_EXPORT USBDownloader: public Downloader
  52. {
  53. protected:
  54. USBIOPtr _usbIO;
  55. virtual bool _configureForDownload();
  56. virtual bool _download(InputFileStream &file, long unsigned int filesize);
  57. public:
  58. USBDownloader(DevicePtr device);
  59. virtual bool download(const String &file);
  60. virtual ~USBDownloader() {}
  61. };
  62. /**
  63. * @}
  64. */
  65. }
  66. #endif // VOXEL_DOWNLOADER_H