Skip to content

Commit

Permalink
touch update
Browse files Browse the repository at this point in the history
  • Loading branch information
xukangmin committed Aug 22, 2019
1 parent 2348c05 commit 259db4a
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 16 deletions.
6 changes: 2 additions & 4 deletions avr/boards.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Overhauled summer 2015 by Dr. Azzy (Spence Konde) and again in early 2016 to add support for more chips.

menu.chip=Chip

tiny16.name=TinyCore 16
Expand All @@ -25,5 +23,5 @@ tiny32.build.mcu=attiny3217
tiny32.build.variant=tiny32
tiny32.build.f_cpu=20000000L
tiny32.build.core=tiny
tiny16.build.dacvref=-DDACVREF=2
tiny16.build.extra_flags=-DMILLIS_USE_TIMERA0 -DNO_EXTERNAL_I2C_PULLUP -DUSE_TIMERD0_PWM {build.dacvref}
tiny32.build.dacvref=-DDACVREF=2
tiny32.build.extra_flags=-DMILLIS_USE_TIMERA0 -DNO_EXTERNAL_I2C_PULLUP -DUSE_TIMERD0_PWM {build.dacvref}
3 changes: 2 additions & 1 deletion avr/cores/tiny/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ extern const uint8_t PROGMEM digital_pin_to_port[];
extern const uint8_t PROGMEM digital_pin_to_bit_mask[];
extern const uint8_t PROGMEM digital_pin_to_bit_position[];
extern const uint8_t PROGMEM digital_pin_to_timer[];

extern const uint8_t PROGMEM touch_pin_to_channel[];
// Get the bit location within the hardware port of the given virtual pin.
// This comes from the pins_*.c file for the active board configuration.
//
Expand Down Expand Up @@ -121,6 +121,7 @@ void setup_timers();
bool isDoubleBondedActive(uint8_t pin);

#define digitalPinToPort(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_port + pin) : NOT_A_PIN )
#define digitalPinToTouchPin(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(touch_pin_to_channel + pin) : NOT_A_PIN )
#define digitalPinToBitPosition(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_bit_position + pin) : NOT_A_PIN )
#define analogPinToBitPosition(pin) ( (digitalPinToAnalogInput(pin)!=NOT_A_PIN) ? pgm_read_byte(digital_pin_to_bit_position + pin + 0) : NOT_A_PIN )
#define digitalPinToBitMask(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_bit_mask + pin) : NOT_A_PIN )
Expand Down
15 changes: 13 additions & 2 deletions avr/cores/tiny/wiring.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,21 @@ void init()

#endif

#if (HWSERIAL0_MUX!=0 || SPI_MUX!=0 ||TWI_MUX!=0)
PORTMUX.CTRLB=HWSERIAL0_MUX|SPI_MUX|TWI_MUX; //Set PORTMUX
// alternative pins for TWI and UART are conflicting, so MUX settings must be separated

#if (HWSERIAL0_MUX!=0)
PORTMUX.CTRLB |= HWSERIAL0_MUX;
#endif

#if (SPI_MUX!=0)
PORTMUX.CTRLB |= SPI_MUX;
#endif

#if (TWI_MUX!=0)
PORTMUX.CTRLB |= TWI_MUX;
#endif


setup_timers();

/********************* TIMER for system time tracking **************************/
Expand Down
157 changes: 152 additions & 5 deletions avr/libraries/Touch/src/TinyTouch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "TinyTouch.h"
#include "Arduino.h"

TinyTouch::TinyTouch(/* args */)
TinyTouch::TinyTouch()
{
while (RTC.STATUS > 0) { /* Wait for all register to be synchronized */
}
Expand All @@ -23,21 +23,168 @@ TinyTouch::TinyTouch(/* args */)

RTC.INTCTRL = 1 << RTC_CMP_bp /* Compare Match Interrupt enable: enabled */
| 0 << RTC_OVF_bp; /* Overflow Interrupt enable: disabled */


}

TinyTouch::~TinyTouch()
{
}

void TinyTouch::init()
void TinyTouch::begin() // default initializer
{
touch_init();
}

