Skip to content

Commit

Permalink
Merge pull request #59 from asterics/RP2040
Browse files Browse the repository at this point in the history
FLipMouse3
  • Loading branch information
Benjamin Aigner authored Jan 12, 2023
2 parents fb691be + 0089bb9 commit ed8b7f9
Show file tree
Hide file tree
Showing 139 changed files with 385,029 additions and 232,990 deletions.
306 changes: 133 additions & 173 deletions FLipWare/FLipWare.ino

Large diffs are not rendered by default.

76 changes: 56 additions & 20 deletions FLipWare/FlipWare.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

/*
FLipWare - AsTeRICS Foundation
For more info please visit: http://www.asterics-academy.net
For more info please visit: https://www.asterics-foundation.org
Module: FlipWare.h - main header file
Expand All @@ -26,17 +26,23 @@

#include <Arduino.h>
#include <Wire.h>
#include <Mouse.h>
#include <Keyboard.h>
#include <Joystick.h>
#include <EEPROM.h>
#include <string.h>
#include <stdint.h>
#include "commands.h"
#include "eeprom.h"
#include "buttons.h"
#include "infrared.h"
#include "bluetooth.h"
#include "hid_hal.h"

#define VERSION_STRING "v2.12.1"
#define VERSION_STRING "v3.2"

// V3.2: changed pinning to PCB v3.2
// V3.00: changed platform to Arduino Nano RP2040 Connect
// V2.12.1: fixed keystring buffer problem
// V2.12: improved modularisation and source code documentation, added LC-display support and elliptical deadzone
// V2.11: eeprom access optimization and support for deletion / update of individual slots
Expand All @@ -61,22 +67,22 @@

// Optional Debug Output Control

// #define DEBUG_OUTPUT_FULL // if full debug output is desired
// #define DEBUG_OUTPUT_BASIC // if basic debug output is desired (for eeprom)
//#define DEBUG_OUTPUT_FULL // if full debug output is desired
//#define DEBUG_OUTPUT_MEMORY // enables eeprom.cpp debugging, showing memory access
//#define DEBUG_OUTPUT_KEYS // enable keys.cpp debugging, showing key press/release events and keycode lookup
//#define DEBUG_OUTPUT_IR // enable infrared.cpp debugging, showing whats happening on IR recv/send
//#define DEBUG_OUTPUT_SENSORS // enable sensors.cpp debugging, showing whats happening on sensor reading & init
//#define DEBUG_DELAY_STARTUP // enable a 3s delay after Serial.begin and before all the other stuff.
//#define DEBUG_NO_TONE // disable tones, to avoid annoying other passengers when programming on the train :-)

#define BUILD_FOR_RP2040 // enable a build for RP2040. There are differences in eeprom & infrared handling.

/**
global constant definitions
*/
#define UPDATE_INTERVAL 5 // update interval for performing HID actions (in milliseconds)
#define DEFAULT_CLICK_TIME 8 // time for mouse click (loop iterations from press to release)

// Analog input pins (4FSRs + 1 pressure sensor)
#define PRESSURE_SENSOR_PIN A0
#define HALL_SENSOR_PIN A1
#define DOWN_SENSOR_PIN A6
#define LEFT_SENSOR_PIN A9
#define UP_SENSOR_PIN A7
#define RIGHT_SENSOR_PIN A8
#define CALIBRATION_PERIOD 200 // approx. 200*UPDATE_INTERVAL = 1sec calibration time

