Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //CONTROLLERS: (Uno)
- //OLED libraries
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #include <SparkFun_APDS9960.h>
- //Wireless libraries
- #include <SPI.h>
- #include "nRF24L01.h"
- #include "RF24.h"
- //Controller inputs
- #define button 4
- #define pressureButton A0
- #define touch 3
- #define APDS9960_INT 2
- //The OLED display
- Adafruit_SSD1306 display(-1);
- SparkFun_APDS9960 apds = SparkFun_APDS9960();
- //The wireless signal radio
- RF24 radio(7, 8); // CE, CSN
- const byte pipe[6] = "00001";
- uint8_t buttonState, lastButton, pressure, lastPressure, lastGesture, isTouch = 0;
- //The controller data that will be sent to the screen
- uint8_t controllerData[6] = {0, 0, 0, 0, 0, 0}; //PING -> Button -> Pressure -> PLAYERNUM -> Touch -> Gesture
- const uint8_t DATA_PLAYER = 0, DATA_PING = 1, DATA_BUTTON = 2, DATA_PRESSURE = 3, DATA_TOUCH = 4, DATA_GESTURE = 5;
- //Change this to 1 or 2 for the dedicated controllers
- const uint8_t CONTROLLER_P = 2;
- //The APDS9960 Gesture sensor
- const uint8_t GESTURE_UP = 0, GESTURE_DOWN = 1, GESTURE_RIGHT = 3, GESTURE_LEFT = 2, GESTURE_NEAR = 4, GESTURE_FAR = 5;
- //Used to track the connection
- uint8_t prevPing = 0, ping = 0;
- //What does the OLED screen show? this int keeps track of the sequence of what should be displayed. You can tinker around with various things, while this number will say what will be shown.
- uint8_t screen_state = 0;
- void setup(void)
- {
- Serial.begin(9600);
- //The controller inputs
- pinMode(button, INPUT);
- pinMode(pressureButton, INPUT);
- pinMode(touch, INPUT);
- //The OLED screen initialisation
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
- display.clearDisplay();
- //Wireless radio initialisation
- radio.begin();
- radio.openWritingPipe(pipe);
- //Assign player number
- controllerData[DATA_PLAYER] = CONTROLLER_P;
- }
- unsigned long old_sec = 0;
- uint8_t gestureDel = 0;
- void loop(void)
- {
- unsigned long current_time = millis();
- //Update per second
- if (current_time - old_sec >= 1000)
- {
- old_sec = current_time;
- if (gestureDel > 0) gestureDel++;
- }
- //Pressure pad
- pressure = analogRead(pressureButton);
- int force = 0;
- if (analogRead(pressureButton) > 290)
- {
- int calc = map(pressure, 85, 140, 0, 255);
- if (calc >= 0) force = calc;
- if (lastPressure != force)
- {
- lastPressure = force;
- controllerData[DATA_PRESSURE] = force;
- }
- }
- //Button
- lastButton = buttonState;
- buttonState = digitalRead(button);
- controllerData[DATA_BUTTON] = buttonState;
- //PING
- controllerData[DATA_PING] = random();
- //Touch
- isTouch = clamp(digitalRead(touch), 0, 1);
- controllerData[DATA_TOUCH] = isTouch;
- //Gestures
- handleGesture();
- //Draw the input on the OLED screen of the controller
- if (screen_state > 0) drawControls();
- //Write
- radio.write(&controllerData, sizeof(controllerData));
- }
- void drawControls()
- {
- drawButton(20, 20);
- drawPressure(20, 10);
- drawTouch(80, 11);
- drawGesture(105, 20);
- display.display();
- }
- void drawGesture(int x, int y)
- {
- int iconSize = 2;
- display.setFont(&Org_01);
- display.fillRect(x - 9, y - 9, 24, 14, BLACK);
- display.stopscroll();
- if (gestureDel > 0)
- {
- switch (lastGesture)
- {
- case DIR_UP:
- controllerData[DATA_GESTURE] = GESTURE_UP;
- writeArrow("U", x + 1, y - 1);
- break;
- case DIR_DOWN:
- controllerData[DATA_GESTURE] = GESTURE_DOWN;
- writeArrow("D", x + 1, y - 3);
- break;
- case DIR_LEFT:
- controllerData[DATA_GESTURE] = GESTURE_LEFT;
- writeArrow("L", x - 1, y);
- break;
- case DIR_RIGHT:
- controllerData[DATA_GESTURE] = GESTURE_RIGHT;
- writeArrow("R", x - 3, y);
- break;
- case DIR_NEAR:
- controllerData[DATA_GESTURE] = GESTURE_NEAR;
- writeScreen("O", x, y, iconSize, 0);
- break;
- case DIR_FAR:
- controllerData[DATA_GESTURE] = GESTURE_FAR;
- writeScreen("o", x + 2, y - 2, iconSize, 0);
- break;
- default:
- controllerData[DATA_GESTURE] = -1;
- writeScreen("*", x + 2, y - 1, iconSize, 0);
- }
- }
- if (gestureDel >= 3)
- {
- gestureDel = 0;
- display.fillRect(x - 9, y - 9, 24, 14, BLACK);
- }
- }
- void drawTouch(int x, int y)
- {
- int w = 12, h = 14;
- display.fillRect(x, y, w, h, BLACK);
- display.drawRect(x, y, w, h, WHITE);
- display.fillRect(x, y + (isTouch * (h / 2)), w, h / 2, WHITE);
- }
- void drawButton(int x, int y)
- {
- display.fillRect(x - 2, y - 1, 10, 6, BLACK);
- if (buttonState > 0) display.fillRect(x - 2, y - 1, 10, 6, WHITE);
- else display.drawRect(x, y, 7, 4, WHITE);
- }
- void drawPressure(int x, int y)
- {
- display.drawRect(x, y, 52, 8, WHITE);
- display.fillRect(x + 2, y + 2, 48, 4, BLACK);
- int fill = controllerData[DATA_PRESSURE] / 4;
- display.fillRect(x + 2, y + 2, clamp(fill, 0, 48), 4, WHITE);
- }
- int clamp(int val, int mini, int maxi)
- {
- if (val < mini) return mini;
- else if (val > maxi) return maxi;
- return val;
- }
- void writeScreen(String text, int x, int y, int textSize, int scrollDel)
- {
- display.setTextSize(textSize);
- display.setTextColor(WHITE);
- display.setCursor(x, y);
- display.print(text);
- display.display();
- delay(scrollDel);
- if (scrollDel > 0) display.startscrollleft(0x00, 0x0F);
- }
- void writeArrow(String text, int x, int y)
- {
- display.setTextColor(WHITE);
- if (text == "U")
- {
- display.fillRect(x + 2, y - 7, 3, 3, WHITE);
- display.fillRect(x, y - 6, 7, 2, WHITE);
- display.fillRect(x - 2 - 1, y - 6 + 1, 3, 2, WHITE);
- display.fillRect(x + 5 + 2, y - 6 + 1, 3, 2, WHITE);
- display.fillRect(x + 2, y - 5, 3, 7, WHITE);
- }
- else if (text == "D")
- {
- display.fillRect(x + 2, y, 3, 3, WHITE);
- display.fillRect(x, y, 7, 2, WHITE);
- display.fillRect(x - 2 - 1, y - 1, 3, 2, WHITE);
- display.fillRect(x + 5 + 2, y - 1, 3, 2, WHITE);
- display.fillRect(x + 2, y - 5, 3, 7, WHITE);
- }
- else if (text == "R")
- {
- display.fillRect(x + 13, y - 5, 5, 2, WHITE);
- display.fillRect(x + 12, y - 7, 4, 6, WHITE);
- display.fillRect(x + 11, y - 8, 3, 2, WHITE);
- display.fillRect(x + 11, y - 2, 3, 2, WHITE);
- display.fillRect(x + 1, y - 5, 14, 2, WHITE);
- }
- else if (text == "L")
- {
- display.fillRect(x - 2, y - 5, 5, 2, WHITE);
- display.fillRect(x, y - 7, 4, 6, WHITE);
- display.fillRect(x + 2, y - 8, 3, 2, WHITE);
- display.fillRect(x + 2, y - 2, 3, 2, WHITE);
- display.fillRect(x + 1, y - 5, 14, 2, WHITE);
- }
- display.display();
- }
- void handleGesture()
- {
- if (apds.isGestureAvailable()) lastGesture = apds.readGesture();
- gestureDel = 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement