Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

be explicit about signs and sizes of integer variables

trying to standardize as well:
  signed for anything counter/range-ish
  unsigned for anything byte-field/boolean-ish
  8 bits where it will fit
  16 bits where needed
  • Loading branch information...
commit 5e7414ae2ebc74c7e794200a135025fdc14563e3 1 parent cf6fe93
@beerriot authored
View
28 include/bggame.h
@@ -7,9 +7,9 @@
typedef struct {
// size of the board
- int width, height;
+ int8_t width, height;
// number of unique piece types
- int variety;
+ int8_t variety;
// board state (with enough room for the larges board)
char board[MAX_HEIGHT][MAX_WIDTH];
// score
@@ -22,7 +22,7 @@ typedef struct {
int8_t column;
// extra data about this point
// (used for "activeness" of selection)
- int8_t meta;
+ uint8_t meta;
} point_t;
char bggame_random_piece(game_t game);
@@ -30,7 +30,7 @@ void bggame_board_init(game_t *game);
void bggame_move_cursor(game_t game,
uint8_t buttons_pushed,
point_t *cursor);
-uint8_t bggame_are_neighbor_rowcols(int rc1, int rc2, int max);
+uint8_t bggame_are_neighbor_rowcols(int8_t rc1, int8_t rc2, int8_t max);
uint8_t bggame_are_neighbors(game_t game,
point_t p1,
point_t p2);
@@ -40,10 +40,10 @@ void bggame_clear_selection(game_t *game, point_t *selection);
void bggame_set_selection(game_t *game,
point_t *selection,
point_t cursor);
-int bggame_next_row(game_t game, int r);
-int bggame_next_column(game_t game, int c);
-int bggame_match(char a, char b, char c);
-int bggame_mark_sets(game_t *game);
+int8_t bggame_next_row(game_t game, int8_t r);
+int8_t bggame_next_column(game_t game, int8_t c);
+uint8_t bggame_match(char a, char b, char c);
+uint8_t bggame_mark_sets(game_t *game);
uint8_t bggame_remove_sets(game_t *game);
void bggame_swap_pieces(game_t *game, point_t a, point_t b);
uint8_t bggame_select(game_t *game,
@@ -51,15 +51,15 @@ uint8_t bggame_select(game_t *game,
point_t cursor,
point_t *selection);
void bggame_write_board(game_t game);
-int bggame_first_space(char *row, int width);
-void bggame_shift(char *row, int width, int start);
-int bggame_fill_spaces_row(game_t game, char *row);
-int bggame_fill_spaces(game_t *game);
+int8_t bggame_first_space(char *row, int8_t width);
+void bggame_shift(char *row, int8_t width, int8_t start);
+uint8_t bggame_fill_spaces_row(game_t game, char *row);
+uint8_t bggame_fill_spaces(game_t *game);
void bggame_animate_space_fill(game_t *game);
void bggame_animate_clear_sets(game_t *game);
void bggame_clear_marks(game_t *game);
-int8_t bggame_valid_move(game_t game, point_t a, point_t b);
-int8_t bggame_valid_move_exists(game_t game);
+uint8_t bggame_valid_move(game_t game, point_t a, point_t b);
+uint8_t bggame_valid_move_exists(game_t game);
void bggame_play(game_t *game);
void bggame_over(uint16_t score);
#endif
View
8 include/bghighscore.h
@@ -18,11 +18,11 @@ uint8_t bghighscore_checksum();
uint8_t bghighscore_read();
void bghighscore_clear();
void bghighscore_write();
-void bghighscore_display_line(int rank, int lcd_line);
+void bghighscore_display_line(int8_t rank, int8_t lcd_line);
void bghighscore_screen();
-void bghighscore_alter_initials(uint8_t buttons, int rank, int i);
-int bghighscore_move_cursor(uint8_t buttons, int *i);
-void bghighscore_new(int rank, uint16_t score);
+void bghighscore_alter_initials(uint8_t buttons, int8_t rank, int8_t i);
+uint8_t bghighscore_move_cursor(uint8_t buttons, int8_t *i);
+void bghighscore_new(int8_t rank, uint16_t score);
void bghighscore_maybe(uint16_t score);
#endif
View
22 include/bgmenu.h
@@ -1,17 +1,17 @@
#ifndef __BGMENU_H__
#define __BGMENU_H__
-void bgmenu_forb(int row, uint8_t focus);
-void bgmenu_focus(int row);
-void bgmenu_blur(int row);
-void bgmenu_write_prompt(int row, int val);
-int bgmenu_previous_prompt(int current);
-int bgmenu_next_prompt(int current);
-int bgmenu_refocus(int blur, int focus);
-int bgmenu_field_and_limits(int prompt, game_t *game,
- int **field, int *min, int *max);
-void bgmenu_increase_prompt(int prompt, game_t *game);
-void bgmenu_decrease_prompt(int prompt, game_t *game);
+void bgmenu_forb(int8_t row, uint8_t focus);
+void bgmenu_focus(int8_t row);
+void bgmenu_blur(int8_t row);
+void bgmenu_write_prompt(int8_t row, int8_t val);
+int8_t bgmenu_previous_prompt(int8_t current);
+int8_t bgmenu_next_prompt(int8_t current);
+int8_t bgmenu_refocus(int8_t blur, int8_t focus);
+int8_t bgmenu_field_and_limits(int8_t prompt, game_t *game,
+ int8_t **field, int8_t *min, int8_t *max);
+void bgmenu_increase_prompt(int8_t prompt, game_t *game);
+void bgmenu_decrease_prompt(int8_t prompt, game_t *game);
uint8_t bgmenu_display(game_t *game);
#endif
View
4 include/nkeeprom.h
@@ -3,7 +3,7 @@
char nkeeprom_read_byte(uint16_t address);
void nkeeprom_write_byte(char byte, uint16_t address);
-void nkeeprom_read_bytes(unsigned char *dest, int offset, int count);
-void nkeeprom_write_bytes(unsigned char *src, int offset, int count);
+void nkeeprom_read_bytes(unsigned char *dest, uint16_t offset, int16_t count);
+void nkeeprom_write_bytes(unsigned char *src, uint16_t offset, int16_t count);
#endif
View
4 include/nktimer.h
@@ -3,10 +3,10 @@
#define F_CPU 14745600
-void nktimer_init();
+void nktimer_init(int8_t freq);
void nktimer_resume();
void nktimer_pause();
uint8_t nktimer_animate();
-void nktimer_simple_delay(int clicks);
+void nktimer_simple_delay(int16_t clicks);
#endif
View
48 src/bggame.c
@@ -25,7 +25,7 @@ char bggame_random_piece(game_t game) {
// initialize the board
void bggame_board_init(game_t *game) {
- int r,c;
+ int8_t r,c;
for (r = 0; r < game->height; r++)
for (c = 0; c < game->width; c++)
game->board[r][c] = ' ';
@@ -56,8 +56,8 @@ void bggame_move_cursor(game_t game,
}
// return true if the given columns/rows are neighbors
-uint8_t bggame_are_neighbor_rowcols(int rc1, int rc2, int max) {
- int diff = rc1 - rc2;
+uint8_t bggame_are_neighbor_rowcols(int8_t rc1, int8_t rc2, int8_t max) {
+ int8_t diff = rc1 - rc2;
return ((diff == 1) || // p1 is right-of/below p2
(-diff == 1) || // p1 is left-of/above of p2
(diff == max-1) || // p1 is far right/bottom, p2 is far left/top
@@ -103,25 +103,25 @@ void bggame_set_selection(game_t *game,
}
// return the index to the row to the "right" of the given row
-int bggame_next_row(game_t game, int r) {
+int8_t bggame_next_row(game_t game, int8_t r) {
if (++r > (game.height-1)) return 0;
return r;
}
// return the index to the column "below" the given column
-int bggame_next_column(game_t game, int c) {
+int8_t bggame_next_column(game_t game, int8_t c) {
if (++c > (game.width-1)) return 0;
return c;
}
// determine if a, b, and c are the same piece
-int bggame_match(char a, char b, char c) {
+uint8_t bggame_match(char a, char b, char c) {
return ((0x1F & b) == (0x1F & a)) && ((0x1F & b) == (0x1F & c));
}
// mark all sets on the board (as capital letters)
-int bggame_mark_sets(game_t *game) {
- int r, nr, nnr, c, nc, nnc, found=0;
+uint8_t bggame_mark_sets(game_t *game) {
+ int8_t r, nr, nnr, c, nc, nnc, found=0;
for(r=0, nr=bggame_next_row(*game, r), nnr=bggame_next_row(*game, nr);
r < game->height;
r++, nr=bggame_next_row(*game, nr), nnr=bggame_next_row(*game, nnr)) {
@@ -153,7 +153,7 @@ int bggame_mark_sets(game_t *game) {
// remove all sets on the board (as previously marked)
uint8_t bggame_remove_sets(game_t *game) {
- int r, c;
+ int8_t r, c;
uint8_t removed = 0;
for(r=0; r < game->height; r++) {
for(c=0; c < game->width; c++) {
@@ -200,7 +200,7 @@ uint8_t bggame_select(game_t *game,
}
void bggame_write_board(game_t game) {
- int r, c;
+ int8_t r, c;
for (r=0; r < game.height; r++) {
lcd_goto_position(r, 0);
for (c=0; c < game.width; c++) {
@@ -209,21 +209,21 @@ void bggame_write_board(game_t game) {
}
}
-int bggame_first_space(char *row, int width) {
- int c;
+int8_t bggame_first_space(char *row, int8_t width) {
+ int8_t c;
for (c = 0; c < width; c++)
if (row[c] == ' ')
break;
return c;
}
-void bggame_shift(char *row, int width, int start) {
+void bggame_shift(char *row, int8_t width, int8_t start) {
for (; start < (width-1); start++)
row[start] = row[start+1];
}
-int bggame_fill_spaces_row(game_t game, char *row) {
- int first_space = bggame_first_space(row, game.width);
+uint8_t bggame_fill_spaces_row(game_t game, char *row) {
+ int8_t first_space = bggame_first_space(row, game.width);
if (first_space < game.width) {
bggame_shift(row, game.width, first_space);
row[(game.width-1)] = bggame_random_piece(game);
@@ -232,7 +232,7 @@ int bggame_fill_spaces_row(game_t game, char *row) {
return 0;
}
-int bggame_fill_spaces(game_t *game) {
+uint8_t bggame_fill_spaces(game_t *game) {
int r, spaces = 0;
for(r = 0; r < game->height; r++) {
spaces |= bggame_fill_spaces_row(*game, game->board[r]);
@@ -241,7 +241,7 @@ int bggame_fill_spaces(game_t *game) {
}
void bggame_animate_space_fill(game_t *game) {
- int spaces = 1, move = 0;
+ uint8_t spaces = 1, move = 0;
while(spaces) {
if (nktimer_animate()) {
if(move > 7) {
@@ -265,14 +265,14 @@ void bggame_animate_clear_sets(game_t *game) {
}
void bggame_clear_marks(game_t *game) {
- int r, c;
+ int8_t r, c;
for (r = 0; r < game->height; r++)
for (c = 0; c < game->width; c++)
game->board[r][c] |= 0x20;
}
-int8_t bggame_valid_move(game_t game, point_t a, point_t b) {
- int8_t valid = 0;
+uint8_t bggame_valid_move(game_t game, point_t a, point_t b) {
+ uint8_t valid = 0;
bggame_swap_pieces(&game, a, b);
if (bggame_mark_sets(&game)) {
valid = 1;
@@ -282,9 +282,9 @@ int8_t bggame_valid_move(game_t game, point_t a, point_t b) {
return valid;
}
-int8_t bggame_valid_move_exists(game_t game) {
+uint8_t bggame_valid_move_exists(game_t game) {
point_t check, right, below;
- int8_t valid = 0;
+ uint8_t valid = 0;
for (check.row = 0, right.row = 0, below.row = 1;
check.row < game.height;
check.row++, right.row++,
@@ -311,7 +311,7 @@ void bggame_play(game_t *game) {
// selection state
point_t selection;
// idle/sleep timer
- uint16_t idle = 0;
+ int16_t idle = 0;
nkbuttons_clear(&button_state);
cursor.row = 0;
@@ -324,7 +324,7 @@ void bggame_play(game_t *game) {
game->score = 0; // no points for tiles removed before play starts
lcd_goto_position(cursor.row, cursor.column);
nklcd_start_blinking();
- int8_t move_exists = bggame_valid_move_exists(*game);
+ uint8_t move_exists = bggame_valid_move_exists(*game);
// now let play begin
while(move_exists) {
if (nktimer_animate()) {
View
24 src/bghighscore.c
@@ -54,7 +54,7 @@ uint8_t bghighscore_read() {
}
void bghighscore_clear() {
- uint8_t s, c;
+ int8_t s, c;
for (s = 0; s < HIGH_SCORES; s++) {
for (c = 0; c < INITIALS; c++)
highscores[s].initials[c] = 'a';
@@ -63,9 +63,8 @@ void bghighscore_clear() {
}
void bghighscore_write() {
- uint8_t x;
+ uint8_t x = bghighscore_checksum();
cli(); //disable interrupts
- x = bghighscore_checksum();
nkeeprom_write_bytes((unsigned char *)&highscores,
0,
HIGH_SCORES*sizeof(bghighscore_t));
@@ -75,8 +74,8 @@ void bghighscore_write() {
sei();
}
-void bghighscore_display_line(int rank, int lcd_line) {
- int c;
+void bghighscore_display_line(int8_t rank, int8_t lcd_line) {
+ int8_t c;
lcd_goto_position(lcd_line, 6);
for (c = 0; c < INITIALS; c++)
lcd_write_data(highscores[rank].initials[c]);
@@ -85,7 +84,7 @@ void bghighscore_display_line(int rank, int lcd_line) {
}
void bghighscore_screen() {
- int s;
+ int8_t s;
// game is over (no more moves)
lcd_clear_and_home();
nklcd_stop_blinking();
@@ -98,7 +97,7 @@ void bghighscore_screen() {
nktimer_simple_delay(300);
}
-void alter_highscore_initials(uint8_t buttons, int rank, int i) {
+void alter_highscore_initials(uint8_t buttons, int8_t rank, int8_t i) {
if (buttons & B_UP) {
if (highscores[rank].initials[i] < 'z')
highscores[rank].initials[i]++;
@@ -112,7 +111,7 @@ void alter_highscore_initials(uint8_t buttons, int rank, int i) {
}
}
-int bghighscore_move_cursor(uint8_t buttons, int *i) {
+uint8_t bghighscore_move_cursor(uint8_t buttons, int8_t *i) {
if (buttons & B_LEFT) {
if (*i > 0)
*i -= 1;
@@ -129,13 +128,13 @@ int bghighscore_move_cursor(uint8_t buttons, int *i) {
return 0;
}
-void bghighscore_new(int rank, uint16_t score) {
- int i=0, c;
+void bghighscore_new(int8_t rank, uint16_t score) {
+ int8_t i;
nkbuttons_t button_state;
uint8_t pressed_buttons;
nkbuttons_clear(&button_state);
- for (c = 0; c < INITIALS; c++)
- highscores[rank].initials[c] = 'a';
+ for (i = 0; i < INITIALS; i++)
+ highscores[rank].initials[i] = 'a';
highscores[rank].score = score;
lcd_clear_and_home();
@@ -144,6 +143,7 @@ void bghighscore_new(int rank, uint16_t score) {
bghighscore_display_line(rank, 2);
lcd_goto_position(2, 6);
nklcd_start_blinking();
+ i = 0;
while(1) {
if (nktimer_animate()) {
View
30 src/bgmenu.c
@@ -24,42 +24,42 @@
#define P_START 3
#define P_LAST 3
-void bgmenu_forb(int row, uint8_t focus) {
+void bgmenu_forb(int8_t row, uint8_t focus) {
lcd_goto_position(row, 10);
lcd_write_data(focus ? 0x7e : ' ');
lcd_goto_position(row, 16);
lcd_write_data(focus ? 0x7f : ' ');
}
-void bgmenu_focus(int row) {
+void bgmenu_focus(int8_t row) {
bgmenu_forb(row, 1);
}
-void bgmenu_blur(int row) {
+void bgmenu_blur(int8_t row) {
bgmenu_forb(row, 0);
}
-void bgmenu_write_prompt(int row, int val) {
+void bgmenu_write_prompt(int8_t row, int8_t val) {
lcd_goto_position(row, 12);
if (val < 10)
lcd_write_data(' ');
lcd_write_int16(val);
}
-int bgmenu_previous_prompt(int current) {
+int8_t bgmenu_previous_prompt(int8_t current) {
return bgmenu_refocus(current, (current == 0) ? P_LAST : current-1);
}
-int bgmenu_next_prompt(int current) {
+int8_t bgmenu_next_prompt(int8_t current) {
return bgmenu_refocus(current, (current == P_LAST) ? 0 : current+1);
}
-int bgmenu_refocus(int blur, int focus) {
+int8_t bgmenu_refocus(int8_t blur, int8_t focus) {
bgmenu_blur(blur);
bgmenu_focus(focus);
return focus;
}
-int bgmenu_field_and_limits(int prompt, game_t *game,
- int **field, int *min, int *max) {
+int8_t bgmenu_field_and_limits(int8_t prompt, game_t *game,
+ int8_t **field, int8_t *min, int8_t *max) {
switch (prompt) {
case P_WIDTH:
*field = &game->width;
@@ -81,8 +81,8 @@ int bgmenu_field_and_limits(int prompt, game_t *game,
}
}
-void bgmenu_increase_prompt(int prompt, game_t *game) {
- int *field, min, max;
+void bgmenu_increase_prompt(int8_t prompt, game_t *game) {
+ int8_t *field, min, max;
if (bgmenu_field_and_limits(prompt, game, &field, &min, &max)) {
if (*field < max)
*field = *field+1;
@@ -90,8 +90,8 @@ void bgmenu_increase_prompt(int prompt, game_t *game) {
}
}
-void bgmenu_decrease_prompt(int prompt, game_t *game) {
- int *field, min, max;
+void bgmenu_decrease_prompt(int8_t prompt, game_t *game) {
+ int8_t *field, min, max;
if (bgmenu_field_and_limits(prompt, game, &field, &min, &max)) {
if (*field > min)
*field = *field-1;
@@ -100,7 +100,9 @@ void bgmenu_decrease_prompt(int prompt, game_t *game) {
}
uint8_t bgmenu_display(game_t *game) {
- int ready = 0, prompt = 0, inactivity = 0;
+ uint8_t ready = 0;
+ int8_t prompt = 0;
+ int16_t inactivity = 0;
uint8_t pressed_buttons;
nkbuttons_t button_state;
nkbuttons_clear(&button_state);
View
2  src/blockgame.c
@@ -24,7 +24,7 @@
int main() {
game_t game;
// idle/sleep timer
- uint8_t idle = 0;
+ int8_t idle = 0;
// the playing board
game.width = MAX_WIDTH;
View
4 src/nkeeprom.c
@@ -27,12 +27,12 @@ void nkeeprom_write_byte(char byte, uint16_t address) {
EECR |= (1<<EEPE); //start write
}
-void nkeeprom_read_bytes(unsigned char *dest, int offset, int count) {
+void nkeeprom_read_bytes(unsigned char *dest, uint16_t offset, int16_t count) {
for (; count > 0; count--, dest++, offset++)
*dest = nkeeprom_read_byte(offset);
}
-void nkeeprom_write_bytes(unsigned char *src, int offset, int count) {
+void nkeeprom_write_bytes(unsigned char *src, uint16_t offset, int16_t count) {
for(; count > 0; count--, src++, offset++)
nkeeprom_write_byte(*src, offset);
}
View
4 src/nktimer.c
@@ -21,7 +21,7 @@ ISR(TIMER0_COMPA_vect) {
}
// configure the animation timer at boot
-void nktimer_init(int freq) {
+void nktimer_init(int8_t freq) {
// Clear Timer on Compare Match of OCRA
TCCR0A |= (1<<WGM01);
@@ -56,7 +56,7 @@ uint8_t nktimer_animate() {
return 0;
}
-void nktimer_simple_delay(int clicks) {
+void nktimer_simple_delay(int16_t clicks) {
nkbuttons_t button_state;
nkbuttons_clear(&button_state);
Please sign in to comment.
Something went wrong with that request. Please try again.