fgjfjfdjdjdjd
Dependencies: mbed
Fork of EDP2_display by
main.cpp@4:82c807b7685e, 2018-03-06 (annotated)
- Committer:
- raduschirila
- Date:
- Tue Mar 06 16:19:18 2018 +0000
- Revision:
- 4:82c807b7685e
- Parent:
- 3:5f89484f8c84
- Child:
- 5:4152a51f0724
gdgdgfd;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
raduschirila | 0:850d65a7d593 | 1 | #include "mbed.h" |
raduschirila | 0:850d65a7d593 | 2 | |
raduschirila | 0:850d65a7d593 | 3 | |
raduschirila | 0:850d65a7d593 | 4 | #define max7219_reg_noop 0x00 |
raduschirila | 0:850d65a7d593 | 5 | #define max7219_reg_digit0 0x01 |
raduschirila | 0:850d65a7d593 | 6 | #define max7219_reg_digit1 0x02 |
raduschirila | 0:850d65a7d593 | 7 | #define max7219_reg_digit2 0x03 |
raduschirila | 0:850d65a7d593 | 8 | #define max7219_reg_digit3 0x04 |
raduschirila | 0:850d65a7d593 | 9 | #define max7219_reg_digit4 0x05 |
raduschirila | 0:850d65a7d593 | 10 | #define max7219_reg_digit5 0x06 |
raduschirila | 0:850d65a7d593 | 11 | #define max7219_reg_digit6 0x07 |
raduschirila | 0:850d65a7d593 | 12 | #define max7219_reg_digit7 0x08 |
raduschirila | 0:850d65a7d593 | 13 | #define max7219_reg_decodeMode 0x09 |
raduschirila | 0:850d65a7d593 | 14 | #define max7219_reg_intensity 0x0a |
raduschirila | 0:850d65a7d593 | 15 | #define max7219_reg_scanLimit 0x0b |
raduschirila | 0:850d65a7d593 | 16 | #define max7219_reg_shutdown 0x0c |
raduschirila | 0:850d65a7d593 | 17 | #define max7219_reg_displayTest 0x0f |
raduschirila | 0:850d65a7d593 | 18 | |
raduschirila | 0:850d65a7d593 | 19 | |
raduschirila | 0:850d65a7d593 | 20 | |
raduschirila | 0:850d65a7d593 | 21 | |
raduschirila | 2:281d8c268a8e | 22 | Serial pc(USBTX, USBRX); // tx, rx this is for the data to be sent via the usb port to the computer |
raduschirila | 4:82c807b7685e | 23 | Ticker pulse,freq,display_rate; |
raduschirila | 0:850d65a7d593 | 24 | AnalogIn pulse_in(PTB0); |
raduschirila | 0:850d65a7d593 | 25 | AnalogOut o(PTE30); |
raduschirila | 2:281d8c268a8e | 26 | DigitalOut led(PTD5); |
raduschirila | 4:82c807b7685e | 27 | float xi,ypast,y,alpha=0.5,sum=0; |
raduschirila | 4:82c807b7685e | 28 | float v[80]; |
raduschirila | 4:82c807b7685e | 29 | int q=0; |
raduschirila | 2:281d8c268a8e | 30 | bool first=true; |
raduschirila | 3:5f89484f8c84 | 31 | |
raduschirila | 4:82c807b7685e | 32 | |
raduschirila | 4:82c807b7685e | 33 | |
raduschirila | 0:850d65a7d593 | 34 | void get_pulse() |
raduschirila | 0:850d65a7d593 | 35 | { |
raduschirila | 4:82c807b7685e | 36 | xi=(float)pulse_in; |
raduschirila | 2:281d8c268a8e | 37 | //noise reduction algorithm |
raduschirila | 4:82c807b7685e | 38 | if(!first) { //noise filtering procedure |
raduschirila | 3:5f89484f8c84 | 39 | y= (alpha*xi)+ ((1-alpha)*ypast); |
raduschirila | 4:82c807b7685e | 40 | ypast=y; |
raduschirila | 4:82c807b7685e | 41 | sum+=y; |
raduschirila | 4:82c807b7685e | 42 | q++; |
raduschirila | 4:82c807b7685e | 43 | } else |
raduschirila | 2:281d8c268a8e | 44 | first=false; |
raduschirila | 4:82c807b7685e | 45 | //normalization procedure |
raduschirila | 4:82c807b7685e | 46 | v[q-1]=y-(sum/(float)q)+0.1;//normalization procedure raw pulse value minus the trendline(running average) |
raduschirila | 4:82c807b7685e | 47 | o=v[q-1]; |
raduschirila | 4:82c807b7685e | 48 | if(q==80) { |
raduschirila | 4:82c807b7685e | 49 | q=0; |
raduschirila | 4:82c807b7685e | 50 | sum=sum/80; |
raduschirila | 4:82c807b7685e | 51 | } |
raduschirila | 4:82c807b7685e | 52 | |
raduschirila | 0:850d65a7d593 | 53 | } |
raduschirila | 0:850d65a7d593 | 54 | |
raduschirila | 0:850d65a7d593 | 55 | |
raduschirila | 0:850d65a7d593 | 56 | |
raduschirila | 0:850d65a7d593 | 57 | |
raduschirila | 0:850d65a7d593 | 58 | #define LOW 0 |
raduschirila | 0:850d65a7d593 | 59 | #define HIGH 1 |
raduschirila | 0:850d65a7d593 | 60 | |
raduschirila | 0:850d65a7d593 | 61 | SPI max72_spi(PTD2, NC, PTD1); |
raduschirila | 0:850d65a7d593 | 62 | DigitalOut load(PTD0); //will provide the load signal |
raduschirila | 0:850d65a7d593 | 63 | |
raduschirila | 4:82c807b7685e | 64 | //patterns declarations do not touch |
raduschirila | 4:82c807b7685e | 65 | char heart[8] = {0x00,0x36,0x7f,0x7f,0x3e,0x1c,0x08,0x00}; |
raduschirila | 4:82c807b7685e | 66 | char five[8]= {0x7c,0x40,0x40,0x78,0x04,0x04,0x04,0x78}; |
raduschirila | 4:82c807b7685e | 67 | char six[8]= {0x00,0x3c,0x20,0x20,0x3e,0x22,0x22,0x3e}; |
raduschirila | 4:82c807b7685e | 68 | char seven[8]= {0x7c,0x04,0x04,0x08,0x08,0x08,0x08,0x08}; |
raduschirila | 4:82c807b7685e | 69 | char eight[8]= {0x7e,0x42,0x42,0x42,0x7e,0x42,0x42,0x7e}; |
raduschirila | 4:82c807b7685e | 70 | char nine[8]= {0x3e,0x22,0x22,0x3e,0x02,0x02,0x02,0x1e}; |
raduschirila | 4:82c807b7685e | 71 | char ten[8]= {0x4f,0x49,0x49,0x49,0x49,0x49,0x49,0x4f}; |
raduschirila | 4:82c807b7685e | 72 | char eleven[8]= {0x48,0x48,0x48,0x48,0x48,0x48,0x48,0x48}; |
raduschirila | 4:82c807b7685e | 73 | char twlv[8]= {0x4f,0x41,0x41,0x41,0x4f,0x48,0x48,0x4f}; |
raduschirila | 4:82c807b7685e | 74 | char thirtn[8]= {0x4f,0x41,0x41,0x4f,0x41,0x41,0x41,0x4f}; |
raduschirila | 4:82c807b7685e | 75 | char fourtn[8]= {0x49,0x49,0x49,0x4f,0x41,0x41,0x41,0x41}; |
raduschirila | 4:82c807b7685e | 76 | char fiftn[8]= {0x4f,0x48,0x48,0x4f,0x41,0x41,0x41,0x4f}; |
raduschirila | 0:850d65a7d593 | 77 | |
raduschirila | 0:850d65a7d593 | 78 | |
raduschirila | 0:850d65a7d593 | 79 | |
raduschirila | 1:82127072abb8 | 80 | //DISPLAY FUNCTIONS DO NOT TOUCH |
raduschirila | 1:82127072abb8 | 81 | |
raduschirila | 0:850d65a7d593 | 82 | void write_to_max( int reg, int col) |
raduschirila | 0:850d65a7d593 | 83 | { |
raduschirila | 0:850d65a7d593 | 84 | load = LOW; // begin |
raduschirila | 0:850d65a7d593 | 85 | max72_spi.write(reg); // specify register |
raduschirila | 0:850d65a7d593 | 86 | max72_spi.write(col); // put data |
raduschirila | 0:850d65a7d593 | 87 | load = HIGH; // make sure data is loaded (on rising edge of LOAD/CS) |
raduschirila | 0:850d65a7d593 | 88 | } |
raduschirila | 0:850d65a7d593 | 89 | |
raduschirila | 4:82c807b7685e | 90 | //writes 8 bytes to the display |
raduschirila | 4:82c807b7685e | 91 | void pattern_to_display(char *testdata) |
raduschirila | 4:82c807b7685e | 92 | { |
raduschirila | 4:82c807b7685e | 93 | int cdata; |
raduschirila | 0:850d65a7d593 | 94 | for(int idx = 0; idx <= 7; idx++) { |
raduschirila | 4:82c807b7685e | 95 | cdata = testdata[idx]; |
raduschirila | 0:850d65a7d593 | 96 | write_to_max(idx+1,cdata); |
raduschirila | 0:850d65a7d593 | 97 | } |
raduschirila | 4:82c807b7685e | 98 | } |
raduschirila | 4:82c807b7685e | 99 | |
raduschirila | 0:850d65a7d593 | 100 | |
raduschirila | 0:850d65a7d593 | 101 | void setup_dot_matrix () |
raduschirila | 0:850d65a7d593 | 102 | { |
raduschirila | 0:850d65a7d593 | 103 | // initiation of the max 7219 |
raduschirila | 0:850d65a7d593 | 104 | // SPI setup: 8 bits, mode 0 |
raduschirila | 0:850d65a7d593 | 105 | max72_spi.format(8, 0); |
raduschirila | 4:82c807b7685e | 106 | |
raduschirila | 4:82c807b7685e | 107 | |
raduschirila | 4:82c807b7685e | 108 | |
raduschirila | 4:82c807b7685e | 109 | max72_spi.frequency(100000); //down to 100khx easier to scope ;-) |
raduschirila | 4:82c807b7685e | 110 | |
raduschirila | 0:850d65a7d593 | 111 | |
raduschirila | 0:850d65a7d593 | 112 | write_to_max(max7219_reg_scanLimit, 0x07); |
raduschirila | 0:850d65a7d593 | 113 | write_to_max(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits) |
raduschirila | 0:850d65a7d593 | 114 | write_to_max(max7219_reg_shutdown, 0x01); // not in shutdown mode |
raduschirila | 0:850d65a7d593 | 115 | write_to_max(max7219_reg_displayTest, 0x00); // no display test |
raduschirila | 0:850d65a7d593 | 116 | for (int e=1; e<=8; e++) { // empty registers, turn all LEDs off |
raduschirila | 0:850d65a7d593 | 117 | write_to_max(e,0); |
raduschirila | 0:850d65a7d593 | 118 | } |
raduschirila | 4:82c807b7685e | 119 | // maxAll(max7219_reg_intensity, 0x0f & 0x0f); // the first 0x0f is the value you can set |
raduschirila | 4:82c807b7685e | 120 | write_to_max(max7219_reg_intensity, 0x08); |
raduschirila | 4:82c807b7685e | 121 | |
raduschirila | 0:850d65a7d593 | 122 | } |
raduschirila | 0:850d65a7d593 | 123 | |
raduschirila | 4:82c807b7685e | 124 | void clear() |
raduschirila | 4:82c807b7685e | 125 | { |
raduschirila | 4:82c807b7685e | 126 | for (int e=1; e<=8; e++) { // empty registers, turn all LEDs off |
raduschirila | 0:850d65a7d593 | 127 | write_to_max(e,0); |
raduschirila | 0:850d65a7d593 | 128 | } |
raduschirila | 0:850d65a7d593 | 129 | } |
raduschirila | 1:82127072abb8 | 130 | |
raduschirila | 1:82127072abb8 | 131 | //END OF DISPLAY FUNCTIONS |
raduschirila | 1:82127072abb8 | 132 | |
raduschirila | 4:82c807b7685e | 133 | |
raduschirila | 3:5f89484f8c84 | 134 | |
raduschirila | 3:5f89484f8c84 | 135 | |
raduschirila | 3:5f89484f8c84 | 136 | |
raduschirila | 1:82127072abb8 | 137 | void splash_screen() |
raduschirila | 0:850d65a7d593 | 138 | { |
raduschirila | 0:850d65a7d593 | 139 | setup_dot_matrix (); /* setup matric */ |
raduschirila | 0:850d65a7d593 | 140 | pattern_to_display(heart); |
raduschirila | 0:850d65a7d593 | 141 | wait_ms(1000); |
raduschirila | 4:82c807b7685e | 142 | heart[7]=0x01; |
raduschirila | 4:82c807b7685e | 143 | heart[0]=0x01; |
raduschirila | 4:82c807b7685e | 144 | for(int i=1; i<=8; ++i) { |
raduschirila | 0:850d65a7d593 | 145 | pattern_to_display(heart); |
raduschirila | 0:850d65a7d593 | 146 | wait(0.2); |
raduschirila | 0:850d65a7d593 | 147 | heart[7]<<=1; |
raduschirila | 0:850d65a7d593 | 148 | heart[7]|=1; |
raduschirila | 0:850d65a7d593 | 149 | heart[0]=heart[7]; |
raduschirila | 0:850d65a7d593 | 150 | } |
raduschirila | 4:82c807b7685e | 151 | for(int i=1; i<=6; ++i) { |
raduschirila | 0:850d65a7d593 | 152 | heart[i]=~heart[i]; |
raduschirila | 4:82c807b7685e | 153 | } |
raduschirila | 0:850d65a7d593 | 154 | pattern_to_display(heart); |
raduschirila | 4:82c807b7685e | 155 | wait(1); |
raduschirila | 4:82c807b7685e | 156 | clear(); |
raduschirila | 4:82c807b7685e | 157 | } |
raduschirila | 4:82c807b7685e | 158 | void shift_all(char wav[8]) |
raduschirila | 4:82c807b7685e | 159 | { |
raduschirila | 4:82c807b7685e | 160 | for(int i=1;i<=7;++i) |
raduschirila | 4:82c807b7685e | 161 | { |
raduschirila | 4:82c807b7685e | 162 | wav[i-1]=wav[i]; |
raduschirila | 4:82c807b7685e | 163 | } |
raduschirila | 4:82c807b7685e | 164 | wav[7]=0x00; |
raduschirila | 4:82c807b7685e | 165 | pattern_to_display(wav); |
raduschirila | 0:850d65a7d593 | 166 | } |
raduschirila | 4:82c807b7685e | 167 | char wave[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}; |
raduschirila | 1:82127072abb8 | 168 | |
raduschirila | 4:82c807b7685e | 169 | void disp() |
raduschirila | 4:82c807b7685e | 170 | { |
raduschirila | 4:82c807b7685e | 171 | int steps=v[q]/0.0125; |
raduschirila | 4:82c807b7685e | 172 | for(int i=1;i<=steps;++i) |
raduschirila | 4:82c807b7685e | 173 | { |
raduschirila | 4:82c807b7685e | 174 | wave[7]<<=1; |
raduschirila | 4:82c807b7685e | 175 | wave[7]|=1; |
raduschirila | 4:82c807b7685e | 176 | |
raduschirila | 4:82c807b7685e | 177 | } |
raduschirila | 4:82c807b7685e | 178 | pattern_to_display(wave); |
raduschirila | 4:82c807b7685e | 179 | shift_all(wave); |
raduschirila | 4:82c807b7685e | 180 | } |
raduschirila | 3:5f89484f8c84 | 181 | |
raduschirila | 1:82127072abb8 | 182 | int main() |
raduschirila | 1:82127072abb8 | 183 | { |
raduschirila | 2:281d8c268a8e | 184 | pulse.attach(&get_pulse,0.0125);//attach the interrupt thing so it starts the ISR every 0.0125 s |
raduschirila | 4:82c807b7685e | 185 | |
raduschirila | 4:82c807b7685e | 186 | //splash_screen();//wait for the pulse to be stable by showing stuff on the display so the user is happy knowing he is gay |
raduschirila | 4:82c807b7685e | 187 | display_rate.attach(&disp,0.1); |
raduschirila | 4:82c807b7685e | 188 | while(1) { |
raduschirila | 1:82127072abb8 | 189 | } |
raduschirila | 1:82127072abb8 | 190 | } |