Software Upload 2

Firmware-Header ist fertig und kann nun zur Implementation zur DA verwendet werden. Anbei ist ein Beispiel der Verwendung!
This commit is contained in:
Erik Tóth
2025-10-31 15:37:57 +01:00
parent 64c6bfd271
commit 54d8f104f4
4 changed files with 129 additions and 28 deletions

View File

@@ -17,6 +17,8 @@
#define N_MAX_COLS 8
#define MS_DEBOUNCE 20
#define N_MAX_DAC_CH 4
struct Key
{
int row;
@@ -59,4 +61,26 @@ class Keyboard
bool _isEqualKey(Key k1, Key k2);
};
class CV
{
public:
CV(Adafruit_MCP4728 *dac, TwoWire *wire, uint8_t nCV, MCP4728_channel_t *cvChannelMap, uint16_t *keyToVoltage, uint8_t row, uint8_t col);
bool begin(uint8_t pinSDA, uint8_t pinSCL);
void setVoltage(uint8_t cvIndex, Key k);
void setVoltage(uint8_t cvIndex, uint16_t mV);
void clearAll();
private:
Adafruit_MCP4728 *_dac;
TwoWire *_wire;
uint8_t _nCV;
uint8_t _row;
uint8_t _col;
MCP4728_channel_t _cvChannelMap[N_MAX_DAC_CH];
uint16_t *_keyToVoltage;
uint8_t _getKeyToVoltageIndex(uint8_t row, uint8_t col);
uint8_t _getKeyToVoltageIndex(Key k);
};
#endif

View File

@@ -11,19 +11,20 @@
#include <Arduino.h>
#include <Wire.h>
// CONSTANTS DEFINITONS
#define N_KEYBOARD_ROW 4
#define N_KEYBOARD_COL 3
#define BAUDRATE 115200
#define N_KEYBOARD_ROW 4
#define N_KEYBOARD_COL 3
#define N_CV_GATES 2
#define BAUDRATE 115200
// PIN DEFENTITIONS
// I2C PINS
#define PIN_SDA 15
#define PIN_SCL 16
#define PIN_SDA 15
#define PIN_SCL 16
// KEYBOARD PINS
#define PIN_K_R0 7
#define PIN_K_R1 8
#define PIN_K_R2 9
#define PIN_K_R3 10
#define PIN_K_C0 1
#define PIN_K_C1 2
#define PIN_K_C2 4
#define PIN_K_R0 7
#define PIN_K_R1 8
#define PIN_K_R2 9
#define PIN_K_R3 10
#define PIN_K_C0 1
#define PIN_K_C1 2
#define PIN_K_C2 4
#endif

View File

