fgjfjfdjdjdjd
Dependencies: mbed
Fork of EDP2_display by
main.cpp@3:5f89484f8c84, 2018-02-27 (annotated)
- Committer:
- raduschirila
- Date:
- Tue Feb 27 13:58:05 2018 +0000
- Revision:
- 3:5f89484f8c84
- Parent:
- 2:281d8c268a8e
- Child:
- 4:82c807b7685e
under construction, normalized wave
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 | 0:850d65a7d593 | 23 | Ticker pulse; |
raduschirila | 0:850d65a7d593 | 24 | AnalogIn pulse_in(PTB0); |
raduschirila | 0:850d65a7d593 | 25 | AnalogOut o(PTE30); |
raduschirila | 2:281d8c268a8e | 26 | DigitalOut led(PTD5); |
raduschirila | 3:5f89484f8c84 | 27 | float xi,ypast,y,alpha=0.5;//absolute max |
raduschirila | 3:5f89484f8c84 | 28 | float v[80]; int q=0; |
raduschirila | 2:281d8c268a8e | 29 | bool first=true; |
raduschirila | 3:5f89484f8c84 | 30 | |
raduschirila | 0:850d65a7d593 | 31 | void get_pulse() |
raduschirila | 0:850d65a7d593 | 32 | { |
raduschirila | 3:5f89484f8c84 | 33 | if(q==80) q=0; |
raduschirila | 3:5f89484f8c84 | 34 | xi=(float)pulse_in; |
raduschirila | 2:281d8c268a8e | 35 | //noise reduction algorithm |
raduschirila | 2:281d8c268a8e | 36 | if(!first)//noise filtering procedure |
raduschirila | 2:281d8c268a8e | 37 | { |
raduschirila | 3:5f89484f8c84 | 38 | y= (alpha*xi)+ ((1-alpha)*ypast); |
raduschirila | 3:5f89484f8c84 | 39 | ypast=y; |
raduschirila | 2:281d8c268a8e | 40 | } |
raduschirila | 2:281d8c268a8e | 41 | else |
raduschirila | 2:281d8c268a8e | 42 | first=false; |
raduschirila | 3:5f89484f8c84 | 43 | v[q]=xi; |
raduschirila | 3:5f89484f8c84 | 44 | o=v[q]; |
raduschirila | 3:5f89484f8c84 | 45 | q++; |
raduschirila | 0:850d65a7d593 | 46 | } |
raduschirila | 0:850d65a7d593 | 47 | |
raduschirila | 0:850d65a7d593 | 48 | |
raduschirila | 0:850d65a7d593 | 49 | |
raduschirila | 0:850d65a7d593 | 50 | |
raduschirila | 0:850d65a7d593 | 51 | #define LOW 0 |
raduschirila | 0:850d65a7d593 | 52 | #define HIGH 1 |
raduschirila | 0:850d65a7d593 | 53 | |
raduschirila | 0:850d65a7d593 | 54 | SPI max72_spi(PTD2, NC, PTD1); |
raduschirila | 0:850d65a7d593 | 55 | DigitalOut load(PTD0); //will provide the load signal |
raduschirila | 0:850d65a7d593 | 56 | |
raduschirila | 0:850d65a7d593 | 57 | |
raduschirila | 0:850d65a7d593 | 58 | char heart[8] = {0x00,0x36,0x7f,0x7f,0x3e,0x1c,0x08,0x00}; |
raduschirila | 0:850d65a7d593 | 59 | |
raduschirila | 0:850d65a7d593 | 60 | |
raduschirila | 1:82127072abb8 | 61 | //DISPLAY FUNCTIONS DO NOT TOUCH |
raduschirila | 1:82127072abb8 | 62 | |
raduschirila | 0:850d65a7d593 | 63 | void write_to_max( int reg, int col) |
raduschirila | 0:850d65a7d593 | 64 | { |
raduschirila | 0:850d65a7d593 | 65 | load = LOW; // begin |
raduschirila | 0:850d65a7d593 | 66 | max72_spi.write(reg); // specify register |
raduschirila | 0:850d65a7d593 | 67 | max72_spi.write(col); // put data |
raduschirila | 0:850d65a7d593 | 68 | load = HIGH; // make sure data is loaded (on rising edge of LOAD/CS) |
raduschirila | 0:850d65a7d593 | 69 | } |
raduschirila | 0:850d65a7d593 | 70 | |
raduschirila | 0:850d65a7d593 | 71 | //writes 8 bytes to the display |
raduschirila | 0:850d65a7d593 | 72 | void pattern_to_display(char *testdata){ |
raduschirila | 0:850d65a7d593 | 73 | int cdata; |
raduschirila | 0:850d65a7d593 | 74 | for(int idx = 0; idx <= 7; idx++) { |
raduschirila | 0:850d65a7d593 | 75 | cdata = testdata[idx]; |
raduschirila | 0:850d65a7d593 | 76 | write_to_max(idx+1,cdata); |
raduschirila | 0:850d65a7d593 | 77 | } |
raduschirila | 0:850d65a7d593 | 78 | } |
raduschirila | 0:850d65a7d593 | 79 | |
raduschirila | 0:850d65a7d593 | 80 | |
raduschirila | 0:850d65a7d593 | 81 | void setup_dot_matrix () |
raduschirila | 0:850d65a7d593 | 82 | { |
raduschirila | 0:850d65a7d593 | 83 | // initiation of the max 7219 |
raduschirila | 0:850d65a7d593 | 84 | // SPI setup: 8 bits, mode 0 |
raduschirila | 0:850d65a7d593 | 85 | max72_spi.format(8, 0); |
raduschirila | 0:850d65a7d593 | 86 | |
raduschirila | 0:850d65a7d593 | 87 | |
raduschirila | 0:850d65a7d593 | 88 | |
raduschirila | 0:850d65a7d593 | 89 | max72_spi.frequency(100000); //down to 100khx easier to scope ;-) |
raduschirila | 0:850d65a7d593 | 90 | |
raduschirila | 0:850d65a7d593 | 91 | |
raduschirila | 0:850d65a7d593 | 92 | write_to_max(max7219_reg_scanLimit, 0x07); |
raduschirila | 0:850d65a7d593 | 93 | write_to_max(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits) |
raduschirila | 0:850d65a7d593 | 94 | write_to_max(max7219_reg_shutdown, 0x01); // not in shutdown mode |
raduschirila | 0:850d65a7d593 | 95 | write_to_max(max7219_reg_displayTest, 0x00); // no display test |
raduschirila | 0:850d65a7d593 | 96 | for (int e=1; e<=8; e++) { // empty registers, turn all LEDs off |
raduschirila | 0:850d65a7d593 | 97 | write_to_max(e,0); |
raduschirila | 0:850d65a7d593 | 98 | } |
raduschirila | 0:850d65a7d593 | 99 | // maxAll(max7219_reg_intensity, 0x0f & 0x0f); // the first 0x0f is the value you can set |
raduschirila | 0:850d65a7d593 | 100 | write_to_max(max7219_reg_intensity, 0x08); |
raduschirila | 0:850d65a7d593 | 101 | |
raduschirila | 0:850d65a7d593 | 102 | } |
raduschirila | 0:850d65a7d593 | 103 | |
raduschirila | 0:850d65a7d593 | 104 | void clear(){ |
raduschirila | 0:850d65a7d593 | 105 | for (int e=1; e<=8; e++) { // empty registers, turn all LEDs off |
raduschirila | 0:850d65a7d593 | 106 | write_to_max(e,0); |
raduschirila | 0:850d65a7d593 | 107 | } |
raduschirila | 0:850d65a7d593 | 108 | } |
raduschirila | 1:82127072abb8 | 109 | |
raduschirila | 1:82127072abb8 | 110 | //END OF DISPLAY FUNCTIONS |
raduschirila | 1:82127072abb8 | 111 | |
raduschirila | 3:5f89484f8c84 | 112 | void detect_pulse() |
raduschirila | 3:5f89484f8c84 | 113 | { |
raduschirila | 3:5f89484f8c84 | 114 | if(slope(5,25,v[5],v[25])>SOMETHING NEED TESTING and slope(40,50,v[40],v[50])<SOMETHING NEEDS TESTING) |
raduschirila | 3:5f89484f8c84 | 115 | //then the pulse is where it is supposed to be |
raduschirila | 3:5f89484f8c84 | 116 | //so out put it with regard to 0 |
raduschirila | 3:5f89484f8c84 | 117 | |
raduschirila | 3:5f89484f8c84 | 118 | } |
raduschirila | 3:5f89484f8c84 | 119 | |
raduschirila | 3:5f89484f8c84 | 120 | inline float slope(float a1, float a2, float b1, float b2) |
raduschirila | 3:5f89484f8c84 | 121 | { |
raduschirila | 3:5f89484f8c84 | 122 | return (float)((b2-b1)/(a2-a1)); |
raduschirila | 3:5f89484f8c84 | 123 | } |
raduschirila | 3:5f89484f8c84 | 124 | |
raduschirila | 3:5f89484f8c84 | 125 | |
raduschirila | 1:82127072abb8 | 126 | void splash_screen() |
raduschirila | 0:850d65a7d593 | 127 | { |
raduschirila | 0:850d65a7d593 | 128 | setup_dot_matrix (); /* setup matric */ |
raduschirila | 0:850d65a7d593 | 129 | pattern_to_display(heart); |
raduschirila | 0:850d65a7d593 | 130 | wait_ms(1000); |
raduschirila | 0:850d65a7d593 | 131 | heart[7]=0x01;heart[0]=0x01; |
raduschirila | 0:850d65a7d593 | 132 | for(int i=1;i<=8;++i) |
raduschirila | 0:850d65a7d593 | 133 | { |
raduschirila | 0:850d65a7d593 | 134 | pattern_to_display(heart); |
raduschirila | 0:850d65a7d593 | 135 | wait(0.2); |
raduschirila | 0:850d65a7d593 | 136 | heart[7]<<=1; |
raduschirila | 0:850d65a7d593 | 137 | heart[7]|=1; |
raduschirila | 0:850d65a7d593 | 138 | heart[0]=heart[7]; |
raduschirila | 0:850d65a7d593 | 139 | } |
raduschirila | 0:850d65a7d593 | 140 | for(int i=1;i<=6;++i) |
raduschirila | 0:850d65a7d593 | 141 | { |
raduschirila | 0:850d65a7d593 | 142 | heart[i]=~heart[i]; |
raduschirila | 0:850d65a7d593 | 143 | } |
raduschirila | 0:850d65a7d593 | 144 | pattern_to_display(heart); |
raduschirila | 0:850d65a7d593 | 145 | wait(1.6); |
raduschirila | 0:850d65a7d593 | 146 | clear(); |
raduschirila | 0:850d65a7d593 | 147 | } |
raduschirila | 1:82127072abb8 | 148 | |
raduschirila | 3:5f89484f8c84 | 149 | |
raduschirila | 3:5f89484f8c84 | 150 | |
raduschirila | 1:82127072abb8 | 151 | int main() |
raduschirila | 1:82127072abb8 | 152 | { |
raduschirila | 2:281d8c268a8e | 153 | pulse.attach(&get_pulse,0.0125);//attach the interrupt thing so it starts the ISR every 0.0125 s |
raduschirila | 2:281d8c268a8e | 154 | 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 | 1:82127072abb8 | 155 | while(1) |
raduschirila | 1:82127072abb8 | 156 | { |
raduschirila | 3:5f89484f8c84 | 157 | |
raduschirila | 1:82127072abb8 | 158 | } |
raduschirila | 1:82127072abb8 | 159 | } |