summaryrefslogtreecommitdiff
path: root/src/lib/ecore_psl1ght/moveutil.c
blob: 1dadfbc4b9b58cfe6b0f222c18d5113cc72a08e5 (plain) (blame)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <stdio.h>
#include <io/move.h>
#include <sys/memory.h>
#include <ppu-types.h>
#include <io/camera.h>
#include <ppu-types.h>
#include <spurs/spurs.h>
#include <sys/thread.h>
#include <sys/systime.h>

#include "spursutil.h"
#include "moveutil.h"

u16 oldGemPad = 0;
u16 newGemPad = 0;
u16 newGemAnalogT = 0;

static void
endCamera(moveContext *context)
{
   cameraStop (0);

   cameraClose (0);
   cameraEnd ();

   sysMemContainerDestroy (context->camInfo.container);
}

static int
initCamera(moveContext *context)
{
   int ret;

   context->camInfo.container = NULL;

   ret = cameraInit ();
   printf ("cameraInit() returned %d\n", ret);
   if (ret == 0)
     {
        cameraType type = CAM_TYPE_UNKNOWN;

        ret = cameraGetType (0, &type);
        if (ret == 0 && type == CAM_TYPE_PLAYSTATION_EYE)
          {
             context->camInfo.format = CAM_FORM_RAW8;
             context->camInfo.framerate = 60;
             context->camInfo.resolution = CAM_RESO_VGA;
             context->camInfo.info_ver = 0x0101;
             ret = sysMemContainerCreate (&context->camInfo.container, 0x200000);
             printf ("sysMemContainerCreate() for camera container returned %d\n", ret);

             ret = cameraOpenEx (0, &context->camInfo);
             switch (ret) {
                case 0:
                  printf ("Found me an eye, arrr!\n");
                  printf ("cameraOpenEx returned %08X\n", ret);
                  printf ("Video dimensions: %dx%d\n", context->camInfo.width, context->camInfo.height);
                  printf ("Buffer at %08X\n", context->camInfo.buffer);
                  printf ("pbuf0 Buffer at %08X\n", context->camInfo.pbuf[0]);
                  printf ("pbuf0 Buffer at %08X\n", context->camInfo.pbuf[1]);
                  printf ("context->camInfo.info_ver %X\n", context->camInfo.info_ver);

                  context->camRead.buffer = context->camInfo.buffer;
                  context->camRead.version = 0x0100;
                  printf ("Setting CameraReadEx %08X buffer to cameraInfoex buffer \n",
                          context->camRead.buffer);
                  break;

                default:
                  printf ("Error %X detected opening PlayStation Eye\n", ret);
                  goto error;
               }
          }
        else {
             printf ("Device detected is not a PlayStation Eye and this sample need it\n");
             goto error;
          }
     }
   else {
        goto error;
     }
   return ret;

error:
   if (context->camInfo.container)
     sysMemContainerDestroy (context->camInfo.container);
   return ret;
}

static int
readCamera(moveContext *context)
{
   int ret;

   ret = cameraReadEx (0, &context->camRead);
   switch (ret) {
      case CAMERA_ERRO_NEED_START:
        cameraReset (0);
        ret = gemPrepareCamera (128, 0.5);
        printf ("GemPrepareCamera return %d exposure set to 128 and quality to 0.5 before cameraStart\n",
                ret);
        printf ("lets go!! It's time to look your face in Sony Bravia :P\n");
        ret = cameraStart (0);
        printf ("cameraStart return %d \n", ret);
        printf ("*******************************************\n");
        printf ("* Now make sure you have a Move connected\n");
        printf ("* and point it towards the camera and press\n");
        printf ("* the action button to calibrate\n");
        printf ("*******************************************\n");
        break;

      case 0:
        break;

      default:
        printf ("error %08X ", ret);
        ret = 1;
        break;
     }
   // printf("despues de start return %d \n",ret);
   if (ret == 0 && context->camRead.readcount != 0)
     {
        return context->camRead.readcount;
     }
   else {
        return 0;
     }
}

moveContext *
initMove()
{
   moveContext *context = NULL;
   Spurs *spurs;
   gemAttribute gem_attr;
   int ret;
   int i;

   spurs = initSpurs ("gem_spurs");

   if (spurs == NULL)
     goto error;

   printf ("preparing GemAttribute structure with spurs\n");

   gem_attr.version = 2;
   gem_attr.max = 1;
   gem_attr.spurs = spurs;
   gem_attr.memory = NULL;
   gem_attr.spu_priorities[0] = 1;
   for (i = 1; i < 8; ++i)
     gem_attr.spu_priorities[i] = 0;

   printf ("calling GemInit with GemAttribute structure version=%d max_connect=%d spurs=%X\n",
           gem_attr.version, gem_attr.max, gem_attr.spurs);
   ret = gemInit (&gem_attr);
   printf ("return from GemInit %X \n", ret);
   if (ret)
     goto error;

   ret = gemPrepareCamera (128, 0.5);
   printf ("GemPrepareCamera return %d exposure set to 128 and quality to 0.5\n",
           ret);
   if (ret)
     goto error;
   ret = gemReset (0);
   printf ("GemReset return %X \n", ret);
   if (ret)
     goto error;

   context = (moveContext *)malloc (sizeof (moveContext));
   context->spurs = spurs;
   ret = initCamera (context);

   if (ret == 0)
     return context;

error:
   if (spurs)
     endSpurs (spurs);
   if (context)
     free (context);
   return NULL;
}

void
endMove(moveContext *context)
{
   /* Stop Move */
    gemEnd ();
    /* Stop Camera */
    endCamera (context);
    /* Stop Spurs */
    endSpurs (context->spurs);

    free (context);
}

int
processMove(moveContext *context)
{
   const unsigned int hues[] = { 4 << 24, 4 << 24, 4 << 24, 4 << 24 };
   int ret = -1;

   if (readCamera (context) > 0)
     {
        ret = gemUpdateStart (context->camRead.buffer, context->camRead.timestamp);
        //printf ("Return from gemUpdateStart %X\n", ret);
        if (ret == 0)
          {
             ret = gemUpdateFinish ();
             //printf ("Return from gemUpdateFinish %X\n", ret);
             if (ret == 0)
               {
                  ret = gemGetState (0, STATE_LATEST_IMAGE_TIME, 0, &context->state);
                  switch (ret) {
                     case 2:
                       gemForceRGB (0, 0.5, 0.5, 0.5);
                       break;

                     case 5:
                       gemTrackHues (hues, NULL);
                       break;

                     default:
                       break;
                    }
               }
          }
     }

   return ret;
}

void
moveGet3DPosition(moveContext *context, float *x, float *y, float *z)
{
   if (x)
     *x = vec_array (context->state.pos, 0);
   if (y)
     *y = vec_array (context->state.pos, 1);
   if (z)
     *z = vec_array (context->state.pos, 2);
}