![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Part of the Pacemaker Project; this models the Pacemaker.
Dependencies: mbed TextLCD mbed-rtos
PacemakerController.cpp@2:5e9c4d83d038, 2015-11-28 (annotated)
- Committer:
- lucastai
- Date:
- Sat Nov 28 19:34:39 2015 +0000
- Revision:
- 2:5e9c4d83d038
- Parent:
- 1:979e9e785549
- Child:
- 3:44d132582373
Alarm, Mode working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chadnach1 | 0:d92e0b71c6b5 | 1 | #include "mbed.h" |
lucastai | 2:5e9c4d83d038 | 2 | #include "LPC17xx.h" |
lucastai | 2:5e9c4d83d038 | 3 | #include "TextLCD.h" |
lucastai | 2:5e9c4d83d038 | 4 | #include "rtos.h" |
lucastai | 2:5e9c4d83d038 | 5 | #include "Thread.h" |
lucastai | 2:5e9c4d83d038 | 6 | using namespace rtos; |
chadnach1 | 0:d92e0b71c6b5 | 7 | |
lucastai | 2:5e9c4d83d038 | 8 | |
chadnach1 | 0:d92e0b71c6b5 | 9 | // This is for the pacemaker |
lucastai | 2:5e9c4d83d038 | 10 | volatile unsigned short timer_count; |
lucastai | 2:5e9c4d83d038 | 11 | Serial pc(USBTX, USBRX); |
lucastai | 2:5e9c4d83d038 | 12 | TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x4); // rs, e, d4-d7 |
lucastai | 2:5e9c4d83d038 | 13 | |
chadnach1 | 0:d92e0b71c6b5 | 14 | |
chadnach1 | 0:d92e0b71c6b5 | 15 | int h_clock; |
chadnach1 | 0:d92e0b71c6b5 | 16 | |
lucastai | 2:5e9c4d83d038 | 17 | |
lucastai | 2:5e9c4d83d038 | 18 | // constants |
lucastai | 2:5e9c4d83d038 | 19 | int MAX_PM_RT = 180; |
lucastai | 2:5e9c4d83d038 | 20 | int MIN_PM_RT = 40; |
lucastai | 2:5e9c4d83d038 | 21 | enum mode {NORMAL, SLEEP, EXERCISE, MANUAL}; |
lucastai | 2:5e9c4d83d038 | 22 | |
lucastai | 2:5e9c4d83d038 | 23 | // counters |
lucastai | 2:5e9c4d83d038 | 24 | int beats = 0; |
lucastai | 2:5e9c4d83d038 | 25 | |
lucastai | 2:5e9c4d83d038 | 26 | // state variables |
lucastai | 2:5e9c4d83d038 | 27 | int upper_bound = 100; |
lucastai | 2:5e9c4d83d038 | 28 | int lower_bound = 40; |
lucastai | 2:5e9c4d83d038 | 29 | int obs_int = 10; |
lucastai | 2:5e9c4d83d038 | 30 | mode curr_mode = NORMAL; |
lucastai | 2:5e9c4d83d038 | 31 | |
lucastai | 2:5e9c4d83d038 | 32 | |
lucastai | 2:5e9c4d83d038 | 33 | |
lucastai | 2:5e9c4d83d038 | 34 | // alarms |
lucastai | 2:5e9c4d83d038 | 35 | DigitalOut Apace(LED1); |
lucastai | 2:5e9c4d83d038 | 36 | DigitalOut Vpace(LED2); |
lucastai | 2:5e9c4d83d038 | 37 | DigitalOut Asense(LED3); |
lucastai | 2:5e9c4d83d038 | 38 | DigitalOut Vsense(LED4); |
lucastai | 2:5e9c4d83d038 | 39 | |
lucastai | 2:5e9c4d83d038 | 40 | // hardware interrupt handler, adapted from code in piazza post by Dagaen |
lucastai | 2:5e9c4d83d038 | 41 | extern "C" void TIMER0_IRQHandler (void) |
lucastai | 2:5e9c4d83d038 | 42 | { |
lucastai | 2:5e9c4d83d038 | 43 | if((LPC_TIM0->IR & 0x01) == 0x01) // if MR0 interrupt, proceed |
lucastai | 2:5e9c4d83d038 | 44 | { |
lucastai | 2:5e9c4d83d038 | 45 | LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag |
lucastai | 2:5e9c4d83d038 | 46 | timer_count++; //increment timer_count |
lucastai | 2:5e9c4d83d038 | 47 | } |
lucastai | 2:5e9c4d83d038 | 48 | } |
lucastai | 2:5e9c4d83d038 | 49 | |
lucastai | 2:5e9c4d83d038 | 50 | // init the hardware interrupt (timer0), adapted same as above |
lucastai | 2:5e9c4d83d038 | 51 | void timer0_init(void) |
lucastai | 2:5e9c4d83d038 | 52 | { |
lucastai | 2:5e9c4d83d038 | 53 | LPC_SC->PCONP |=1<1; //timer0 power on |
lucastai | 2:5e9c4d83d038 | 54 | LPC_SC-> PCLKSEL0 |= 1 << 2; // set timer clock to CCLCK nondivided (1 clock cycle = 1 increment) |
lucastai | 2:5e9c4d83d038 | 55 | LPC_TIM0->MR0 = 1000000; //100mhz clock cycle, 1 cycle = 10ns, 10ms = 10 000 000 ns = 1M cycles |
lucastai | 2:5e9c4d83d038 | 56 | LPC_TIM0->MCR = 3; //interrupt and reset control |
lucastai | 2:5e9c4d83d038 | 57 | //3 = Interrupt & reset timer0 on match (111) sets all three bits |
lucastai | 2:5e9c4d83d038 | 58 | NVIC_EnableIRQ(TIMER0_IRQn); //enable timer0 interrupt |
chadnach1 | 0:d92e0b71c6b5 | 59 | } |
chadnach1 | 0:d92e0b71c6b5 | 60 | |
lucastai | 2:5e9c4d83d038 | 61 | |
lucastai | 2:5e9c4d83d038 | 62 | void PM_ALARM(void const *argument){ |
lucastai | 2:5e9c4d83d038 | 63 | |
lucastai | 2:5e9c4d83d038 | 64 | // min hr alarm |
lucastai | 2:5e9c4d83d038 | 65 | if( beats < MIN_PM_RT){ |
lucastai | 2:5e9c4d83d038 | 66 | lcd.locate(0,1); |
lucastai | 2:5e9c4d83d038 | 67 | lcd.printf("!<"); |
lucastai | 2:5e9c4d83d038 | 68 | } |
lucastai | 2:5e9c4d83d038 | 69 | |
lucastai | 2:5e9c4d83d038 | 70 | // max hr alarm |
lucastai | 2:5e9c4d83d038 | 71 | if(beats > MAX_PM_RT){ |
lucastai | 2:5e9c4d83d038 | 72 | lcd.locate(0,1); |
lucastai | 2:5e9c4d83d038 | 73 | lcd.printf("!>"); |
lucastai | 2:5e9c4d83d038 | 74 | } |
chadnach1 | 0:d92e0b71c6b5 | 75 | |
chadnach1 | 0:d92e0b71c6b5 | 76 | } |
chadnach1 | 0:d92e0b71c6b5 | 77 | |
lucastai | 2:5e9c4d83d038 | 78 | // hw interrupt callback, deal with the keyboard input from PC |
lucastai | 2:5e9c4d83d038 | 79 | void MODE_SWITCH() { |
lucastai | 2:5e9c4d83d038 | 80 | |
lucastai | 2:5e9c4d83d038 | 81 | // get the char, put it on the PC command line |
lucastai | 2:5e9c4d83d038 | 82 | char a = pc.getc(); |
lucastai | 2:5e9c4d83d038 | 83 | |
lucastai | 2:5e9c4d83d038 | 84 | // if the char is N, update bounds to normal mode |
lucastai | 2:5e9c4d83d038 | 85 | if(a == 'N'){ |
lucastai | 2:5e9c4d83d038 | 86 | curr_mode = NORMAL; |
lucastai | 2:5e9c4d83d038 | 87 | upper_bound = 100; |
lucastai | 2:5e9c4d83d038 | 88 | lower_bound = 40; |
lucastai | 2:5e9c4d83d038 | 89 | pc.printf("MODE IS N\n"); |
lucastai | 2:5e9c4d83d038 | 90 | // if the char is S, set bounds to sleep |
lucastai | 2:5e9c4d83d038 | 91 | }else if (a == 'S'){ |
lucastai | 2:5e9c4d83d038 | 92 | curr_mode = SLEEP; |
lucastai | 2:5e9c4d83d038 | 93 | upper_bound = 60; |
lucastai | 2:5e9c4d83d038 | 94 | lower_bound = 30; |
lucastai | 2:5e9c4d83d038 | 95 | pc.printf("MODE IS S\n"); |
lucastai | 2:5e9c4d83d038 | 96 | // if the char is E, set bounds to exercise |
lucastai | 2:5e9c4d83d038 | 97 | }else if (a == 'E'){ |
lucastai | 2:5e9c4d83d038 | 98 | curr_mode = EXERCISE; |
lucastai | 2:5e9c4d83d038 | 99 | upper_bound = 175; |
lucastai | 2:5e9c4d83d038 | 100 | lower_bound = 100; |
lucastai | 2:5e9c4d83d038 | 101 | pc.printf("MODE IS E\n"); |
lucastai | 2:5e9c4d83d038 | 102 | beats = 2; |
lucastai | 2:5e9c4d83d038 | 103 | // if the char is M, set to manual |
lucastai | 2:5e9c4d83d038 | 104 | }else if (a == 'M'){ |
lucastai | 2:5e9c4d83d038 | 105 | curr_mode = MANUAL; |
lucastai | 2:5e9c4d83d038 | 106 | upper_bound = 175; |
lucastai | 2:5e9c4d83d038 | 107 | lower_bound = 30; |
lucastai | 2:5e9c4d83d038 | 108 | beats = 300; |
lucastai | 2:5e9c4d83d038 | 109 | pc.printf("MODE IS MANUAL\n"); |
lucastai | 2:5e9c4d83d038 | 110 | // check for A if mode is manual |
lucastai | 2:5e9c4d83d038 | 111 | }else if (a == 'A'){ |
lucastai | 2:5e9c4d83d038 | 112 | if(curr_mode == MANUAL){ |
lucastai | 2:5e9c4d83d038 | 113 | pc.printf("MODE IS MANUAL GOT APACE\n"); |
lucastai | 2:5e9c4d83d038 | 114 | } |
lucastai | 2:5e9c4d83d038 | 115 | // check for V is mode is manual |
lucastai | 2:5e9c4d83d038 | 116 | }else if (a == 'V'){ |
lucastai | 2:5e9c4d83d038 | 117 | if(curr_mode == MANUAL){ |
lucastai | 2:5e9c4d83d038 | 118 | pc.printf("MODE IS MANUAL GOT VPACE\n"); |
lucastai | 2:5e9c4d83d038 | 119 | } |
lucastai | 2:5e9c4d83d038 | 120 | }else{ |
lucastai | 2:5e9c4d83d038 | 121 | // do nothing for invalid char |
lucastai | 2:5e9c4d83d038 | 122 | } |
lucastai | 2:5e9c4d83d038 | 123 | |
lucastai | 2:5e9c4d83d038 | 124 | |
lucastai | 2:5e9c4d83d038 | 125 | } |
lucastai | 2:5e9c4d83d038 | 126 | |
lucastai | 2:5e9c4d83d038 | 127 | |
lucastai | 2:5e9c4d83d038 | 128 | |
lucastai | 2:5e9c4d83d038 | 129 | void apace() { |
lucastai | 2:5e9c4d83d038 | 130 | |
lucastai | 2:5e9c4d83d038 | 131 | } |
lucastai | 2:5e9c4d83d038 | 132 | |
lucastai | 2:5e9c4d83d038 | 133 | void vpace() { |
lucastai | 2:5e9c4d83d038 | 134 | |
chadnach1 | 0:d92e0b71c6b5 | 135 | } |
chadnach1 | 0:d92e0b71c6b5 | 136 | |
chadnach1 | 0:d92e0b71c6b5 | 137 | int main() { |
lucastai | 2:5e9c4d83d038 | 138 | // connect the serial device (PC keybd) to the interrupt |
lucastai | 2:5e9c4d83d038 | 139 | pc.attach(&MODE_SWITCH); |
lucastai | 2:5e9c4d83d038 | 140 | |
lucastai | 2:5e9c4d83d038 | 141 | //Thread t3(PM_ALARM); |
chadnach1 | 0:d92e0b71c6b5 | 142 | } |