1////////////////////////////////////////////////////////////////////////////////
2// //
3// This software is supplied under the terms of a license agreement or //
4// nondisclosure agreement with Mitov Software and may not be copied //
5// or disclosed except in accordance with the terms of that agreement. //
6// Copyright(c) 2002-2016 Mitov Software. All Rights Reserved. //
7// //
8////////////////////////////////////////////////////////////////////////////////
9
10#ifndef _MITOV_BASIC_SPI_h
11#define _MITOV_BASIC_SPI_h
12
13#include <Mitov.h>
14#include <SPI.h>
15
16namespace Mitov
17{
18 class BasicSPI : public OpenWire::Component
19 {
20 public:
21 virtual uint16_t transfer16(uint16_t data) = 0;
22 virtual uint8_t transfer(uint8_t data) = 0;
23 virtual void transfer(void *buf, size_t count) = 0;
24 virtual void beginTransaction(SPISettings settings) = 0;
25 virtual void endTransaction() = 0;
26 };
27//---------------------------------------------------------------------------
28 class Basic_SPIChannel;
29//---------------------------------------------------------------------------
30 class Basic_SPI : public Mitov::EnabledComponent
31 {
32 typedef Mitov::EnabledComponent inherited;
33
34 public:
35 OpenWire::SourcePin ChipSelectOutputPin;
36
37 protected:
38 BasicSPI &FSPI;
39
40 public:
41 Basic_SPI( BasicSPI &ASPI ) :
42 FSPI( ASPI )
43 {
44 }
45
46 };
47//---------------------------------------------------------------------------
48 class Basic_MultiChannel_SourceSPI : public Mitov::Basic_SPI, public ClockingSupport
49 {
50 typedef Mitov::Basic_SPI inherited;
51
52 public:
53 bool FModified = false;
54
55 public:
56 Mitov::SimpleList<Basic_SPIChannel *> FChannels;
57
58 protected:
59 virtual void SystemInit();
60
61 protected:
62 virtual void DoClockReceive( void * ) override;
63
64 public:
65 using inherited::inherited;
66
67 };
68//---------------------------------------------------------------------------
69 class Basic_SPIChannel : public Mitov::CommonSink
70 {
71 typedef Mitov::CommonSink inherited;
72
73 public:
74 virtual void InitChannel() {}
75 virtual void SendOutput() = 0;
76
77 };
78//---------------------------------------------------------------------------
79 template<typename T_OWNER> class Basic_Typed_SPIChannel : public Mitov::Basic_SPIChannel
80 {
81 typedef Mitov::Basic_SPIChannel inherited;
82
83 public:
84 float InitialValue = 0.0f;
85
86 protected:
87 float FValue = 0.0f;
88 float FNewValue = 0.0f;
89
90 int FIndex;
91
92 protected:
93 T_OWNER &FOwner;
94
95 protected:
96 virtual void DoReceive( void *_Data )
97 {
98 FNewValue = constrain( *((float *)_Data), 0.0f, 1.0f );
99 if( FNewValue == FValue )
100 return;
101
102 if( FOwner.ClockInputPin.IsConnected() )
103 FOwner.FModified = true;
104
105 else
106 SendOutput();
107
108 }
109
110 public:
111 Basic_Typed_SPIChannel( T_OWNER &AOwner, int AIndex ) :
112 FOwner( AOwner ),
113 FIndex( AIndex )
114 {
115 AOwner.FChannels.push_back( this );
116 }
117
118 };
119//---------------------------------------------------------------------------
120 void Basic_MultiChannel_SourceSPI::DoClockReceive( void * )
121 {
122 if( ! FModified )
123 return;
124
125 for( int i =0; i < FChannels.size(); ++i )
126 FChannels[ i ]->SendOutput();
127 }
128//---------------------------------------------------------------------------
129 void Basic_MultiChannel_SourceSPI::SystemInit()
130 {
131 inherited::SystemInit();
132 ChipSelectOutputPin.SendValue( true );
133
134 for( int i =0; i < FChannels.size(); ++i )
135 FChannels[ i ]->InitChannel();
136 }
137//---------------------------------------------------------------------------
138}
139#endif