SCAMP-5c SPI Interface  1.0.0
SCAMP-5c is connected to the flight computer via a SPI interface
Scamp5cHost.hpp
Go to the documentation of this file.
1 
18 #ifndef SCAMP5C_SPI_HOST_HPP
19 #define SCAMP5C_SPI_HOST_HPP
20 
21 #include "scamp5c_spi_versions.hpp"
22 #include "jcStopwatch.hpp"
23 #include <functional>
24 
25 #define SPI_PACKET_TYPE_STANDARD_LOOPC 4
26 #define SPI_PACKET_TYPE_STANDARD_AOUT 5
27 #define SPI_PACKET_TYPE_STANDARD_DOUT 6
28 #define SPI_PACKET_TYPE_STANDARD_TARGET 7
29 #define SPI_PACKET_TYPE_STANDARD_EVENTS 8
30 #define SPI_PACKET_TYPE_STANDARD_APPINFO 10
31 
32 #define S5C_SPI_UNKNOWN 0
33 #define S5C_SPI_LOOPC 1
34 #define S5C_SPI_AOUT 2
35 #define S5C_SPI_DOUT 3
36 #define S5C_SPI_TARGET 4
37 #define S5C_SPI_EVENTS 5
38 #define S5C_SPI_APPINFO 6
39 
48 
49 
50 public:
51 
52  Scamp5cHost();
53  ~Scamp5cHost();
54 
75  void SetupSpi(scamp5c_spi_ht *spi_class);
76 
81  void Open();
82 
87  void Close();
88 
99  void Process();
100 
117  void RegisterStandardOutputCallback(int type,std::function<void(Scamp5cHost*)> func);
118 
119 
126  void RegisterGenericPacketCallback(std::function<void(Scamp5cHost*)> func);
127 
145  inline uint32_t GetDataDim(int d){
146  if(d==0){
147  return data_dim_r;
148  }else
149  if(d==1){
150  return data_dim_c;
151  }
152  return 0;
153  };
154 
170  inline uint8_t *GetData(){
171  return data_ptr;
172  };
173 
184  return original_packet;
185  };
186 
191  inline void SetInputPort(int i,uint8_t u){
192  Scamp5spi->ipu_port_forward[i] = u;
193  };
194 
195 
196  inline uint32_t GetPacketCount(){
197  return host_packet_count;
198  }
199  inline double GetPacketRate(){
200  return host_packet_rate;
201  }
202 
203  inline uint32_t GetLoopCounter(){
204  return loop_counter;
205  }
206  inline uint32_t GetLoopCounterError(){
207  return loop_counter_error;
208  }
209 
210  void RegisterErrorCallback(std::function<void(Scamp5cHost*)> func);
211  void ResetCounters();
212  int SaveFrameBMP(const char*filepath);
213  scamp5c_spi_ht *GetSpiClass(void);
214 
215 /*
216  inline uint8_t *SwapDataBuffer(uint8_t *new_buffer = NULL,size_t r = 0,size_t c = 1){
217  uint8_t *old_buffer = data_buffer;
218  data_dim_r = r;
219  data_dim_c = c;
220  data_buffer = new_buffer;
221  return old_buffer;
222  }
223 */
224 
225 protected:
226 
227  scamp5c_spi_ht *Scamp5spi;
228  jcStopwatch PacketStopwatch;
229  double host_packet_rate;
230  double host_packet_rate_e;
231  uint32_t host_packet_count;
232  scamp5c_spi::packet *original_packet;
233  int data_type;
234  uint16_t data_dim_r;
235  uint16_t data_dim_c;
236  uint8_t *data_ptr;
237  uint8_t *data_buffer;
238  uint32_t loop_counter;
239  int loop_counter_error;
240 
241  std::function<void(Scamp5cHost*)> standard_output_callback[8];
242  std::function<void(Scamp5cHost*)> generic_packet_callback;
243  std::function<void(Scamp5cHost*)> error_callback;
244 
245  struct std_loopc_meta{
246  uint32_t loop_counter;
247  };
248  struct std_events_meta{
249  uint32_t loop_counter;
250  uint16_t event_dimension;
251  uint16_t event_count;
252  };
253  struct std_aout_meta{
254  uint32_t loop_counter;
255  uint16_t width;
256  uint16_t height;
257  };
258  struct std_dout_meta{
259  uint32_t loop_counter;
260  uint16_t width;
261  uint16_t height;
262  };
263  struct std_target_meta{
264  uint32_t loop_counter;
265  };
266 
267  inline uint8_t&data(size_t r,size_t c){
268  return *(data_buffer + r*data_dim_c + c);
269  }
270 
271  inline void format_data_buffer(size_t r,size_t c){
272  if(data_buffer){
273  free(data_buffer);
274  }
275  data_dim_r = r;
276  data_dim_c = c;
277  data_buffer = (uint8_t*)malloc(sizeof(uint8_t)*data_dim_r*data_dim_c);
278  }
279 
280  void update_loop_counter(uint32_t new_lc);
281  void update_packet_rate(uint32_t n);
282 
283  void process_std_loopc(scamp5c_spi::packet *pkt);
284  void process_std_aout(scamp5c_spi::packet *pkt);
285  void process_std_dout(scamp5c_spi::packet *pkt);
286  void process_std_target(scamp5c_spi::packet *pkt);
287  void process_std_events(scamp5c_spi::packet *pkt);
288  void process_std_appinfo(scamp5c_spi::packet *pkt);
289 
290  static int save_bmp24(const char*filename,uint32_t width,uint32_t height,const uint8_t*data);
291 
292 };
293 
294 #endif
uint32_t GetDataDim(int d)
this function can only be used within a packet callback function to get the dimension of the data ...
Definition: Scamp5cHost.hpp:145
void Open()
open the spi interface and initialize the host
void SetInputPort(int i, uint8_t u)
set the value of SPI_RX_* port on the IPU
Definition: Scamp5cHost.hpp:191
this interface uses the data-ready pin to trigger transfers.
Definition: scamp5c_spi_versions.hpp:29
the SPI packet class
Definition: scamp5c_spi.hpp:73
void Process()
process any present packet
void SetupSpi(scamp5c_spi_ht *spi_class)
assign the SPI interface class to operate upon
the SPI interface hosting class
Definition: Scamp5cHost.hpp:47
uint8_t * GetData()
this function can only be used within a packet callback function to get the data
Definition: Scamp5cHost.hpp:170
void Close()
close the spi interface and terminate the host
scamp5c_spi::packet * GetPacket()
get the orgininal packet class received (can be used only in packet callback)
Definition: Scamp5cHost.hpp:183
void RegisterGenericPacketCallback(std::function< void(Scamp5cHost *)> func)
register a function to be called when a generic packet is received
void RegisterStandardOutputCallback(int type, std::function< void(Scamp5cHost *)> func)
register a function to be called when a type of standard packet is received