// RAM buffers and memory constraints
#define WORKINGMEM_SIZE 300 // reserved RAM for working memory (command parser, IR-rec/play)
Expand All @@ -100,7 +106,7 @@
*/
struct SlotSettings {

char slotName[MAX_NAME_LEN]; // slotname
char slotName[MAX_NAME_LEN]; // slotname (@warning: must be always the first element, storing relies on that!)
uint16_t keystringBufferLen;

uint8_t stickMode; // alternative(0), mouse(1), joystick (2,3,4)
Expand All @@ -119,29 +125,33 @@ struct SlotSettings {
uint8_t rv; // range vertical drift compensation
uint8_t gh; // gain horizontal drift compensation
uint8_t rh; // range horizontal drift compensation
int16_t cx; // calib x
int16_t cy; // calib y
uint16_t ro; // orientation (0,90,180,270)
uint8_t bt; // bt-mode (0,1,2)
uint8_t sb; // sensorboard-profileID (0,1,2,3)
uint32_t sc; // slotcolor (0x: rrggbb)
char kbdLayout[6];
};

/**
SensorData struct
contains working data of sensors (raw and processed values)
SensorData structs
contain working data of sensors (raw and processed values)
*/
struct SensorData {
int x, y, xRaw,yRaw;
int pressure;
float deadZone, force, forceRaw, angle;
uint8_t dir;
int8_t autoMoveX,autoMoveY;
int up, down, left, right;
uint8_t calib_now;
int16_t cx, cy;
int xDriftComp, yDriftComp;
int xLocalMax, yLocalMax;
};

struct I2CSensorValues {
int xRaw,yRaw;
int pressure;
uint16_t calib_now;
};

/**
extern declarations of functions and data structures
which can be accessed from different modules
Expand All @@ -150,6 +160,7 @@ extern char moduleName[];
extern uint8_t actSlot;
extern uint8_t addonUpgrade;
extern struct SensorData sensorData;
extern struct I2CSensorValues sensorValues;
extern struct SlotSettings slotSettings;
extern const struct SlotSettings defaultSlotSettings;
extern uint8_t workingmem[WORKINGMEM_SIZE]; // working memory (command parser, IR-rec/play)
Expand All @@ -162,4 +173,29 @@ extern char keystringBuffer[MAX_KEYSTRINGBUFFER_LEN]; // storage for all button
#define strcmp_FM strcmp
typedef char* uint_farptr_t_FM;

/**
* Some define checks to warn from building in debug settings
*/
#ifdef DEBUG_OUTPUT_MEMORY
#warning "DEBUG_OUTPUT_MEMORY active! (GUI might not work)"
#endif
#ifdef DEBUG_OUTPUT_FULL
#warning "DEBUG_OUTPUT_FULL active! (GUI might not work)"
#endif
#ifdef DEBUG_OUTPUT_KEYS
#warning "DEBUG_OUTPUT_KEYS active! (GUI might not work)"
#endif
#ifdef DEBUG_OUTPUT_IR
#warning "DEBUG_OUTPUT_IR active! (GUI might not work)"
#endif
#ifdef DEBUG_OUTPUT_SENSORS
#warning "DEBUG_OUTPUT_SENSORS active! (GUI might not work)"
#endif
#ifdef DEBUG_DELAY_STARTUP
#warning "DELAY_STARTUP is active, do not release this way!"
#endif
#ifdef DEBUG_NO_TONE
#warning "DEBUG_NO_TONE is defined, do not release this way!"
#endif

#endif
137 changes: 137 additions & 0 deletions FLipWare/NAU7802.h2
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
FLipWare - AsTeRICS Foundation
For more info please visit: https://www.asterics-foundation.org

Module: NAU7802.h - register definitions for the NAU7802 sensor chip

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; See the GNU General Public License:
http://www.gnu.org/licenses/gpl-3.0.en.html

*/

#ifndef _NAU7802_H_
#define _NAU7802_H_

/** fwd declarations */
static inline uint8_t readNAURegister(uint8_t reg_addr);
static inline uint32_t readNAUADC();
static inline void writeNAURegister(uint8_t reg_addr, uint8_t val);

static inline uint8_t readNAURegister(uint8_t reg_addr)
{
Wire.beginTransmission(i2c_addr);
Wire.write(reg_addr);
Wire.requestFrom(i2c_addr,1);
Wire.endTransmission();
delayMicroseconds(100);
return Wire.read();
}

static inline uint32_t readNAUADC()
{
Wire.beginTransmission(i2c_addr);
Wire.write(NAU7802_OUTPUT);
Wire.requestFrom(i2c_addr,3);
Wire.endTransmission();
delayMicroseconds(100);
uint32_t val = Wire.read();
val |= Wire.read() << 8;
val |= Wire.read() << 16;
return val;
}

static inline void writeNAURegister(uint8_t reg_addr, uint8_t val)
{
Wire.beginTransmission(i2c_addr);
Wire.write(reg_addr);
Wire.write(val);
Wire.endTransmission();
}

//NAU7802 startup sequence
writeNAURegister(NAU7802_PU_CTRL,1); //set register reset
delay(10);
writeNAURegister(NAU7802_PU_CTRL,0); //clear register reset
delayMicroseconds(200); //wait, then check the pwr up bit
for(uint8_t i = 0; i<4; i++) { //do 4 retries
if(readNAURegister(NAU7802_PU_CTRL) & (1<<3))
{
#ifdef DEBUG_OUTPUT_SENSORS
Serial.println("SEN: NAU7802 powered up");
#endif
break;
}
delay(10);
if(i == 3) {
Serial.println("SEN: error, NAU7802 not powering up!");
return;
}
}

//enable digital
writeNAURegister(NAU7802_PU_CTRL, (1<<1));
//enable analog + digital
writeNAURegister(NAU7802_PU_CTRL, (1<<2)|(1<<1));
//wait
delay(600);
//setup LDO / gain
writeNAURegister(NAU7802_CTRL1, NAU7802_LDO_30 | NAU7802_PGA_128);
//set sampling rate to 320SPS.
writeNAURegister(NAU7802_CTRL2, NAU7802_SPS_320);


/**
* @brief I2C address of the NAU7802
*/
#define NAU7802_ADDR 0b00101010

/** Power control */
#define NAU7802_PU_CTRL 0x00
/** Config/Ctrl 1 */
#define NAU7802_CTRL1 0x01
/** Config/Ctrl 2 */
#define NAU7802_CTRL2 0x02

/** 0x03-0x05: channel 1 offset calib */
/** 0x06-0x09: channel 1 gain ctrl */
/** 0x0A-0x0C: channel 2 offset calib */
/** 0x0D-0x10: channel 2 gain ctrl */

/** I2C ctrl */
#define NAU7802_I2CCTRL 0x11
/** ADC output LSB / starting address */
#define NAU7802_OUTPUT 0x12
/** ADC ctrl */
#define NAU7802_ADCCTRL 0x15
/** PGA ctrl */
#define NAU7802_PGACTRL 0x1B
/** Power ctrl */
#define NAU7802_POWER 0x1C
/** Revision ID */
#define NAU7802_ID 0x1F

/***** LDO settings *****/
#define NAU7802_LDO_33 ((0b100)<<3)
#define NAU7802_LDO_30 ((0b101)<<3)
#define NAU7802_LDO_27 ((0b110)<<3)
#define NAU7802_LDO_24 ((0b111)<<3)

/***** gain settings *****/
#define NAU7802_PGA_128 0b111
#define NAU7802_PGA_64 0b110
#define NAU7802_PGA_32 0b101
#define NAU7802_PGA_16 0b100
#define NAU7802_PGA_8 0b011
#define NAU7802_PGA_4 0b010
#define NAU7802_PGA_2 0b001
#define NAU7802_PGA_1 0b000

/***** sampling rate *****/
#define NAU7802_SPS_320 ((0b111)<<4)
#define NAU7802_SPS_80 ((0b011)<<4)
#define NAU7802_SPS_40 ((0b010)<<4)
#define NAU7802_SPS_20 ((0b001)<<4)
#define NAU7802_SPS_10 ((0b000)<<4)

#endif /*_NAU7802_H_*/
Loading

0 comments on commit ed8b7f9

Please sign in to comment.