fixed a bug... :/

in the while loop in main() that busywaits until we can scan again, the
compiler was optimizing out the function call, it seems like, when i
wrote `(uint8_t)timer__get_milliseconds()`; if i cast the whole
expression (not just the function) to `(volatile uint8_t)`, or if i just
didn't cast anything at all, it worked.  not sure why the compiler would
optimize the function call out like that though, even if it was cast...
this happened when i put it in a for loop too.  i need to research it
just a little more, and write a warning about it in the timer
documentation.
partial-rewrite
Ben Blazak 2013-05-07 03:01:21 -07:00
parent 9724cf9331
commit f031c99c1d
4 changed files with 25 additions and 12 deletions

View File

@ -73,17 +73,17 @@ bool kb__led__read(uint8_t led) {
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void kb__led__all_on(void) {
for(int8_t i=1; i<=3; i++)
for(uint8_t i=1; i<=3; i++)
kb__led__on(i);
}
void kb__led__all_off(void) {
for(int8_t i=1; i<=3; i++)
for(uint8_t i=1; i<=3; i++)
kb__led__off(i);
}
void kb__led__all_set(float n) {
for(int8_t i=1; i<=3; i++)
for(uint8_t i=1; i<=3; i++)
kb__led__set(i, n);
}

View File

@ -8,8 +8,6 @@
* Timer interface
*
* Prefix: `timer__`
*
* `timer__init()` is meant to be called once, on startup, by `main()`
*/
@ -19,9 +17,16 @@
// ----------------------------------------------------------------------------
void timer__init(void);
uint8_t timer__init(void);
/** functions/timer__init/description
* Initialize the timer
*
* Returns:
* - success: `0`
* - failure: [other]
*
* Notes:
* - Should be called exactly once by `main()` before entering the run loop.
*/
uint32_t timer__get_milliseconds(void);

View File

@ -22,11 +22,13 @@ static volatile uint32_t _milliseconds;
// ----------------------------------------------------------------------------
void timer__init(void) {
uint8_t timer__init(void) {
TCCR0A = 0b10000010;
TCCR0B = 0b00000011;
TIMSK0 = 0b00000010;
OCR0A = 250;
return 0; // success
}
uint32_t timer__get_milliseconds(void) {

View File

@ -11,7 +11,6 @@
#include <stdbool.h>
#include <stdint.h>
#include <util/delay.h>
#include "../firmware/keyboard.h"
#include "../firmware/lib/timer.h"
#include "../firmware/lib/usb.h"
@ -68,7 +67,7 @@ int main(void) {
static uint8_t time_scan_started;
kb__init(); // initialize hardware (besides USB and timer)
kb__init(); // initialize hardware (besides USB and timer)
kb__led__state__power_on();
@ -80,9 +79,16 @@ int main(void) {
kb__led__state__ready();
time_scan_started // first iteration, scan immediately
= (uint8_t)timer__get_milliseconds() - OPT__DEBOUNCE_TIME;
time_scan_started // on the first iteration, scan immediately
= timer__get_milliseconds() - OPT__DEBOUNCE_TIME;
// TODO
// - test the scan rate, to make sure it's what we want
// - write a nice note about not casting `timer__get_milliseconds()` inside
// while loops... or maybe write a `timer__diff_with_now()` or something
// function
// - look at the generated asm for these things, maybe, to see what's going
// on
for(;;) {
temp = is_pressed;
is_pressed = was_pressed;
@ -91,7 +97,7 @@ int main(void) {
// delay if necessary, then rescan
// - add 1 to `OPT__DEBOUNCE_TIME` in case `time_scan_started` caught
// the tail end of the millisecond it recorded
while( (uint8_t)timer__get_milliseconds() - time_scan_started
while( timer__get_milliseconds() - time_scan_started
< OPT__DEBOUNCE_TIME + 1 );
time_scan_started = timer__get_milliseconds();
kb__update_matrix(*is_pressed);