Skip to content

Instantly share code, notes, and snippets.

@Crenshinibon
Crenshinibon / gist:3915145
Created October 18, 2012 22:24
Basic keyboard polling
struct Key {
byte id;
boolean pressed;
uint8_t bounceKey;
Key(byte id) : id(id), pressed(false), bounceKey(NOT_BOUNCING) {};
}
boolean toggleKey(struct Key *key) {
key->bounceKey = checkStillBouncing(key->bounceKey);
if(key->bounceKey == NOT_BOUNCING){
@Crenshinibon
Crenshinibon / gist:3917754
Created October 19, 2012 11:39
Keymatrix polling Function
static void scanKeys(){
//iterate over columns
for(int currentColumn = 0; currentColumn < COL_COUNT; currentColumn ++){
digitalWrite(cols[currentColumn],HIGH);
//scan rows
for(int y = 0; y < ROW_COUNT; y++){
Key* key = keyMatrix[currentColumn][y];
if(key != NOKEY){
@Crenshinibon
Crenshinibon / gist:3919048
Created October 19, 2012 16:05
Debouncing
//arbitrary number to signal that a
//key is not bouncing
const uint8_t NOT_BOUNCING = 20;
//how many ms a key is expected to bounce
const uint8_t BOUNCE_TIME = 10;
//how many bouncing keys can be handled
//in parallel
const uint8_t MAX_BOUNCING = 5;
//the index where the last bouncing key
@Crenshinibon
Crenshinibon / gist:3952167
Created October 25, 2012 11:49
HID SPP switch sketch
/*
TheTasTaTur - bt config
Author:
Dirk Porsche
Shiggy Enterprises UG (haftungsbeschränkt)
All Rights reserved
*/
@Crenshinibon
Crenshinibon / gist:3952406
Created October 25, 2012 12:57
Right hand key class and sample keys
enum keyCodes {
KC__ = 0,
KC_ErrorRollOver,
KC_POSTfail,
KC_ErrorUndefined,
// ...
KC_N,
KC_O,
KC_P,
// ...
@Crenshinibon
Crenshinibon / gist:3952501
Created October 25, 2012 13:20
On the fly key mapping
//discover correct keyCodes
static void calcKeyCodes(){
//a special case for caps lock
//switch caps lock and submit
if((modifiers[0] & M2L->keyCode) && (modifiers[0] & M2R->keyCode)){
modifiers[0] = 0x00;
keyCodes[0] = KC_CAPSLOCK;
keyCodeIndex = 1;
if(!capslock){
@Crenshinibon
Crenshinibon / gist:3955017
Created October 25, 2012 19:54
Sending the keys to the BlueSmirf
static void sendKeyState(){
if(stateChanged){
calcKeyCodes();
byte hidReport[REPORT_LENGTH] = {0xFD,0x09,0x01,modifiers[0],0x00,0x00,0x00,0x00,0x00,0x00,0x00};
for(int i = 0; i < keyCodeIndex; i++){
hidReport[5 + i] = keyCodes[i];
}
@Crenshinibon
Crenshinibon / gist:3968599
Created October 28, 2012 13:28
Receiving pull-up simple RF
byte leftSideData[5]={0};
uint8_t receivingIndex = 0;
boolean receiving = false;
const byte WAITING=0xE1,PULLUP=B10101010,PRE=0x5E,ADDR=0x10,NOTHING=0xFE,ERROR=0x77;
int receiveState = WAITING;
void loop(){
byte in = receivePackage();
if(in != NOTHING){
@Crenshinibon
Crenshinibon / gist:3968652
Created October 28, 2012 13:52
Carrier signal sending simple RF
const byte CARRIER=B10101010,PRE=0x5E,ADDR=0x10;
const byte START = 0xD0;
const byte STOP = 0xDF;
static void sendKeyState(){
if(stateChanged){
sendPackage(START);
for(int i = 0; i < pressedKeyIndex; i++){
sendPackage(pressedKeys[i]->id);
@Crenshinibon
Crenshinibon / gist:3973365
Created October 29, 2012 12:54
Carrier signal receiving simple RF
byte leftSideData[MAX_LEFTSIDE_KEYS]={0};
uint8_t receivingIndex = 0;
boolean receiving = false;
const byte CARRIER=B10101010,PRE=0x5E,ADDR=0x10,NOTHING=0xFE,ERROR=0x77;
int receiveState = CARRIER;
const byte START = 0xD0;
const byte STOP = 0xDF;