1
0
mirror of https://github.com/sjlongland/atinysynth.git synced 2025-09-13 10:03:15 +10:00

adckbd: Add first stab at ADC-based keyboard.

This works by relying on a resistor ladder that is part-shorted-out by a
button to Vcc.  The ADC detects the button pressed from the voltage seen
on the ADC.

There's some work to be done on this yet.
This commit is contained in:
Stuart Longland 2017-05-01 17:35:19 +10:00
parent 44e2d937d6
commit aa3771dc78
Signed by: stuartl
GPG Key ID: F954BBBB7948D546
2 changed files with 147 additions and 0 deletions

93
ports/attiny85/adckbd.c Normal file
View File

@ -0,0 +1,93 @@
/*!
* Polyphonic synthesizer for microcontrollers: Atmel ATTiny85 port.
* (C) 2016 Stuart Longland
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include "adckbd.h"
/*!
* Last known keyboard state
*/
uint8_t adckbd_last = 0;
/*!
* Current keyboard state
*/
uint8_t adckbd_now = 0;
/*!
* Keyboard state differences from last
*/
uint8_t adckbd_diff = 0;
/* Keyboard thresholds */
#define B0_THRESHOLD 0xe8 /*!< Keyboard button 1 threshold */
#define B1_THRESHOLD 0xc2 /*!< Keyboard button 2 threshold */
#define B2_THRESHOLD 0xa7 /*!< Keyboard button 3 threshold */
#define B3_THRESHOLD 0x80 /*!< Keyboard button 4 threshold */
/*!
* Initialise the ADC keyboard.
*/
void adckbd_init() {
/* This code generates an "operand out of range" assembler error */
#if 0
/* Turn on ADC */
PRR &= ~(1 << PRADC);
#endif
/* Select ADC3, 5V reference, left-justify output */
ADMUX = (3 << MUX0) | (1 << ADLAR);
/* Free running mode */
ADCSRB = 0;
/* Minimum speed ADC, Enable ADC, with interrupts, auto-trigger */
ADCSRA = (1 << ADPS2)
| (1 << ADPS1)
| (1 << ADPS0)
| (1 << ADEN)
| (1 << ADATE)
| (1 << ADIE)
| (1 << ADSC)
;
}
ISR(ADC_vect) {
uint8_t adc = ADCH;
adckbd_last = adckbd_now;
if (adc > B0_THRESHOLD)
adckbd_now |= ADCKBD_PRESSED(0);
else if (adc > B1_THRESHOLD)
adckbd_now |= ADCKBD_PRESSED(1);
else if (adc > B2_THRESHOLD)
adckbd_now |= ADCKBD_PRESSED(2);
else if (adc > B3_THRESHOLD)
adckbd_now |= ADCKBD_PRESSED(3);
else
adckbd_now |= ADCKBD_RELEASED;
adckbd_diff = (adckbd_now & ADCKBD_ALL)
^ (adckbd_last & ADCKBD_ALL);
if (adckbd_diff)
adckbd_now |= ADCKBD_CHANGED;
}

54
ports/attiny85/adckbd.h Normal file
View File

@ -0,0 +1,54 @@
/*!
* Polyphonic synthesizer for microcontrollers: Atmel ATTiny85 port.
* (C) 2016 Stuart Longland
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include <stdint.h>
/*! Number of keys defined */
#define ADCKBD_NUM_KEYS (4)
/*! Bit mask: key (n) pressed */
#define ADCKBD_PRESSED(n) (1 << (n))
/*! Bit mask: all keys */
#define ADCKBD_ALL ((1 << (ADCKBD_NUM_KEYS)) - 1)
/*! Bit mask: keys released */
#define ADCKBD_RELEASED (1 << 6)
/*! Bit mask: change detected */
#define ADCKBD_CHANGED (1 << 7)
/*! Initialisation routine */
void adckbd_init();
/*!
* Last known keyboard state
*/
extern uint8_t adckbd_last;
/*!
* Current keyboard state
*/
extern uint8_t adckbd_now;
/*!
* Differences from last
*/
extern uint8_t adckbd_diff;