1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4 
5   This software is provided 'as-is', without any express or implied
6   warranty.  In no event will the authors be held liable for any damages
7   arising from the use of this software.
8 
9   Permission is granted to anyone to use this software for any purpose,
10   including commercial applications, and to alter it and redistribute it
11   freely, subject to the following restrictions:
12 
13   1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17   2. Altered source versions must be plainly marked as such, and must not be
18      misrepresented as being the original software.
19   3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22 
23 #if defined(SDL_JOYSTICK_ALIOS) || defined(SDL_JOYSTICK_DISABLED)
24 
25 /* This is the dummy implementation of the SDL joystick API */
26 
27 #include "SDL_joystick.h"
28 #include "../SDL_sysjoystick.h"
29 #include "../SDL_joystick_c.h"
30 #include <aos/hal/gpio.h>
31 #include <aos/hal/adc.h>
32 #include <SDL_events.h>
33 
game_key_irq_fun(int key_num)34 void game_key_irq_fun(int key_num)
35 {
36     int i = 0;
37     Uint8 button;
38     SDL_Joystick *joystick = SDL_JoystickFromInstanceID(0);
39     // do your things here
40     printf("key %d down\n", key_num);
41 
42     if (!joystick)
43         return;
44 
45     if (key_num == 0) {
46         button = SDL_SCANCODE_LSHIFT;
47     } else if (key_num == 1) {
48         button = SDL_SCANCODE_SPACE;
49     } else if (key_num == 2) {
50         button = SDL_SCANCODE_RETURN;
51     } else if (key_num == 3) {
52         button = SDL_SCANCODE_ESCAPE;
53     }
54 
55     SDL_PrivateJoystickButton(joystick, button, SDL_PRESSED);
56 }
57 
58 static gpio_dev_t key_array[4] = {
59     /*key_num, Port*/
60     {33, IRQ_MODE, NULL}, /*Y*/
61     {32, IRQ_MODE, NULL}, /*R*/
62     {22, IRQ_MODE, NULL}, /*B*/
63     {23, IRQ_MODE, NULL}, /*W*/
64 };
65 
66 static int
AliOS_JoystickInit(void)67 AliOS_JoystickInit(void)
68 {
69     int ret = 0, i = 0;
70     int key_num = 0; // 在这里填键值
71     gpio_dev_t key;
72 
73     printf("AliOS_JoystickInit\n");
74     SDL_PrivateJoystickAdded(0);
75 
76     for (i = 0; i < 4; i++) {
77         key_num = i;
78         key.port = key_array[i].port;
79         key.config = key_array[i].config;
80         key.priv = key_array[i].priv;
81         ret |= hal_gpio_init(&key);
82         ret |= hal_gpio_enable_irq(&key, IRQ_TRIGGER_RISING_EDGE, game_key_irq_fun, key_num);
83     }
84 
85     return 0;
86 }
87 
88 static int
AliOS_JoystickGetCount(void)89 AliOS_JoystickGetCount(void)
90 {
91     return 1;
92 }
93 
94 static void
AliOS_JoystickDetect(void)95 AliOS_JoystickDetect(void)
96 {
97 }
98 
99 static const char *
AliOS_JoystickGetDeviceName(int device_index)100 AliOS_JoystickGetDeviceName(int device_index)
101 {
102     return NULL;
103 }
104 
105 static int
AliOS_JoystickGetDevicePlayerIndex(int device_index)106 AliOS_JoystickGetDevicePlayerIndex(int device_index)
107 {
108     return 0;
109 }
110 
111 static void
AliOS_JoystickSetDevicePlayerIndex(int device_index,int player_index)112 AliOS_JoystickSetDevicePlayerIndex(int device_index, int player_index)
113 {
114 }
115 
116 static SDL_JoystickGUID
AliOS_JoystickGetDeviceGUID(int device_index)117 AliOS_JoystickGetDeviceGUID(int device_index)
118 {
119     SDL_JoystickGUID guid;
120     SDL_zero(guid);
121     return guid;
122 }
123 
124 static SDL_JoystickID
AliOS_JoystickGetDeviceInstanceID(int device_index)125 AliOS_JoystickGetDeviceInstanceID(int device_index)
126 {
127     return 0;
128 }
129 
130 static int
AliOS_JoystickOpen(SDL_Joystick * joystick,int device_index)131 AliOS_JoystickOpen(SDL_Joystick * joystick, int device_index)
132 {
133     joystick->nballs = 1;
134     joystick->nbuttons = 4;
135     printf("AliOS_JoystickOpen ok\n");
136     return 0;
137 }
138 
139 static int
AliOS_JoystickRumble(SDL_Joystick * joystick,Uint16 low_frequency_rumble,Uint16 high_frequency_rumble)140 AliOS_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
141 {
142     return SDL_Unsupported();
143 }
144 
145 static void
AliOS_JoystickUpdate(SDL_Joystick * joystick)146 AliOS_JoystickUpdate(SDL_Joystick * joystick)
147 {
148 	int adc_ret = 0, i;
149     static Sint16 last_xrel, last_yrel;
150     adc_dev_t roll_x;
151     adc_dev_t roll_y;
152 
153     if (!joystick || !joystick->balls)
154         return;
155 
156     roll_x.port = 0;
157     roll_x.config.sampling_cycle = 100;
158 	hal_adc_init(&roll_x);
159     roll_y.port = 2;
160     roll_y.config.sampling_cycle = 100;
161 	hal_adc_init(&roll_y);
162 
163     /* Deliver ball motion updates */
164     for (i = 0; i < joystick->nballs; ++i) {
165         int xrel, yrel;
166         adc_ret |= hal_adc_value_get(&roll_x, &xrel, 5);
167         adc_ret |= hal_adc_value_get(&roll_y, &yrel, 5);
168         //printf("xrel: %d, yrel: %d\n", xrel, yrel);
169         if (xrel || yrel) {
170             if ((last_xrel == xrel) && (last_yrel == yrel))
171                 return;
172             SDL_PrivateJoystickBall(joystick, (Uint8) i, xrel, yrel);
173             last_xrel = xrel;
174             last_yrel = yrel;
175         }
176     }
177 }
178 
179 static void
AliOS_JoystickClose(SDL_Joystick * joystick)180 AliOS_JoystickClose(SDL_Joystick * joystick)
181 {
182 }
183 
184 static void
AliOS_JoystickQuit(void)185 AliOS_JoystickQuit(void)
186 {
187 }
188 
189 static SDL_bool
AliOS_JoystickGetGamepadMapping(int device_index,SDL_GamepadMapping * out)190 AliOS_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
191 {
192     return SDL_FALSE;
193 }
194 
195 SDL_JoystickDriver SDL_AliOS_JoystickDriver =
196 {
197     AliOS_JoystickInit,
198     AliOS_JoystickGetCount,
199     AliOS_JoystickDetect,
200     AliOS_JoystickGetDeviceName,
201     AliOS_JoystickGetDevicePlayerIndex,
202     AliOS_JoystickSetDevicePlayerIndex,
203     AliOS_JoystickGetDeviceGUID,
204     AliOS_JoystickGetDeviceInstanceID,
205     AliOS_JoystickOpen,
206     AliOS_JoystickRumble,
207     AliOS_JoystickUpdate,
208     AliOS_JoystickClose,
209     AliOS_JoystickQuit,
210     AliOS_JoystickGetGamepadMapping
211 };
212 
213 #endif /* SDL_JOYSTICK_AliOS || SDL_JOYSTICK_DISABLED */
214 
215 /* vi: set ts=4 sw=4 expandtab: */
216