![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Adnan Mehanovic Faruk Mustafic
main.cpp@0:2144396e0e9b, 2014-03-17 (annotated)
- Committer:
- tim008
- Date:
- Mon Mar 17 10:50:49 2014 +0000
- Revision:
- 0:2144396e0e9b
PAI_grupa2_tim008_lab3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tim008 | 0:2144396e0e9b | 1 | #include "mbed.h" |
tim008 | 0:2144396e0e9b | 2 | |
tim008 | 0:2144396e0e9b | 3 | #include <cmath> |
tim008 | 0:2144396e0e9b | 4 | |
tim008 | 0:2144396e0e9b | 5 | #define EYE_WAIT_TIME 10 // microseconds |
tim008 | 0:2144396e0e9b | 6 | #define DEBOUNCE_TIME 100 |
tim008 | 0:2144396e0e9b | 7 | |
tim008 | 0:2144396e0e9b | 8 | #define HIGH 1 // xD |
tim008 | 0:2144396e0e9b | 9 | #define LOW 0 |
tim008 | 0:2144396e0e9b | 10 | |
tim008 | 0:2144396e0e9b | 11 | int counter = 0; |
tim008 | 0:2144396e0e9b | 12 | |
tim008 | 0:2144396e0e9b | 13 | BusOut segments(dp2, dp1, dp28, dp6, dp5, dp27, dp26); |
tim008 | 0:2144396e0e9b | 14 | BusOut digit(dp23, dp24, dp25); |
tim008 | 0:2144396e0e9b | 15 | DigitalOut decimal_point(dp4); |
tim008 | 0:2144396e0e9b | 16 | |
tim008 | 0:2144396e0e9b | 17 | BusIn columns(dp9,dp10,dp11,dp13); |
tim008 | 0:2144396e0e9b | 18 | BusOut rows(dp16, dp15, dp17, dp18); |
tim008 | 0:2144396e0e9b | 19 | |
tim008 | 0:2144396e0e9b | 20 | int digit_buffer[3] = {1, 2, 3}; |
tim008 | 0:2144396e0e9b | 21 | unsigned int predefined_segments[19] = {0x01, 0x4f, 0x12, 0x06, 0x4c, |
tim008 | 0:2144396e0e9b | 22 | 0x24, 0x20, 0x0f, 0x00, 0x04, |
tim008 | 0:2144396e0e9b | 23 | 0x08,0x60,0x31,0x42,0x30,0x38, |
tim008 | 0:2144396e0e9b | 24 | ~(0x01), 0x36, ~0}; |
tim008 | 0:2144396e0e9b | 25 | |
tim008 | 0:2144396e0e9b | 26 | int pressed_button() { |
tim008 | 0:2144396e0e9b | 27 | |
tim008 | 0:2144396e0e9b | 28 | if(counter > 5) { |
tim008 | 0:2144396e0e9b | 29 | counter = 0; |
tim008 | 0:2144396e0e9b | 30 | } else { |
tim008 | 0:2144396e0e9b | 31 | counter++; |
tim008 | 0:2144396e0e9b | 32 | return -1; |
tim008 | 0:2144396e0e9b | 33 | } |
tim008 | 0:2144396e0e9b | 34 | |
tim008 | 0:2144396e0e9b | 35 | int lookup[] = {1, 2, 3, 10, 4, 5, 6, 11, |
tim008 | 0:2144396e0e9b | 36 | 7, 8, 9, 12, 16, 0, 17, 13}; |
tim008 | 0:2144396e0e9b | 37 | |
tim008 | 0:2144396e0e9b | 38 | for(int i = 0; i <= 3; i++) { |
tim008 | 0:2144396e0e9b | 39 | rows = (1 << i); // probe a row |
tim008 | 0:2144396e0e9b | 40 | for(int j = 0; j <= 3; j++) |
tim008 | 0:2144396e0e9b | 41 | if(columns == (1 << j)) |
tim008 | 0:2144396e0e9b | 42 | return lookup[ i * 4 + j ]; |
tim008 | 0:2144396e0e9b | 43 | } |
tim008 | 0:2144396e0e9b | 44 | |
tim008 | 0:2144396e0e9b | 45 | return -1; |
tim008 | 0:2144396e0e9b | 46 | } |
tim008 | 0:2144396e0e9b | 47 | |
tim008 | 0:2144396e0e9b | 48 | void display_digit_buffer() { |
tim008 | 0:2144396e0e9b | 49 | if(digit_buffer[2] == 12) { |
tim008 | 0:2144396e0e9b | 50 | digit_buffer[0] = 18; |
tim008 | 0:2144396e0e9b | 51 | digit_buffer[1] = 18; |
tim008 | 0:2144396e0e9b | 52 | digit_buffer[2] = 18; |
tim008 | 0:2144396e0e9b | 53 | } |
tim008 | 0:2144396e0e9b | 54 | for(int i = 1; i <= 4; i = i * 2) { // ne moze *= |
tim008 | 0:2144396e0e9b | 55 | digit = ~i; |
tim008 | 0:2144396e0e9b | 56 | segments = predefined_segments[ digit_buffer[i / 2] ]; |
tim008 | 0:2144396e0e9b | 57 | decimal_point = 1; |
tim008 | 0:2144396e0e9b | 58 | wait_ms(EYE_WAIT_TIME); |
tim008 | 0:2144396e0e9b | 59 | } |
tim008 | 0:2144396e0e9b | 60 | } |
tim008 | 0:2144396e0e9b | 61 | |
tim008 | 0:2144396e0e9b | 62 | void buffer(int button) { |
tim008 | 0:2144396e0e9b | 63 | if(button == -1) return; |
tim008 | 0:2144396e0e9b | 64 | |
tim008 | 0:2144396e0e9b | 65 | for(int i = 0; i <= 1; i++) |
tim008 | 0:2144396e0e9b | 66 | digit_buffer[i] = digit_buffer[i + 1]; |
tim008 | 0:2144396e0e9b | 67 | |
tim008 | 0:2144396e0e9b | 68 | digit_buffer[2] = button; |
tim008 | 0:2144396e0e9b | 69 | } |
tim008 | 0:2144396e0e9b | 70 | |
tim008 | 0:2144396e0e9b | 71 | int main() { |
tim008 | 0:2144396e0e9b | 72 | |
tim008 | 0:2144396e0e9b | 73 | while(true) { |
tim008 | 0:2144396e0e9b | 74 | buffer(pressed_button()); |
tim008 | 0:2144396e0e9b | 75 | display_digit_buffer(); |
tim008 | 0:2144396e0e9b | 76 | //wait_ms(50); |
tim008 | 0:2144396e0e9b | 77 | } |
tim008 | 0:2144396e0e9b | 78 | |
tim008 | 0:2144396e0e9b | 79 | return 0; |
tim008 | 0:2144396e0e9b | 80 | } |