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