Lab_interaccio/2020/MGC3130_TEST/MGC3130_TEST.ino
2025-02-25 21:29:42 +01:00

164 lines
10 KiB
C++

/*************************************************************************************************************
# HISTORY
#
# v1.0 - Initial Release
#
# =============================================================================
# This is an example and explains how to use the MGC3130 library developed by Matteo Destro for Futura Group srl
#
# www.Futurashop.it
# www.open-electronics.org
#
# BSD license, all text above must be included in any redistribution
#
# The code for this demo is splitted into four file:
#
# - MGC3130_Demo -> Main file project
# - DigitalInput -> Contains code to manage digital inputs and the respective functions to manage it
# - Digital Output -> Contains code to manage digital output and the respective functions to manage it
# - Timersint -> Contains code to manage Timer1 of ATMEGA328P. Timer1 generates an interrupt every 2 mSec.
# With this resolution (2 mSec) it is possible to manage the timer variables used in this demo.
# For example the TimeOut used to filter the unwanted variations on the digital inputs (debouncing)
#
# - Arduino Uno Rev. 3
# - Arduino Leonardo Rev. 3
# - ArdUnoR3_RaspPiB+_GE_100x50 Rev. 1.0 (Parametrization file Elettrodo_Ard_Rasp_100x50.enz)
# - Gestic_Elettrodo_100x50_10 (Parametrization file Elettrodo_100x50.enz)
# - Gestic_Elettrodo_80x80_10 (Parametrization file Elettrodo_80x80.enz)
# - Gestic_Elettrodo_140x90_10 (Parametrization file Elettrodo_140x90.enz)
#
# Arduino Uno Rev. 3 (PinOut Arduino to MGC3130 Electrode if is used CN1 connector:
#
# Line Reset: Arduino pin 8 -> Electrode pin 6
# Line Ts: Arduino pin 9 -> Electrode pin 1
# Line SDA: Arduino pin 27 -> Electrode pin 4
# Line SCL: Arduino pin 28 -> Electrode pin 5
#
# +3,3Vdc -> Electrode pin 2
# GND -> Electrode pin 3
#
# If is used "ArdUnoR3_RaspPiB+_GE_100x50 Rev. 1.0" it is possible to connect the ArduinoUno Rev. 3
# directly to this hardware using the connection on the bottom layer. It is totally compatible with
# ArduinoUno board R3 and Arduino Leonardo R3
#
# Note: For this demo is necessary the FT1079K shield developed by Futura Electronica to report the
# gesture on the digital output (Relè). In this demo we have used two FT1079K shield
#
# The gestures reported on the digital output are:
#
# - Tap South -> First FT1079 shield. For digital output it is used the PORTA of MCP23017. Hardware address 0x00, point 0
# - Tap West -> First FT1079 shield. For digital output it is used the PORTA of MCP23017. Hardware address 0x00, point 1
# - Tap North -> First FT1079 shield. For digital output it is used the PORTA of MCP23017. Hardware address 0x00, point 2
# - Tap East -> First FT1079 shield. For digital output it is used the PORTA of MCP23017. Hardware address 0x00, point 3
# - Tap Centre -> First FT1079 shield. For digital output it is used the PORTA of MCP23017. Hardware address 0x00, point 4
# - Double Tap South -> First FT1079 shield. For digital output it is used the PORTA of MCP23017. Hardware address 0x00, point 5
# - Double Tap West -> First FT1079 shield. For digital output it is used the PORTA of MCP23017. Hardware address 0x00, point 6
# - Double Tap North -> First FT1079 shield. For digital output it is used the PORTA of MCP23017. Hardware address 0x00, point 7
# - Double Tap East -> Second FT1079 shield. For digital output it is used the PORTA of MCP23017. Hardware address 0x01, point 0
# - Double Tap Centre -> Second FT1079 shield. For digital output it is used the PORTA of MCP23017. Hardware address 0x01, point 1
# - West To East -> Second FT1079 shield. For digital output it is used the PORTA of MCP23017. Hardware address 0x01, point 2
# - East to West -> Second FT1079 shield. For digital output it is used the PORTA of MCP23017. Hardware address 0x01, point 3
# - South to North -> Second FT1079 shield. For digital output it is used the PORTA of MCP23017. Hardware address 0x01, point 4
# - North to South -> Second FT1079 shield. For digital output it is used the PORTA of MCP23017. Hardware address 0x01, point 5
# - ClockWise -> Second FT1079 shield. For digital output it is used the PORTA of MCP23017. Hardware address 0x01, point 6
# - CounterClockWise -> Second FT1079 shield. For digital output it is used the PORTA of MCP23017. Hardware address 0x01, point 7
#
# =============================================================================
#
# INSTALLATION
# The 3 library files (MGC3130.cpp, MGC3130.h and keywords.txt) in the MGC3130 folder should be placed in your Arduino Library folder.
# Run the MGC3130_Demo.ino file from your Arduino IDE.
#
# =============================================================================
#
# SUPPORT
#
# info@open-electronics.org
#
*************************************************************************************************************/
#include <Wire.h>
#include "MGC3130.h"
//======================================================================
MGC3130 mgc3130 = MGC3130();
//======================================================================
//======================================================================
// Define timer Constants and Variables
#define TIMEBASE 2 // Time Base: 2mSec
#define T_10MSEC (10/TIMEBASE) // Defines a constant for a timeout of 10 mSec
#define T_20MSEC (20/TIMEBASE) // Defines a constant for a timeout of 20 mSec
#define T_50MSEC (50/TIMEBASE) // Defines a constant for a timeout of 50 mSec
#define T_100MSEC (100/TIMEBASE) // Defines a constant for a timeout of 100 mSec
#define T_150MSEC (150/TIMEBASE) // Defines a constant for a timeout of 150 mSec
#define T_200MSEC (200/TIMEBASE) // Defines a constant for a timeout of 200 mSec
#define T_250MSEC (250/TIMEBASE) // Defines a constant for a timeout of 250 mSec
#define T_500MSEC (500/TIMEBASE) // Defines a constant for a timeout of 500 mSec
#define T_750MSEC (750/TIMEBASE) // Defines a constant for a timeout of 750 mSec
#define T_1SEC (1000/TIMEBASE) // Defines a constant for a timeout of 1,000 Sec
#define T_1SEC250 (1250/TIMEBASE) // Defines a constant for a timeout of 1,250 Sec
#define T_1SEC500 (1500/TIMEBASE) // Defines a constant for a timeout of 1,500 Sec
#define T_1SEC750 (1750/TIMEBASE) // Defines a constant for a timeout of 1,750 Sec
#define T_2SEC (2000/TIMEBASE) // Defines a constant for a timeout of 2,000 Sec
#define T_2SEC5 (2500/TIMEBASE) // Defines a constant for a timeout of 2,500 Sec
#define T_3SEC (3000/TIMEBASE) // Defines a constant for a timeout of 3,000 Sec
#define T_4SEC (4000/TIMEBASE) // Defines a constant for a timeout of 4,000 Sec
#define T_5SEC (5000/TIMEBASE) // Defines a constant for a timeout of 5,000 Sec
#define T_10SEC (10000/TIMEBASE) // Defines a constant for a timeout of 10,000 Sec
#define T_15SEC (15000/TIMEBASE) // Defines a constant for a timeout of 15,000 Sec
#define T_20SEC (20000/TIMEBASE) // Defines a constant for a timeout of 20,000 Sec
#define T_25SEC (25000/TIMEBASE) // Defines a constant for a timeout of 25,000 Sec
#define T_30SEC (30000/TIMEBASE) // Defines a constant for a timeout of 30,000 Sec
#define T_60SEC (60000/TIMEBASE) // Defines a constant for a timeout of 60,000 Sec
//======================================================================
uint8_t Reset_MGC3130 = 33; // MGC3130 Reset line
uint8_t Ts_MGC3130 = 32; // MGC3130 TS line
uint8_t Addr_MGC3130 = 0x42; // MGC3130 hardware address
//======================================================================
#define TOUCH_CODE 1 // Code, for touch gesture, used to fill the array gesture storage. See variables above
#define TAP_CODE 2 // Code, for tap gesture, used to fill the array gesture storage. See variables above
#define DOUBLE_TAP_CODE 3 // Code, for double tap gesture, used to fill the array gesture storage. See variables above
//============================================
// Use these constants if no filter is set on the gestures that has been read from MGC3130
#define GESTURE_DOUBLE_TAP_NORTH 10 // \
#define GESTURE_DOUBLE_TAP_SOUTH 10 // |
#define GESTURE_DOUBLE_TAP_WEST 10 // >--- The "double tap gesture" is composed by a sequence of gestures. So the sum of gesture codes that have been read returns the value indicated
#define GESTURE_DOUBLE_TAP_EAST 10 // |
#define GESTURE_DOUBLE_TAP_CENTRE 10 // /
#define GESTURE_TAP_NORTH 3 // \
#define GESTURE_TAP_SOUTH 3 // |
#define GESTURE_TAP_WEST 3 // >--- The "tap gesture" is composed by a sequence of gestures. So the sum of gesture codes that have been read returns the value indicated
#define GESTURE_TAP_EAST 3 // |
#define GESTURE_TAP_CENTRE 3 // /
#define GESTURE_TOUCH_NORTH 1 // \
#define GESTURE_TOUCH_SOUTH 1 // |
#define GESTURE_TOUCH_WEST 1 // >--- The "Touch Gesture"
#define GESTURE_TOUCH_EAST 1 // |
#define GESTURE_TOUCH_CENTRE 1 // /
//============================================
void setup() {
mgc3130.ResetDevice(Reset_MGC3130); // hold MGC3130 in reset mode
mgc3130.SetSerial(115200, SERIAL_8N1); // Initializes serial interface
mgc3130.SetAdd(Addr_MGC3130); // Set MGC3130 address
mgc3130.Begin(Ts_MGC3130, Reset_MGC3130); // Initializes TS and Reset lines
}
//======================================================================
//======================================================================
void loop() {
if (mgc3130.GetTsLineStatus(Ts_MGC3130) == 0) {
mgc3130.GetEvent(); // Start read data from MGC3130
mgc3130.DecodeGesture(); // Decode Gesture
mgc3130.ReleaseTsLine(Ts_MGC3130); // Release TS Line
}
}
//======================================================================