@@ -50,31 +50,31 @@ void Keyboard::begin()
void Keyboard::update()
{
unsigned long now = millis();
for(uint8_t i = 0; i < _nCols; i++)
for(uint8_t col = 0; col < _nCols; col++)
{
digitalWrite(_pinsCol[i], HIGH);
for(uint8_t j = 0; j < _nRows; ++j)
digitalWrite(_pinsCol[col], HIGH);
for(uint8_t row = 0; row < _nRows; ++row)
{
bool reading = (digitalRead(_pinsRow[j]) == HIGH);
bool reading = (digitalRead(_pinsRow[row]) == HIGH);
if(reading != _keyStateLatest[i][j])
if(reading != _keyStateLatest[row][col])
{
_keyStateLatest[i][j] = reading;
_lastChangeTime[i][j] = now;
_keyStateLatest[row][col] = reading;
_lastChangeTime[row][col] = now;
}
if((now - _lastChangeTime[i][j]) > MS_DEBOUNCE)
if((now - _lastChangeTime[row][col]) > MS_DEBOUNCE)
{
if(reading != _keyState[i][j])
if(reading != _keyState[row][col])
{
_keyState[i][j] = reading;
_keyState[row][col] = reading;
if(reading) _addActiveKey(i, j);
else _removeActiveKey(i, j);
if(reading) _addActiveKey(row, col);
else _removeActiveKey(row, col);
}
}
}
digitalWrite(_pinsCol[i], LOW);
digitalWrite(_pinsCol[col], LOW);
}
if((_nActiveKeys == 1) && _inQueue(NOT_A_KEY)) _nActiveKeys = 0;
}
@@ -170,3 +170,66 @@ void Keyboard::_removeActiveKey(uint8_t row, uint8_t col)
}
}
}
/*!
* @param dac Adafruit_MCP4728 object
* @param wire TwoWire object
* @param nCV Number of CV-Gates
* @param cvChannelMap Maps CV-Gate-Index to a MCP4728 output-channel
* @param keyToVoltage One dimensional array of size row times col
* @param row Keyboard matrix rows
* @param col Keyboard matrix cols
*/
CV::CV(Adafruit_MCP4728 *dac, TwoWire *wire, uint8_t nCV, MCP4728_channel_t *cvChannelMap, uint16_t *keyToVoltage, uint8_t row, uint8_t col)
{
_dac = dac;
_wire = wire;
_nCV = nCV;
_row = row;
_col = col;
_keyToVoltage = keyToVoltage;
for(uint8_t i = 0; i < N_MAX_DAC_CH; i++)
{
_cvChannelMap[i] = i < _nCV ? cvChannelMap[i] : (MCP4728_channel_t)(0);
}
}
bool CV::begin(uint8_t pinSDA, uint8_t pinSCL)
{
if((_wire->begin(pinSDA, pinSCL) && _dac->begin(96U, _wire)))
{
clearAll();
return true;
}
else return false;
}
void CV::setVoltage(uint8_t cvIndex, uint16_t mV)
{
if(cvIndex >= _nCV) return;
MCP4728_channel_t ch = _cvChannelMap[cvIndex];
_dac->setChannelValue(ch, map(mV, 0, 2048, 0, 4095), MCP4728_VREF_INTERNAL);
}
void CV::setVoltage(uint8_t cvIndex, Key k)
{
if(cvIndex >= _nCV) return;
if(isNotKey(k)) setVoltage(cvIndex, 0);
setVoltage(cvIndex, _keyToVoltage[_getKeyToVoltageIndex(k)]);
}
void CV::clearAll()
{
for(uint8_t i = 0; i < _nCV; i++) setVoltage(i, 0);
}
uint8_t CV::_getKeyToVoltageIndex(uint8_t row, uint8_t col)
{
return (row*_col + col);
}
uint8_t CV::_getKeyToVoltageIndex(Key k)
{
return (k.row*_col + k.col);
}

View File

@@ -6,11 +6,22 @@ static byte pins_keyboard_col[N_KEYBOARD_COL] = {PIN_K_C0, PIN_K_C1, PIN_K_C2};
Keyboard keyboard(N_KEYBOARD_ROW, N_KEYBOARD_COL, pins_keyboard_row, pins_keyboard_col);
Adafruit_MCP4728 MCP4728;
MCP4728_channel_t cvMap[N_CV_GATES] = {MCP4728_CHANNEL_A, MCP4728_CHANNEL_B};
uint16_t keyToVoltage[N_KEYBOARD_ROW*N_KEYBOARD_COL] = {
1*83, 5*83, 9*83,
2*83, 6*83, 10*83,
3*83, 7*83, 11*83,
4*83, 8*83, 12*83
};
CV cv(&MCP4728, &Wire, N_CV_GATES, cvMap, keyToVoltage, N_KEYBOARD_ROW, N_KEYBOARD_COL);
void setup()
{
Serial.begin(BAUDRATE);
//Wire.begin(PIN_SDA, PIN_SCL);
keyboard.begin();
cv.begin(PIN_SDA, PIN_SCL);
}
void loop()
@@ -22,13 +33,15 @@ void loop()
if(n > 0)
{
Serial.printf("\n\rCurrent queue length: %i", n);
for(int i = 0; i < n; i++)
for(int i = 0; (i < N_CV_GATES) && (i < n); i++)
{
Key k = keyboard.getQueue(i);
cv.setVoltage(i, k);
if(isNotKey(k)) Serial.printf("\n\rQueue position %i: NOT A KEY", i);
else Serial.printf("\n\rQueue position %i: R%iC%i", i, k.row, k.col);
}
}
else cv.clearAll();
delay(50);
}