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_PS2_CONTROLLER_SERIAL_h
11#define _MITOV_PS2_CONTROLLER_SERIAL_h
12
13#include <Mitov.h>
14#include <Mitov_PS2_Controller_Basic.h>
15
16namespace Mitov
17{
18 class PS2BasicControllerSerial : public Mitov::PS2BasicController
19 {
20 typedef Mitov::PS2BasicController inherited;
21
22 protected:
23 Mitov::BasicSerialPort &FSerial;
24
25 protected:
26 void WriteByte( byte AValue )
27 {
28 while( FSerial.GetStream().available() > 0 )
29 Serial.read();
30
31 FSerial.GetStream().write( AValue );
32 FSerial.GetStream().flush(); //wait for all data transmitted
33 }
34
35 byte ReadByte()
36 {
37 long waitcount=0;
38
39 while(true)
40 {
41 if( FSerial.GetStream().available() > 0 )
42 {
43 byte rec_data = FSerial.GetStream().read();
44// SERIAL_ERR=false;
45 return(rec_data);
46 }
47 waitcount++;
48 if(waitcount>50000)
49 {
50// SERIAL_ERR=true;
51 return (0xFF);
52 }
53
54 }
55 }
56
57 public:
58 virtual bool ReadDigital( unsigned int AIndex ) override
59 {
60 WriteByte( AIndex );
61 return ( ReadByte() == 0 );
62 }
63
64 virtual float ReadAnalog( unsigned int AIndex ) override
65 {
66 WriteByte( AIndex );
67 return ((float)ReadByte()) / 255;
68// return ((float)Fps2x.Analog( AIndex )) / 255;
69 }
70
71 public:
72 PS2BasicControllerSerial( Mitov::BasicSerialPort &ASerial ) :
73 FSerial( ASerial )
74 {
75 }
76
77 };
78//---------------------------------------------------------------------------
79 class PS2ControllerSerial : public PS2BasicControllerSerial
80 {
81 typedef PS2BasicControllerSerial inherited;
82
83 public:
84 OpenWire::SinkPin SmallVibrateMotorInputPin;
85 OpenWire::SinkPin LargeVibrateMotorInputPin;
86
87 protected:
88 bool FSmallMotor = false;
89 int8_t FLargeMotor = 0;
90
91 protected:
92 void DoSmallVibrateMotorReceive( void *_Data )
93 {
94 FSmallMotor = *(bool *)_Data;
95 }
96
97 void DoLargeVibrateMotorReceive( void *_Data )
98 {
99 FLargeMotor = (int8_t)( constrain( *(float *)_Data, 0, 1 ) * 255 + 0.5 );
100 }
101
102 virtual void ReadController()
103 {
104 WriteByte( 29 ); // Small Motor
105 if( FSmallMotor )
106 WriteByte( 1 );
107
108 else
109 WriteByte( 0 );
110
111 WriteByte( 30 ); // Large Motor
112 WriteByte( FLargeMotor );
113 }
114
115 public:
116 PS2ControllerSerial( Mitov::BasicSerialPort &ASerial ) :
117 inherited( ASerial )
118 {
119 SmallVibrateMotorInputPin.SetCallback( MAKE_CALLBACK( PS2ControllerSerial::DoSmallVibrateMotorReceive ));
120 LargeVibrateMotorInputPin.SetCallback( MAKE_CALLBACK( PS2ControllerSerial::DoLargeVibrateMotorReceive ));
121 }
122
123 };
124//---------------------------------------------------------------------------
125/*
126 class PS2GuitarSerial : public PS2BasicControllerSerial
127 {
128 typedef PS2BasicControllerSerial inherited;
129
130 virtual void ReadController()
131 {
132 Fps2x.read_gamepad( false, 0 );
133 }
134
135 public:
136 PS2GuitarSerial( int ADataPinNumber, int ACommandPinNumber, int AAttentionPinNumber, int AClockPinNumber ) :
137 inherited( ADataPinNumber, ACommandPinNumber, AAttentionPinNumber, AClockPinNumber )
138 {
139 }
140 };
141*/
142//---------------------------------------------------------------------------
143}
144
145#endif