writing speed is detected, and the model mayhem sets backlight according to
speed
This commit is contained in:
		| @@ -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 |  * Main function, containing the main loop that manages timer- and | ||||||
|  * USB-functionality. |  * USB-functionality. | ||||||
| @@ -531,6 +574,7 @@ void sendString(char* string) { | |||||||
| int main(void) { | int main(void) { | ||||||
|     uint8_t updateNeeded = 0; |     uint8_t updateNeeded = 0; | ||||||
|     uint8_t idleCounter = 0; |     uint8_t idleCounter = 0; | ||||||
|  |     uint8_t updates = 0; | ||||||
|     wdt_enable(WDTO_2S); |     wdt_enable(WDTO_2S); | ||||||
|     hardwareInit(); |     hardwareInit(); | ||||||
|     usbInit(); |     usbInit(); | ||||||
| @@ -545,10 +589,15 @@ int main(void) { | |||||||
|         usbPoll(); |         usbPoll(); | ||||||
|  |  | ||||||
|         updateNeeded = scankeys(reportBuffer, oldReportBuffer, sizeof(reportBuffer)); // changes? |         updateNeeded = scankeys(reportBuffer, oldReportBuffer, sizeof(reportBuffer)); // changes? | ||||||
|  |         if (updateNeeded) { | ||||||
|  |             updates++; | ||||||
|  |         } | ||||||
|  |  | ||||||
|         // check timer if we need periodic reports |         // check timer if we need periodic reports | ||||||
|         if (TIFR & (1 << TOV0)) { |         if (TIFR & (1 << TOV0)) { | ||||||
|             TIFR = (1 << TOV0); // reset flag |             TIFR = (1 << TOV0); // reset flag | ||||||
|  |             calculateSpeed(updates); | ||||||
|  |             updates = 0; | ||||||
|             if (idleRate != 0) { // do we need periodic reports? |             if (idleRate != 0) { // do we need periodic reports? | ||||||
|                 if(idleCounter > 4){ // yes, but not yet |                 if(idleCounter > 4){ // yes, but not yet | ||||||
|                     idleCounter -= 5; // 22ms in units of 4ms |                     idleCounter -= 5; // 22ms in units of 4ms | ||||||
|   | |||||||
| @@ -106,6 +106,10 @@ void printMatrix(void) { | |||||||
|     sendString("---"); |     sendString("---"); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void setSpeed(uint8_t speed) { | ||||||
|  |     // not used in this model/version | ||||||
|  | } | ||||||
|  |  | ||||||
| void setLeds(uint8_t LEDstate) { | void setLeds(uint8_t LEDstate) { | ||||||
|     if (LEDstate & LED_NUM) { // light up num lock |     if (LEDstate & LED_NUM) { // light up num lock | ||||||
|         PORTLEDS &= ~(1 << LEDNUM); |         PORTLEDS &= ~(1 << LEDNUM); | ||||||
|   | |||||||
| @@ -21,6 +21,13 @@ void hardwareInit(void); | |||||||
|  */ |  */ | ||||||
| void printMatrix(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. |  * This function sets the LEDs according to the given data. | ||||||
|  * \param LEDstate bitfield with LED info |  * \param LEDstate bitfield with LED info | ||||||
|   | |||||||
| @@ -70,6 +70,21 @@ void hardwareInit(void) { | |||||||
|     PORTLEDS &= ~((1 << LEDNUM) | (1 << LEDCAPS) | (1 << LEDSCROLL) | (1 << LEDCOMP)); |     PORTLEDS &= ~((1 << LEDNUM) | (1 << LEDCAPS) | (1 << LEDSCROLL) | (1 << LEDCOMP)); | ||||||
|     _delay_ms(50); |     _delay_ms(50); | ||||||
|     PORTLEDS |= ((1 << LEDNUM) | (1 << LEDCAPS) | (1 << LEDSCROLL) | (1 << LEDCOMP)); |     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("---"); |     sendString("---"); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void setSpeed(uint8_t speed) { | ||||||
|  |     OCR1A = 0xffff - (speed * speed); | ||||||
|  |     OCR1B = (speed * speed); | ||||||
|  | } | ||||||
|  |  | ||||||
| void setLeds(uint8_t LEDstate) { | void setLeds(uint8_t LEDstate) { | ||||||
|     if (LEDstate & LED_NUM) { // light up num lock |     if (LEDstate & LED_NUM) { // light up num lock | ||||||
|         PORTLEDS &= ~(1 << LEDNUM); |         PORTLEDS &= ~(1 << LEDNUM); | ||||||
|   | |||||||
| @@ -93,6 +93,10 @@ void printMatrix(void) { | |||||||
|     sendString("\n"); |     sendString("\n"); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void setSpeed(uint8_t speed) { | ||||||
|  |     // not used in this model/version | ||||||
|  | } | ||||||
|  |  | ||||||
| void setLeds(uint8_t LEDstate) { | void setLeds(uint8_t LEDstate) { | ||||||
|     if (LEDstate & LED_NUM) { // light up num lock |     if (LEDstate & LED_NUM) { // light up num lock | ||||||
|         PORTLEDS &= ~(1 << LEDNUM); |         PORTLEDS &= ~(1 << LEDNUM); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user