diff --git a/firmware/main.c b/firmware/main.c index 186842e..31f614d 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -523,6 +523,49 @@ void sendString(char* string) { } } +/** + * Calculate an average value for the speed the keys are pressed in. The speed + * value is sent to the keyboard interface, the keyboard decides what to do + * with it. + * \param updates number of updates since the last call (should be in the range + * 0-1) + */ +void calculateSpeed(uint8_t updates) { + static uint8_t callcounter = 0; + static uint8_t speed = 0; + callcounter++; + if (updates > 0) { + // a key has been pressed + if (speed < 251) { + // it can get hotter... + if (callcounter < 8) { + speed += 4; + } else if (callcounter < 16) { + speed += 3; + } else if (callcounter < 24) { + speed += 2; + } else { + speed += 1; + } + setSpeed(speed); + } + callcounter = 0; + } else { + // no key pressed... + if (callcounter == 16) { + // ... for a long time, decrease speed value + if (speed > 10) { + speed -= 10; + setSpeed(speed); + } else if (speed > 0) { + speed = 0; + setSpeed(speed); + } + callcounter = 0; + } + } +} + /** * Main function, containing the main loop that manages timer- and * USB-functionality. @@ -531,6 +574,7 @@ void sendString(char* string) { int main(void) { uint8_t updateNeeded = 0; uint8_t idleCounter = 0; + uint8_t updates = 0; wdt_enable(WDTO_2S); hardwareInit(); usbInit(); @@ -545,10 +589,15 @@ int main(void) { usbPoll(); updateNeeded = scankeys(reportBuffer, oldReportBuffer, sizeof(reportBuffer)); // changes? + if (updateNeeded) { + updates++; + } // check timer if we need periodic reports if (TIFR & (1 << TOV0)) { TIFR = (1 << TOV0); // reset flag + calculateSpeed(updates); + updates = 0; if (idleRate != 0) { // do we need periodic reports? if(idleCounter > 4){ // yes, but not yet idleCounter -= 5; // 22ms in units of 4ms diff --git a/firmware/modelibmmodelm.c b/firmware/modelibmmodelm.c index 6d7fa5f..60d4a54 100644 --- a/firmware/modelibmmodelm.c +++ b/firmware/modelibmmodelm.c @@ -106,6 +106,10 @@ void printMatrix(void) { sendString("---"); } +void setSpeed(uint8_t speed) { + // not used in this model/version +} + void setLeds(uint8_t LEDstate) { if (LEDstate & LED_NUM) { // light up num lock PORTLEDS &= ~(1 << LEDNUM); diff --git a/firmware/modelinterface.h b/firmware/modelinterface.h index 54d5787..d4c24f5 100644 --- a/firmware/modelinterface.h +++ b/firmware/modelinterface.h @@ -21,6 +21,13 @@ void hardwareInit(void); */ void printMatrix(void); +/** + * This tells the current writing speed to the keyboard, for whatever it will + * do with the value. + * \param speed speed value between 0 and 255 + */ +void setSpeed(uint8_t speed); + /** * This function sets the LEDs according to the given data. * \param LEDstate bitfield with LED info diff --git a/firmware/modelmayhem.c b/firmware/modelmayhem.c index fe6d16c..93d9beb 100644 --- a/firmware/modelmayhem.c +++ b/firmware/modelmayhem.c @@ -70,6 +70,21 @@ void hardwareInit(void) { PORTLEDS &= ~((1 << LEDNUM) | (1 << LEDCAPS) | (1 << LEDSCROLL) | (1 << LEDCOMP)); _delay_ms(50); PORTLEDS |= ((1 << LEDNUM) | (1 << LEDCAPS) | (1 << LEDSCROLL) | (1 << LEDCOMP)); + + // initialize timer for pulse width modulation on backlight LED ports + // WGM13=1, WGM12=1, WGM11=1, WGM10=0 -> PWM mode 14 + // COM1A1=1, COM1B1=1 -> OC1A and OC1B are set at the bottom and cleared on + // when the counter matches OCR1A or OCR1B + // CS10=1 -> prescaler 1 + TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11); + TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS10); + + // the pwm counter counts from 0 to this value + ICR1 = 0xffff; + + // both LEDs are switched off on startup + OCR1A = 0xffff; // red + OCR1B = 0xffff; // green } /** @@ -101,6 +116,11 @@ void printMatrix(void) { sendString("---"); } +void setSpeed(uint8_t speed) { + OCR1A = 0xffff - (speed * speed); + OCR1B = (speed * speed); +} + void setLeds(uint8_t LEDstate) { if (LEDstate & LED_NUM) { // light up num lock PORTLEDS &= ~(1 << LEDNUM); diff --git a/firmware/modelsuntype5.c b/firmware/modelsuntype5.c index c0d4021..0e710f2 100644 --- a/firmware/modelsuntype5.c +++ b/firmware/modelsuntype5.c @@ -93,6 +93,10 @@ void printMatrix(void) { sendString("\n"); } +void setSpeed(uint8_t speed) { + // not used in this model/version +} + void setLeds(uint8_t LEDstate) { if (LEDstate & LED_NUM) { // light up num lock PORTLEDS &= ~(1 << LEDNUM);