File: PSMoveMgr.cpp

package info (click to toggle)
bespokesynth 1.3.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,716 kB
  • sloc: cpp: 117,136; ansic: 18,752; python: 593; xml: 74; makefile: 4
file content (204 lines) | stat: -rw-r--r-- 5,122 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "PSMoveMgr.h"
#include <chrono>
#include <thread>

//--------------------------------------------------------------
void PSMoveMgr::Setup()
{
   for (int i = 0; i < MAX_NUM_PS_MOVES; ++i)
      mMove[i] = NULL;

   AddMoves();
}

void PSMoveMgr::AddMoves()
{
   for (int i = 0; i < MAX_NUM_PS_MOVES; ++i)
   {
      if (mMove[i] == NULL)
         mMove[i] = SetUpMove(i);
   }
}

_PSMove* PSMoveMgr::SetUpMove(int id)
{
   _PSMove* move = NULL;
   int lastBTController = -1;
   for (int i = 0; i <= id; ++i)
   {
      enum PSMove_Connection_Type ctype = Conn_Unknown; //valid: Conn_Unknown, Conn_USB, Conn_Bluetooth
      for (int j = lastBTController + 1; ctype != Conn_Bluetooth; ++j) //find the next bluetooth one
      {
         move = psmove_connect_by_id(j);
         if (move == NULL)
         {
            return NULL;
         }
         ctype = psmove_connection_type(move);
         lastBTController = j;
      }
   }

   for (int i = 0; i < 10; i++)
   {
      psmove_set_leds(move, 0, 255 * (i % 3 == 0), 0);
      //psmove_set_rumble(move, 255*(i%2));
      psmove_update_leds(move);
      std::this_thread::sleep_for(std::chrono::milliseconds{ 10 * (i % 10) });
   }

   for (int i = 250; i >= 0; i -= 5)
   {
      psmove_set_leds(move, i, i, 0);
      psmove_set_rumble(move, 0);
      psmove_update_leds(move);
   }

   psmove_set_leds(move, 0, 0, 0);
   psmove_set_rumble(move, 0);
   psmove_update_leds(move);

   mButtons[id] = 0;

   printf("Connected Move #%d\n", id);

   return move;
}

void PSMoveMgr::GetGyros(int id, ofVec3f& gyros)
{
   if (mMove[id])
   {
      int x, y, z;
      psmove_get_gyroscope(mMove[id], &x, &y, &z);
      gyros.x = x;
      gyros.y = y;
      gyros.z = z;
   }
}

void PSMoveMgr::GetAccel(int id, ofVec3f& accel)
{
   if (mMove[id])
   {
      int x, y, z;
      psmove_get_accelerometer(mMove[id], &x, &y, &z);
      accel.x = x;
      accel.y = y;
      accel.z = z;
   }
}

bool PSMoveMgr::IsButtonDown(int id, PSMove_Button button)
{
   if (mMove[id])
   {
      int currentButtons = psmove_get_buttons(mMove[id]);
      return currentButtons & button;
   }
   return false;
}

float PSMoveMgr::GetBattery(int id)
{
   if (mMove[id])
   {
      return psmove_get_battery(mMove[id]) / 5.0f;
   }
   return 0;
}

//--------------------------------------------------------------
void PSMoveMgr::Update()
{
   for (int i = 0; i < MAX_NUM_PS_MOVES; ++i)
   {
      if (mMove[i])
      {
         _PSMove* move = mMove[i];
         int res = 1;
         while (res)
            res = psmove_poll(move); //poll until there's no new data, so we have the freshest

         int currentButtons = psmove_get_buttons(move);
         int change = currentButtons ^ mButtons[i];
         int pressed = currentButtons & change;
         int released = mButtons[i] & change;
         mButtons[i] = currentButtons;
         if (Btn_TRIANGLE & pressed)
            SendButtonMessage(i, "triangle", 1);
         if (Btn_TRIANGLE & released)
            SendButtonMessage(i, "triangle", 0);
         if (Btn_CIRCLE & pressed)
            SendButtonMessage(i, "circle", 1);
         if (Btn_CIRCLE & released)
            SendButtonMessage(i, "circle", 0);
         if (Btn_CROSS & pressed)
            SendButtonMessage(i, "cross", 1);
         if (Btn_CROSS & released)
            SendButtonMessage(i, "cross", 0);
         if (Btn_SQUARE & pressed)
            SendButtonMessage(i, "square", 1);
         if (Btn_SQUARE & released)
            SendButtonMessage(i, "square", 0);
         if (Btn_SELECT & pressed)
            SendButtonMessage(i, "select", 1);
         if (Btn_SELECT & released)
            SendButtonMessage(i, "select", 0);
         if (Btn_START & pressed)
            SendButtonMessage(i, "start", 1);
         if (Btn_START & released)
            SendButtonMessage(i, "start", 0);
         if (Btn_PS & pressed)
            SendButtonMessage(i, "ps", 1);
         if (Btn_PS & released)
            SendButtonMessage(i, "ps", 0);
         if (Btn_MOVE & pressed)
            SendButtonMessage(i, "move", 1);
         if (Btn_MOVE & released)
            SendButtonMessage(i, "move", 0);
         if (Btn_T & pressed)
            SendButtonMessage(i, "trigger", 1);
         if (Btn_T & released)
            SendButtonMessage(i, "trigger", 0);

         psmove_update_leds(move);
      }
   }
}

void PSMoveMgr::SendButtonMessage(int id, std::string button, int val)
{
   std::string address = "/" + ofToString(id) + "/button/" + button;
   //TheMessenger->SendIntMessage(address,val);

   if (mListener)
      mListener->OnPSMoveButton(id, button, val);
}

void PSMoveMgr::Exit()
{
   for (int i = 0; i < MAX_NUM_PS_MOVES; ++i)
   {
      if (mMove[i])
      {
         psmove_disconnect(mMove[i]);
      }
   }
}

void PSMoveMgr::SetVibration(int id, float amount)
{
   if (mMove[id] == NULL)
      return;

   psmove_set_rumble(mMove[id], int(amount * 255));
}

void PSMoveMgr::SetColor(int id, float r, float g, float b)
{
   if (mMove[id] == NULL)
      return;

   psmove_set_leds(mMove[id], int(r * 255), int(g * 255), int(b * 255));
}