Skip to content

Instantly share code, notes, and snippets.

@samirsogay
Created September 22, 2020 18:44
Show Gist options
  • Save samirsogay/092ea4b4813014a055e962394f18b68f to your computer and use it in GitHub Desktop.
Save samirsogay/092ea4b4813014a055e962394f18b68f to your computer and use it in GitHub Desktop.
This is arduino code for Adafruit Bluefruit for playing Euro Truck Simulator 2
/*********************************************************************
This is an example for our nRF51822 based Bluefruit LE modules
Pick one up today in the adafruit shop!
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/
/*
This example shows how to send HID (keyboard/mouse/etc) data via BLE
Note that not all devices support BLE Mouse!
- OSX, Windows 10 both work
- Android has limited support
- iOS completely ignores mouse
*/
#include <Arduino.h>
#include <SPI.h>
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined(ARDUINO_ARCH_SAMD)
#include <SoftwareSerial.h>
#endif
#include <DigitalIO.h>
const uint8_t SOFT_SPI_MISO_PIN =0;
const uint8_t SOFT_SPI_MOSI_PIN = 1;
const uint8_t SOFT_SPI_SCK_PIN = 6;
const uint8_t SPI_MODE = 3;
byte buttonStateA0 = HIGH;
byte lastbuttonStateA0 = HIGH;
byte buttonStateA1 = HIGH;
byte lastbuttonStateA1 = HIGH;
byte buttonStateA2 = HIGH;
byte lastbuttonStateA2 = HIGH;
byte buttonStateA3 = HIGH;
byte lastbuttonStateA3 = HIGH;
byte buttonStateA4 = HIGH;
byte lastbuttonStateA4 = HIGH;
byte buttonStateA5 = HIGH;
byte lastbuttonStateA5 = HIGH;
byte buttonState10 = HIGH;
byte lastbuttonState10 = HIGH;
byte buttonState11 = HIGH;
byte lastbuttonState11 = HIGH;
byte buttonState12 = HIGH;
byte lastbuttonState12 = HIGH;
byte buttonState13 = HIGH;
byte lastbuttonState13 = HIGH;
SoftSPI<SOFT_SPI_MISO_PIN, SOFT_SPI_MOSI_PIN, SOFT_SPI_SCK_PIN, SPI_MODE> softspi;
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"
#include "BluefruitConfig.h"
/*=========================================================================
APPLICATION SETTINGS
    FACTORYRESET_ENABLE    Perform a factory reset when running this sketch
   
    Enabling this will put your Bluefruit LE module
in a 'known good' state and clear any config
data set in previous sketches or projects, so
    running this at least once is a good idea.
   
    When deploying your project, however, you will
want to disable factory reset by setting this
value to 0.  If you are making changes to your
    Bluefruit LE device via AT commands, and those
changes aren't persisting across resets, this
is the reason why.  Factory reset will erase
the non-volatile memory where config data is
stored, setting it back to factory default
values.
       
    Some sketches that require you to bond to a
central device (HID mouse, keyboard, etc.)
won't work at all with this feature enabled
since the factory reset will clear all of the
bonding data stored on the chip, meaning the
central device won't be able to reconnect.
MINIMUM_FIRMWARE_VERSION Minimum firmware version to have some new features
-----------------------------------------------------------------------*/
#define FACTORYRESET_ENABLE 0
#define MINIMUM_FIRMWARE_VERSION "0.6.6"
/*=========================================================================*/
// Create the bluefruit object, either software serial...uncomment these lines
/*
SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN);
Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN,
BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN);
*/
/* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */
// Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN);
/* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
/* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */
//Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO,
// BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS,
// BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
// A small helper
void error(const __FlashStringHelper*err) {
Serial.println(err);
while (1);
}
// setup pins
int nReset = 2;
int nCS = 3;
/**************************************************************************/
/*!
@brief Sets up the HW an the BLE module (this function is called
automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
//while (!Serial); // required for Flora & Micro
delay(500);
//set pin I/O direction
pinMode (nReset, OUTPUT);
pinMode (nCS, OUTPUT);
pinMode (A0, INPUT);
pinMode (A1, INPUT);
pinMode (A2, INPUT);
pinMode (A3, INPUT);
pinMode (A4, INPUT);
pinMode (A5, INPUT);
pinMode (10, INPUT);
pinMode (11, INPUT);
pinMode (12, INPUT);
pinMode (13, INPUT);
pinMode (A0, INPUT_PULLUP);
pinMode (A1, INPUT_PULLUP);
pinMode (A2, INPUT_PULLUP);
pinMode (A3, INPUT_PULLUP);
pinMode (A4, INPUT_PULLUP);
pinMode (A5, INPUT_PULLUP);
pinMode (10, INPUT_PULLUP);
pinMode (11, INPUT_PULLUP);
pinMode (12, INPUT_PULLUP);
pinMode (13, INPUT_PULLUP);
//put the device in reset not chip selected
digitalWrite(nReset,LOW);
digitalWrite(nCS,HIGH);
Serial.begin(115200);
Serial.println(F("Adafruit Bluefruit HID Mouse Example"));
Serial.println(F("---------------------------------------"));
/* Initialise the module */
Serial.print(F("Initialising the Bluefruit LE module: "));
if ( !ble.begin(VERBOSE_MODE) )
{
error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
}
Serial.println( F("OK!") );
if ( FACTORYRESET_ENABLE )
{
/* Perform a factory reset to make sure everything is in a known state */
Serial.println(F("Performing a factory reset: "));
if ( ! ble.factoryReset() ){
error(F("Couldn't factory reset"));
}
}
/* Disable command echo from Bluefruit */
ble.echo(false);
Serial.println("Requesting Bluefruit info:");
/* Print Bluefruit information */
ble.info();
// This demo only available for firmware from 0.6.6
if ( !ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) )
{
error(F("This sketch requires firmware version " MINIMUM_FIRMWARE_VERSION " or higher!"));
}
/* Enable HID Service (including Mouse) */
Serial.println(F("Enable HID Service (including Mouse): "));
if (! ble.sendCommandCheckOK(F( "AT+BleHIDEn=On" ))) {
error(F("Failed to enable HID (firmware >=0.6.6?)"));
}
/* Add or remove service requires a reset */
Serial.println(F("Performing a SW reset (service changes require a reset): "));
if (! ble.reset() ) {
error(F("Could not reset??"));
}
Serial.println();
//initialize SPI
softspi.begin();
//SPI.setDataMode(SPI_MODE3);
}
/**************************************************************************/
/*!
@brief Constantly poll for new command or response data
*/
/**************************************************************************/
void loop(void)
{
//throttle
buttonStateA0 = digitalRead(A0);
if ((buttonStateA0==LOW)&&(buttonStateA0!=lastbuttonStateA0)){
ble.println(F("AT+BleHidMouseButton=L,press"));
lastbuttonStateA0=buttonStateA0;
}
if ((buttonStateA0==HIGH)&&(buttonStateA0!=lastbuttonStateA0)){
ble.println(F("AT+BleHidMouseButton=0"));
lastbuttonStateA0=buttonStateA0;
}
//horn
buttonStateA1 = digitalRead(A1);
if ((buttonStateA1==LOW)&&(buttonStateA1!=lastbuttonStateA1)){
ble.println(F("AT+BleHidMouseButton=R,press"));
lastbuttonStateA1=buttonStateA1;
}
if ((buttonStateA1==HIGH)&&(buttonStateA1!=lastbuttonStateA1)){
ble.println(F("AT+BleHidMouseButton=0"));
lastbuttonStateA1=buttonStateA1;
}
//engine
buttonStateA2 = digitalRead(A2);
if ((buttonStateA2==LOW)&&(buttonStateA2!=lastbuttonStateA2)){
ble.println("AT+BleKeyboard=E");
lastbuttonStateA2=buttonStateA2;
}
if ((buttonStateA2==HIGH)&&(buttonStateA2!=lastbuttonStateA2)){
ble.println("AT+BleKeyboard=E");
lastbuttonStateA2=buttonStateA2;
}
//right signal
buttonStateA3 = digitalRead(A3);
if ((buttonStateA3==LOW)&&(buttonStateA3!=lastbuttonStateA3)){
ble.println("AT+BleKeyboard=[");
lastbuttonStateA3=buttonStateA3;
}
if ((buttonStateA3==HIGH)&&(buttonStateA3!=lastbuttonStateA3)){
ble.println("AT+BleKeyboard=[");
lastbuttonStateA3=buttonStateA3;
}
//left signal
buttonStateA4 = digitalRead(A4);
if ((buttonStateA4==LOW)&&(buttonStateA4!=lastbuttonStateA4)){
ble.println("AT+BleKeyboard=]");
lastbuttonStateA4=buttonStateA4;
}
if ((buttonStateA4==HIGH)&&(buttonStateA4!=lastbuttonStateA4)){
ble.println("AT+BleKeyboard=]");
lastbuttonStateA4=buttonStateA4;
}
//parking brake
buttonStateA5 = digitalRead(A5);
if ((buttonStateA5==LOW)&&(buttonStateA5!=lastbuttonStateA5)){
ble.println("AT+BleKeyboard=S");
lastbuttonStateA5=buttonStateA5;
}
if ((buttonStateA5==HIGH)&&(buttonStateA5!=lastbuttonStateA5)){
ble.println("AT+BleKeyboard=S");
lastbuttonStateA5=buttonStateA5;
}
//Light
buttonState10 = digitalRead(10);
if ((buttonState10==LOW)&&(buttonState10!=lastbuttonState10)){
ble.println("AT+BleKeyboard=L");
lastbuttonState10=buttonState10;
}
if ((buttonState10==HIGH)&&(buttonState10!=lastbuttonState10)){
lastbuttonState10=buttonState10;
}
//Hazard
buttonState11 = digitalRead(11);
if ((buttonState11==LOW)&&(buttonState11!=lastbuttonState11)){
ble.println("AT+BleKeyboard=F");
lastbuttonState11=buttonState11;
}
if ((buttonState11==HIGH)&&(buttonState11!=lastbuttonState11)){
// ble.println("AT+BleKeyboard=F");
lastbuttonState11=buttonState11;
}
//brake
buttonState12 = digitalRead(12);
if ((buttonState12==LOW)&&(buttonState12!=lastbuttonState12)){
ble.println(F("AT+BleHidMouseButton=M,press"));
lastbuttonState12=buttonState12;
}
if ((buttonState12==HIGH)&&(buttonState12!=lastbuttonState12)){
ble.println(F("AT+BleHidMouseButton=0"));
lastbuttonState12=buttonState12;
}
//Wiper
buttonState13 = digitalRead(13);
if ((buttonState13==LOW)&&(buttonState13!=lastbuttonState13)){
ble.println("AT+BleKeyboard=P");
lastbuttonState13=buttonState13;
}
if ((buttonState13==HIGH)&&(buttonState13!=lastbuttonState13)){
lastbuttonState13=buttonState13;
}
// Serial.println(buttonStateA0);
//enable ADNS
digitalWrite(nReset,HIGH);
// delay a bit
delay(100);
unsigned int motion = 0;
//check for movement
//read motion register, 0x02
motion = ADNS_read(0x02);
if(motion > 127){
Serial.println( motion);
//there has been a motion!
ble.print(F("AT+BleHidMouseMove="));
ble.print(int((convTwosComp(ADNS_read(0x03)))/3));
ble.print(",");
ble.println(convTwosComp(ADNS_read(0x04))*0);
}
}
unsigned int ADNS_read(unsigned int address){
unsigned int data;
// take the CS pin low to select the chip:
digitalWrite(nCS,LOW);
// send in the address and value via SPI:
softspi.transfer(address);
pinMode(MOSI, INPUT);
data = softspi.transfer(0x00);
pinMode(MOSI, OUTPUT);
// take the SS pin high to de-select the chip:
digitalWrite(nCS,HIGH);
return(data);
}
int convTwosComp(int b){ //Convert from 2's complement
if(b & 0x80){
b = -1 * ((b ^ 0xff) + 1);
}
return b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment