StreamAdapter.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/anakod/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * StreamAdapter.h
8  *
9  * @author: October 2020 - mikee47 <mike@sillyhouse.net>
10  *
11 */
12 
13 #pragma once
14 
15 #include "MemoryDevice.h"
17 #include <Interrupts.h>
18 
19 namespace HSPI
20 {
26 {
27 public:
28  StreamAdapter(MemoryDevice& device);
29 
30  bool write(IDataSourceStream* source, uint32_t address, size_t len, InterruptDelegate callback);
31 
32  bool read(ReadWriteStream* dest, uint32_t address, size_t len, InterruptDelegate callback);
33 
34  bool getIsWrite() const
35  {
36  return isWrite;
37  }
38 
39  size_t getBytesRequested() const
40  {
41  return bytesRequested;
42  }
43 
44  size_t getBytesTransferred() const
45  {
46  return bytesTransferred;
47  }
48 
49 private:
50  struct Buffer {
51  static constexpr size_t size{1024};
52  HSPI::Request req;
53  char data[size];
54  };
55 
56  void task();
57  unsigned writeChunks();
58  bool writeChunk();
59  unsigned readChunks();
60  bool readChunk();
61  static void requestComplete(HSPI::Request& req);
62 
63  MemoryDevice& device;
64  InterruptDelegate callback;
65  // Stream* stream{nullptr};
66  IDataSourceStream* stream{nullptr};
67  bool isWrite{false};
68  uint32_t address{0};
69  size_t bytesRequested{0};
70  size_t bytesTransferred{0};
71  static constexpr size_t bufCount{2};
72  Buffer buffers[bufCount];
73  uint8_t index{0};
74  bool taskQueued{false};
75 };
76 
77 } // namespace HSPI
bool write(IDataSourceStream *source, uint32_t address, size_t len, InterruptDelegate callback)
Defines an SPI Request Packet.
Definition: HardwareSPI/src/include/HSPI/Request.h:45
Base class for read-only stream.
Definition: DataSourceStream.h:40
StreamAdapter(MemoryDevice &device)
size_t getBytesRequested() const
Definition: StreamAdapter.h:39
Definition: Common.h:24
bool getIsWrite() const
Definition: StreamAdapter.h:34
bool read(ReadWriteStream *dest, uint32_t address, size_t len, InterruptDelegate callback)
size_t getBytesTransferred() const
Definition: StreamAdapter.h:44
Helper class for streaming data to/from SPI devices.
Definition: StreamAdapter.h:25
Base class for read/write stream.
Definition: ReadWriteStream.h:19
Base class for read/write addressable devices.
Definition: MemoryDevice.h:23