Skip to content

Commit 18118fc

Browse files
committed
added integration for Energia (only tested with MSP432 Launchpad)
1 parent b4a7f3c commit 18118fc

File tree

2 files changed

+52
-34
lines changed

2 files changed

+52
-34
lines changed

PS2X_lib/PS2X_lib.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22
#include <math.h>
33
#include <stdio.h>
44
#include <stdint.h>
5-
#ifdef __AVR__
6-
#include <avr/io.h>
7-
#endif
8-
#if ARDUINO > 22
9-
#include "Arduino.h"
10-
#else
11-
#include "WProgram.h"
12-
#include "pins_arduino.h"
13-
#endif
145

156
static byte enter_config[]={0x01,0x43,0x00,0x01,0x00};
167
static byte set_mode[]={0x01,0x44,0x00,0x01,0x03,0x00,0x00,0x00,0x00};
@@ -57,6 +48,9 @@ byte PS2X::Analog(byte button) {
5748
/****************************************************************************************/
5849
unsigned char PS2X::_gamepad_shiftinout (char byte) {
5950
unsigned char tmp = 0;
51+
#ifdef ENERGIA
52+
noInterrupts();
53+
#endif
6054
for(unsigned char i=0;i<8;i++) {
6155
if(CHK(byte,i)) CMD_SET();
6256
else CMD_CLR();
@@ -73,6 +67,9 @@ unsigned char PS2X::_gamepad_shiftinout (char byte) {
7367
#endif
7468
}
7569
CMD_SET();
70+
#ifdef ENERGIA
71+
interrupts();
72+
#endif
7673
delayMicroseconds(CTRL_BYTE_DELAY);
7774
return tmp;
7875
}
@@ -152,7 +149,7 @@ boolean PS2X::read_gamepad(boolean motor1, byte motor2) {
152149

153150
last_buttons = buttons; //store the previous buttons states
154151

155-
#if defined(__AVR__)
152+
#if defined(__AVR__) || defined(ENERGIA)
156153
buttons = *(uint16_t*)(PS2data+3); //store as one value for multiple functions
157154
#else
158155
buttons = (uint16_t)(PS2data[4] << 8) + PS2data[3]; //store as one value for multiple functions
@@ -171,7 +168,12 @@ byte PS2X::config_gamepad(uint8_t clk, uint8_t cmd, uint8_t att, uint8_t dat, bo
171168

172169
byte temp[sizeof(type_read)];
173170

174-
#ifdef __AVR__
171+
#if defined(ENERGIA)
172+
_clk_pin = clk;
173+
_cmd_pin = cmd;
174+
_att_pin = att;
175+
_dat_pin = dat;
176+
#elif defined(__AVR__)
175177
_clk_mask = digitalPinToBitMask(clk);
176178
_clk_oreg = portOutputRegister(digitalPinToPort(clk));
177179
_cmd_mask = digitalPinToBitMask(cmd);
@@ -180,8 +182,7 @@ byte PS2X::config_gamepad(uint8_t clk, uint8_t cmd, uint8_t att, uint8_t dat, bo
180182
_att_oreg = portOutputRegister(digitalPinToPort(att));
181183
_dat_mask = digitalPinToBitMask(dat);
182184
_dat_ireg = portInputRegister(digitalPinToPort(dat));
183-
#else
184-
#ifdef ESP8266
185+
#elif defined(ESP8266)
185186
_clk_pin = clk;
186187
_cmd_pin = cmd;
187188
_att_pin = att;
@@ -205,13 +206,12 @@ byte PS2X::config_gamepad(uint8_t clk, uint8_t cmd, uint8_t att, uint8_t dat, bo
205206

206207
_dat_mask = digitalPinToBitMask(dat);
207208
_dat_lport = portInputRegister(digitalPinToPort(dat));
208-
#endif
209209
#endif
210210

211211
pinMode(clk, OUTPUT); //configure ports
212212
pinMode(att, OUTPUT);
213213
pinMode(cmd, OUTPUT);
214-
#ifdef ESP8266
214+
#if defined(ESP8266) || defined(ENERGIA)
215215
pinMode(dat, INPUT_PULLUP); // enable pull-up
216216
#else
217217
pinMode(dat, INPUT);
@@ -386,7 +386,7 @@ bool PS2X::enablePressures() {
386386
return false;
387387

388388
en_Pressures = true;
389-
return true;
389+
return true;
390390
}
391391

392392
/****************************************************************************************/
@@ -401,7 +401,7 @@ void PS2X::reconfig_gamepad(){
401401
}
402402

403403
/****************************************************************************************/
404-
#ifdef __AVR__
404+
#if defined(__AVR__)
405405
inline void PS2X::CLK_SET(void) {
406406
register uint8_t old_sreg = SREG;
407407
cli();
@@ -448,9 +448,8 @@ inline bool PS2X::DAT_CHK(void) {
448448
return (*_dat_ireg & _dat_mask) ? true : false;
449449
}
450450

451-
#else
452-
#ifdef ESP8266
453-
// Let's just use digitalWrite() on ESP8266.
451+
#elif defined(ESP8266) || defined(ENERGIA)
452+
// Let's just use digitalWrite() on ESP8266 and Launchpad
454453
inline void PS2X::CLK_SET(void) {
455454
digitalWrite(_clk_pin, HIGH);
456455
}
@@ -478,6 +477,7 @@ inline void PS2X::ATT_CLR(void) {
478477
inline bool PS2X::DAT_CHK(void) {
479478
return digitalRead(_dat_pin) ? true : false;
480479
}
480+
481481
#else
482482
// On pic32, use the set/clr registers to make them atomic...
483483
inline void PS2X::CLK_SET(void) {
@@ -509,4 +509,3 @@ inline bool PS2X::DAT_CHK(void) {
509509
}
510510

511511
#endif
512-
#endif

PS2X_lib/PS2X_lib.h

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/******************************************************************
2-
* Super amazing PS2 controller Arduino Library v1.8
2+
* Super amazing PS2 controller Arduino Library v1.91
33
* details and example sketch:
44
* http://www.billporter.info/?p=240
55
*
@@ -12,6 +12,7 @@
1212
* Contributers:
1313
* Eric Wetzel (thewetzel@gmail.com)
1414
* Kurt Eckhardt
15+
* chris miller
1516
*
1617
* Lib version history
1718
* 0.1 made into library, added analog stick support.
@@ -60,6 +61,8 @@
6061
* 1.9
6162
* Kurt - Added detection and recovery from dropping from analog mode, plus
6263
* integrated Chipkit (pic32mx...) support
64+
* 1.91
65+
* chris - Added integration for Energia (only tested with MSP432 Launchpad)
6366
*
6467
*
6568
*
@@ -80,22 +83,34 @@ GNU General Public License for more details.
8083
#ifndef PS2X_lib_h
8184
#define PS2X_lib_h
8285

83-
#if ARDUINO > 22
84-
#include "Arduino.h"
85-
#else
86-
#include "WProgram.h"
86+
#if defined(ENERGIA) // LaunchPad MSP430/432, Stellaris and Tiva, Experimeter Board FR5739 specific
87+
#include "Energia.h"
88+
#elif defined(ARDUINO) && (ARDUINO >= 100) // Arduino 1.0 and 1.5 specific
89+
#include "Arduino.h"
90+
#elif defined(ARDUINO) // Arduino 23 specific
91+
#include "WProgram.h"
92+
//#elif defined(MPIDE) // chipKIT specific
93+
// #include "WProgram.h"
94+
//#elif defined(CORE_TEENSY) // Teensy specific
95+
// #include "WProgram.h"
96+
#else // error
97+
#error Platform not defined
8798
#endif
8899

89100
#include <math.h>
90101
#include <stdio.h>
91102
#include <stdint.h>
92-
#ifdef __AVR__
103+
#if defined(ENERGIA)
104+
// LaunchPad MSP430/432, Stellaris and Tiva, Experimeter Board FR5739 specific
105+
#define CTRL_CLK 1
106+
#define CTRL_CLK_HIGH 1
107+
#define CTRL_BYTE_DELAY 20
108+
#elif defined(__AVR__)
93109
// AVR
94110
#include <avr/io.h>
95111
#define CTRL_CLK 4
96112
#define CTRL_BYTE_DELAY 3
97-
#else
98-
#ifdef ESP8266
113+
#elif defined(ESP8266)
99114
#define CTRL_CLK 5
100115
#define CTRL_CLK_HIGH 5
101116
#define CTRL_BYTE_DELAY 18
@@ -105,7 +120,6 @@ GNU General Public License for more details.
105120
#define CTRL_CLK 5
106121
#define CTRL_CLK_HIGH 5
107122
#define CTRL_BYTE_DELAY 4
108-
#endif
109123
#endif
110124

111125
//These are our button constants
@@ -206,7 +220,14 @@ class PS2X {
206220
unsigned int last_buttons;
207221
unsigned int buttons;
208222

209-
#ifdef __AVR__
223+
#if defined(ENERGIA)
224+
// LaunchPad MSP430/432, Stellaris and Tiva, Experimeter Board FR5739 specific
225+
uint8_t _clk_pin;
226+
uint8_t _cmd_pin;
227+
uint8_t _att_pin;
228+
uint8_t _dat_pin;
229+
#elif defined(__AVR__)
230+
// AVR
210231
uint8_t maskToBitNum(uint8_t);
211232
uint8_t _clk_mask;
212233
volatile uint8_t *_clk_oreg;
@@ -216,8 +237,7 @@ class PS2X {
216237
volatile uint8_t *_att_oreg;
217238
uint8_t _dat_mask;
218239
volatile uint8_t *_dat_ireg;
219-
#else
220-
#ifdef ESP8266
240+
#elif defined(ESP8266)
221241
int _clk_pin;
222242
int _cmd_pin;
223243
int _att_pin;
@@ -236,7 +256,6 @@ class PS2X {
236256
uint16_t _dat_mask;
237257
volatile uint32_t *_dat_lport;
238258
#endif
239-
#endif
240259

241260
unsigned long last_read;
242261
byte read_delay;

0 commit comments

Comments
 (0)