StreamAdapter.h
Go to the documentation of this file.
1 /****
2  * StreamAdapter.h
3  *
4  * Copyright 2020 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the HardwareSPI Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  * @author: October 2020 - mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "MemoryDevice.h"
26 #include <Interrupts.h>
27 
28 namespace HSPI
29 {
34 class StreamAdapter
35 {
36 public:
37  StreamAdapter(MemoryDevice& device);
38 
39  bool write(IDataSourceStream* source, uint32_t address, size_t len, InterruptDelegate callback);
40 
41  bool read(ReadWriteStream* dest, uint32_t address, size_t len, InterruptDelegate callback);
42 
43  bool getIsWrite() const
44  {
45  return isWrite;
46  }
47 
48  size_t getBytesRequested() const
49  {
50  return bytesRequested;
51  }
52 
53  size_t getBytesTransferred() const
54  {
55  return bytesTransferred;
56  }
57 
58 private:
59  struct Buffer {
60  static constexpr size_t size{1024};
61  HSPI::Request req;
62  char data[size];
63  };
64 
65  void task();
66  unsigned writeChunks();
67  bool writeChunk();
68  unsigned readChunks();
69  bool readChunk();
70  static bool requestComplete(HSPI::Request& req);
71 
72  MemoryDevice& device;
73  InterruptDelegate callback;
74  // Stream* stream{nullptr};
75  IDataSourceStream* stream{nullptr};
76  bool isWrite{false};
77  uint32_t address{0};
78  size_t bytesRequested{0};
79  size_t bytesTransferred{0};
80  static constexpr size_t bufCount{2};
81  Buffer buffers[bufCount];
82  uint8_t index{0};
83  bool taskQueued{false};
84 };
85 
86 } // namespace HSPI
Base class for read-only stream.
Definition: DataSourceStream.h:45
Definition: Common.h:34
size_t getBytesRequested() const
Definition: StreamAdapter.h:86
bool write(IDataSourceStream *source, uint32_t address, size_t len, InterruptDelegate callback)
bool read(ReadWriteStream *dest, uint32_t address, size_t len, InterruptDelegate callback)
StreamAdapter(MemoryDevice &device)
Defines an SPI Request Packet.
Definition: HardwareSPI/src/include/HSPI/Request.h:79
bool getIsWrite() const
Definition: StreamAdapter.h:81
size_t getBytesTransferred() const
Definition: StreamAdapter.h:91
Base class for read/write stream.
Definition: ReadWriteStream.h:19