1
0
mirror of https://github.com/Adam-Ant/macro-pad synced 2024-11-06 02:06:21 +00:00
Macro-Pad/Firmware/Firmware.ino
2016-06-07 02:21:01 +01:00

129 lines
2.8 KiB
C++

#include <Keypad.h>
#include <Joystick.h>
#include <EEPROM.h>
const byte ROWS = 5; //four rows
const byte COLS = 4; //three columns
const int pressdelay = 50;
byte rowPins[ROWS] = { 2, 3, 4, 5, 6 }; //connect to the row pinouts of the keypad
byte colPins[COLS] = { 15, 14, 16, 10 }; //connect to the column pinouts of the keypad
const char keys[ROWS][COLS] = {
{ '0', '1', '2', '3'},
{ '4', '5', '6', '7'},
{ '8', '9', 'a', 'b'},
{ 'c', 'd', 'e', 'f'},
{ 'g', 'h', 'i', 'j'},
};
const int ledPin = 9;
const int dimVals [] = { 255, 200, 150, 100, 50, 10, 0 };
const int eepromAddress = 88; //Generated by a random external source - griffiths :)
int dimLevel = 6;
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
Joystick.begin(false);
dimLevel = EEPROM.read(eepromAddress);
}
void pressKey(int Key) {
Joystick.setButton(Key, 1);
Joystick.sendState();
delay(pressdelay);
Joystick.setButton(Key, 0);
Joystick.sendState();
}
void changeDim (bool level) { //Are we changing the values up or down?
if (level) { dimLevel -= 1; } // An increase in values means a decrease in brightness!
else { dimLevel +=1; }
//If we are out of bounds, theres no point updating th\\\\\\e value. Just set it back inside boundries to save EEPROM write cycles.
if (dimLevel<0) { dimLevel = 0; }
else if (dimLevel>6){ dimLevel = 6; }
else { // Looks like the value has changed. Commit it to EEPROM.
EEPROM.write(eepromAddress, dimLevel);
Serial.println(dimLevel);
}
}
void loop(){
char key = keypad.getKey();
if (key){
Serial.println(key);
switch(key){
case '0':
//pressKey(0);
changeDim(false);
break;
case '1':
//pressKey(1);
changeDim(true);
break;
case '2':
pressKey(2);
break;
case '3':
pressKey(3);
break;
case '4':
pressKey(4);
break;
case '5':
pressKey(5);
break;
case '6':
pressKey(6);
break;
case '7':
pressKey(7);
break;
case '8':
pressKey(8);
break;
case '9':
pressKey(9);
break;
case 'a':
pressKey(10);
break;
case 'b':
pressKey(11);
break;
case 'c':
pressKey(12);
break;
case 'd':
pressKey(13);
break;
case 'e':
pressKey(14);
break;
case 'f':
pressKey(15);
break;
case 'g':
pressKey(16);
break;
case 'h':
pressKey(17);
break;
case 'i':
pressKey(18);
break;
case 'j':
pressKey(19);
break;
}
}
analogWrite(ledPin, dimVals[dimLevel]);
}