aboutsummaryrefslogtreecommitdiff
path: root/software/scan.ino
blob: 87115021b6c6257911d6798c32aa9cec2eef46f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "scan.h"
#include "shift.h"
#include "software.h"

// 0 = horizontal (top-bottom), 1 = vertical (left-right)
unsigned char scan_direction = 0;
unsigned char scan_index = 0;
unsigned char scan_order[8] = {1, 1, 1, 1, 1, 1, 1, 1};

unsigned char get_state_row(unsigned char row, unsigned char direction) {
	unsigned char return_value = 0;

	for (int i = 0; i < 8; i++)
		return_value = return_value | ( led_state[
			direction == SCAN_HOR ? (i + row * 8) : (row + i * 8) ] << (7 - i));

	return return_value;
}

void scan() {
	shift_state[0] = 0xff ^ (1 << scan_index);
	shift_state[1] = get_state_row(scan_index, scan_direction);

	update_shift_state();

	// go to next row/column
	for(int i = scan_index + 1; i < (scan_index + 7); i++) {
		if (scan_order[i % 8] == 0) continue;
		scan_index = i % 8;
		break;
	}
}
void optimize_scan() {
	unsigned char optimal_direction,
								hv_emptyc[2] = {0},
								hv_empty[2][8] = {0};

	// calculate empty rows/columns
	for(unsigned char direction = 0; direction < 2; direction++) {
		for(unsigned char row; row < 8; row++) {
			if (get_state_row(row, direction) == 0) {
				hv_empty[direction][row] = 1;
				hv_emptyc[direction]++;
			}
		}
	}

	optimal_direction = hv_emptyc[0] > hv_emptyc[1] ? 0 : 1;
	scan_direction = optimal_direction;
	memcpy(&scan_order, &hv_emptyc[optimal_direction], sizeof(scan_order));

	return;
}