void TinyTouch::TouchHandle() {
touch_process();
void TinyTouch::begin(uint8_t *pinList, uint16_t totalNumPins) {

// to do, init timer and pins

time_to_measure_touch_flag = 0;

touch_postprocess_request = 0;

measurement_done_touch = 0;

module_error_code = 0;

uint16_t sensor_nodes;
touch_ret_t touch_ret = TOUCH_SUCCESS;

ptc_qtlib_acq_gen1 = {totalNumPins, DEF_SENSOR_TYPE, DEF_PTC_CAL_AUTO_TUNE, DEF_SEL_FREQ_INIT};

ptc_qtlib_node_stat1 = new qtm_acq_node_data_t[totalNumPins];

ptc_seq_node_cfg1 = new qtm_acq_t161x_node_config_t[totalNumPins];

touch_acq_signals_raw = new uint16_t[totalNumPins];

for(int i = 0; i < totalNumPins; i++) {
ptc_seq_node_cfg1[i].node_xmask = X_NONE;
ptc_seq_node_cfg1[i].node_ymask = Y(digitalPinToTouchPin(pinList[i]));
ptc_seq_node_cfg1[i].node_csd = 0;
ptc_seq_node_cfg1[i].node_rsel_prsc = PRSC_DIV_SEL_4;
ptc_seq_node_cfg1[i].node_gain = NODE_GAIN(GAIN_1, GAIN_1);
ptc_seq_node_cfg1[i].node_oversampling = FILTER_LEVEL_16;
}


qtlib_acq_set1 = {&ptc_qtlib_acq_gen1, ptc_seq_node_cfg1, ptc_qtlib_node_stat1};

/* Init acquisition module */
qtm_ptc_init_acquisition_module(&qtlib_acq_set1);


/* Init pointers to DMA sequence memory */
qtm_ptc_qtlib_assign_signal_memory(touch_acq_signals_raw);

/* Initialize sensor nodes */

for (sensor_nodes = 0u; sensor_nodes < totalNumPins; sensor_nodes++)
{
/* Enable each node for measurement and mark for calibration */
qtm_enable_sensor_node(&qtlib_acq_set1, sensor_nodes);
qtm_calibrate_sensor_node(&qtlib_acq_set1, sensor_nodes);
}

qtlib_key_grp_config_set1 = {DEF_NUM_SENSORS,
DEF_TOUCH_DET_INT,
DEF_MAX_ON_DURATION,
DEF_ANTI_TCH_DET_INT,
DEF_ANTI_TCH_RECAL_THRSHLD,
DEF_TCH_DRIFT_RATE,
DEF_ANTI_TCH_DRIFT_RATE,
DEF_DRIFT_HOLD_TIME,
DEF_REBURST_MODE};

qtlib_key_data_set1 = new qtm_touch_key_data_t[totalNumPins];

qtlib_key_configs_set1 = new qtm_touch_key_config_t[totalNumPins];

for (sensor_nodes = 0u; sensor_nodes < totalNumPins; sensor_nodes++)
{
qtlib_key_configs_set1[sensor_nodes].channel_threshold = 20;
qtlib_key_configs_set1[sensor_nodes].channel_hysteresis = HYST_25;
qtlib_key_configs_set1[sensor_nodes].channel_aks_group = NO_AKS_GROUP;
}

qtlib_key_set1
= {&qtlib_key_grp_data_set1, &qtlib_key_grp_config_set1, qtlib_key_data_set1, qtlib_key_configs_set1};

/* Enable sensor keys and assign nodes */
for (sensor_nodes = 0u; sensor_nodes < totalNumPins; sensor_nodes++) {
qtm_init_sensor_key(&qtlib_key_set1, sensor_nodes, &ptc_qtlib_node_stat1[sensor_nodes]);
}


}

static void TinyTouch::qtm_measure_complete_callback(void)
{
touch_postprocess_request = 1u;
}

static void TinyTouch::qtm_error_callback(uint8_t error)
{
module_error_code = error + 1u;

}

void TinyTouch::touchHandle() {
touch_ret_t touch_ret;

/* check the time_to_measure_touch_flag flag for Touch Acquisition */
if (time_to_measure_touch_flag == 1u) {
/* Do the acquisition */
touch_ret = qtm_ptc_start_measurement_seq(&qtlib_acq_set1, TinyTouch::qtm_measure_complete_callback);

/* if the Acquistion request was successful then clear the request flag */
if (TOUCH_SUCCESS == touch_ret) {
/* Clear the Measure request flag */
time_to_measure_touch_flag = 0u;
}
}

/* check the flag for node level post processing */
if (touch_postprocess_request == 1u) {
/* Reset the flags for node_level_post_processing */
touch_postprocess_request = 0u;

/* Run Acquisition module level post processing*/
touch_ret = qtm_acquisition_process();

/* Check the return value */
if (TOUCH_SUCCESS == touch_ret) {
/* Returned with success: Start module level post processing */
touch_ret = qtm_key_sensors_process(&qtlib_key_set1);
if (TOUCH_SUCCESS != touch_ret) {
qtm_error_callback(1);
}
} else {
/* Acq module Eror Detected: Issue an Acq module common error code 0x80 */
qtm_error_callback(0);
}

if ((0u != (qtlib_key_set1.qtm_touch_key_group_data->qtm_keys_status & 0x80u))) {
time_to_measure_touch_flag = 1u;
} else {
measurement_done_touch = 1u;
}

}
}

void TinyTouch::end() {
// clean up
delete ptc_qtlib_node_stat1;
delete ptc_seq_node_cfg1;
delete touch_acq_signals_raw;

delete qtlib_key_data_set1;

delete qtlib_key_configs_set1;
}

uint16_t TinyTouch::get_value(uint8_t sensor_node) {
uint16_t TinyTouch::getValue(uint8_t sensor_node) {
return get_sensor_node_signal(sensor_node);
}
42 changes: 38 additions & 4 deletions avr/libraries/Touch/src/TinyTouch.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
#ifndef TINY_TOUCH_H
#define TINY_TOUCH_H

#include "QTouch/include/touch_api_ptc.h"
#include "pins_arduino.h"
#include "Arduino.h"
#include "QTouch/touch.h"

class TinyTouch{
private:
qtm_acq_t161x_node_config_t *ptc_seq_node_cfg1;
qtm_acq_node_data_t *ptc_qtlib_node_stat1;
qtm_acq_node_group_config_t ptc_qtlib_acq_gen1;
qtm_acquisition_control_t qtlib_acq_set1;
uint16_t *touch_acq_signals_raw;

qtm_touch_key_group_config_t qtlib_key_grp_config_set1;
qtm_touch_key_group_data_t qtlib_key_grp_data_set1;
qtm_touch_key_data_t *qtlib_key_data_set1;
qtm_touch_key_config_t *qtlib_key_configs_set1;
qtm_touch_key_control_t qtlib_key_set1;

/* Flag to indicate time for touch measurement */
static volatile uint8_t time_to_measure_touch_flag;

/* postporcess request flag */
static volatile uint8_t touch_postprocess_request;

/* Measurement Done Touch Flag */
static volatile uint8_t measurement_done_touch;

/* Error Handling */
static uint8_t module_error_code;

static void qtm_measure_complete_callback(void);
static void qtm_error_callback(uint8_t error);

public:
TinyTouch();
~TinyTouch();
void init();
void TouchHandle();
uint16_t get_value(uint8_t sensor_node);

void begin();
void begin(uint8_t *pinList, uint16_t totalNumPins);
void end();
void touchHandle();

uint16_t getValue(uint8_t sensor_node);
};


Expand Down
4 changes: 4 additions & 0 deletions avr/libraries/Wire/src/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ TwoWire::TwoWire()

// Public Methods //////////////////////////////////////////////////////////////

void TwoWire::useAlternatePins(){
PORTMUX.CTRLB |= PORTMUX_TWI0_ALTERNATE_gc;
}

void TwoWire::begin(void)
{
rxBufferIndex = 0;
Expand Down
1 change: 1 addition & 0 deletions avr/libraries/Wire/src/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class TwoWire : public HardwareI2C
void setClock(uint32_t);
void beginTransmission(uint8_t);
void beginTransmission(int);
void useAlternatePins();
uint8_t endTransmission(void);
uint8_t endTransmission(bool);
uint8_t requestFrom(uint8_t, size_t);
Expand Down
20 changes: 20 additions & 0 deletions avr/variants/tiny16/pins_arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,26 @@ const uint8_t PROGMEM analog_pin_to_channel[] = {
7 // 16 PA7
};

const uint8_t PROGMEM touch_pin_to_channel[] = {
NOT_A_PIN, // 0 PB3
NOT_A_PIN, // 1 PB2
4, // 2 PB1
5, // 3 PB0
13, // 4 PB4
12, // 5 PB5
6, // 6 PC0
7, // 7 PC1
8, // 8 PC2
9, // 9 PC3
NOT_A_PIN, // 10 PA1
NOT_A_PIN, // 11 PA2
NOT_A_PIN, // 12 PA3
0, // 13 PA4
1, // 14 PA5
2, // 15 PA6
3 // 16 PA7
};

#define digitalPinToAnalogInput(p) ((p < NUM_TOTAL_PINS) ? pgm_read_byte(analog_pin_to_channel + p) : NOT_A_PIN )

#endif
Expand Down

0 comments on commit 259db4a

Please sign in to comment.