Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Alarm.h@0:9e26d3c8b2b1, 2015-02-12 (annotated)
- Committer:
- nmlaastad
- Date:
- Thu Feb 12 13:49:58 2015 +0000
- Revision:
- 0:9e26d3c8b2b1
Ferdig greier (men stygt p? slutten)
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| nmlaastad | 0:9e26d3c8b2b1 | 1 | #include "mbed.h" |
| nmlaastad | 0:9e26d3c8b2b1 | 2 | #include <vector> |
| nmlaastad | 0:9e26d3c8b2b1 | 3 | #include <string> |
| nmlaastad | 0:9e26d3c8b2b1 | 4 | #include <list> |
| nmlaastad | 0:9e26d3c8b2b1 | 5 | #include "Logger.h" |
| nmlaastad | 0:9e26d3c8b2b1 | 6 | |
| nmlaastad | 0:9e26d3c8b2b1 | 7 | extern Logger GlobalLogger; |
| nmlaastad | 0:9e26d3c8b2b1 | 8 | |
| nmlaastad | 0:9e26d3c8b2b1 | 9 | class Alarm { |
| nmlaastad | 0:9e26d3c8b2b1 | 10 | private: |
| nmlaastad | 0:9e26d3c8b2b1 | 11 | std::string alarmNavn; |
| nmlaastad | 0:9e26d3c8b2b1 | 12 | std::vector<float> AlarmSpenninger; |
| nmlaastad | 0:9e26d3c8b2b1 | 13 | AnalogIn port; |
| nmlaastad | 0:9e26d3c8b2b1 | 14 | DigitalOut led; |
| nmlaastad | 0:9e26d3c8b2b1 | 15 | float feilmargin; |
| nmlaastad | 0:9e26d3c8b2b1 | 16 | bool LEDInvertStatus; |
| nmlaastad | 0:9e26d3c8b2b1 | 17 | bool alarmLogged; |
| nmlaastad | 0:9e26d3c8b2b1 | 18 | |
| nmlaastad | 0:9e26d3c8b2b1 | 19 | public: |
| nmlaastad | 0:9e26d3c8b2b1 | 20 | Alarm(std::string alarmNavn, PinName portIn, PinName ledOut) |
| nmlaastad | 0:9e26d3c8b2b1 | 21 | : alarmNavn(alarmNavn), port(portIn), led(ledOut), feilmargin(0.05), LEDInvertStatus(false), alarmLogged(false) |
| nmlaastad | 0:9e26d3c8b2b1 | 22 | { |
| nmlaastad | 0:9e26d3c8b2b1 | 23 | } |
| nmlaastad | 0:9e26d3c8b2b1 | 24 | ~Alarm() |
| nmlaastad | 0:9e26d3c8b2b1 | 25 | { |
| nmlaastad | 0:9e26d3c8b2b1 | 26 | } |
| nmlaastad | 0:9e26d3c8b2b1 | 27 | void AlarmVoltageAdd(float spenning) { |
| nmlaastad | 0:9e26d3c8b2b1 | 28 | AlarmSpenninger.push_back(spenning); |
| nmlaastad | 0:9e26d3c8b2b1 | 29 | } |
| nmlaastad | 0:9e26d3c8b2b1 | 30 | void AddCurrentAlarmVoltage(void) { |
| nmlaastad | 0:9e26d3c8b2b1 | 31 | AlarmVoltageAdd(AlarmVoltage()); |
| nmlaastad | 0:9e26d3c8b2b1 | 32 | } |
| nmlaastad | 0:9e26d3c8b2b1 | 33 | void AlarmVoltageCLearAll() { |
| nmlaastad | 0:9e26d3c8b2b1 | 34 | AlarmSpenninger.clear(); |
| nmlaastad | 0:9e26d3c8b2b1 | 35 | } |
| nmlaastad | 0:9e26d3c8b2b1 | 36 | float AlarmVoltage (void) { |
| nmlaastad | 0:9e26d3c8b2b1 | 37 | return port.read(); |
| nmlaastad | 0:9e26d3c8b2b1 | 38 | } |
| nmlaastad | 0:9e26d3c8b2b1 | 39 | void LEDSet(bool value) { |
| nmlaastad | 0:9e26d3c8b2b1 | 40 | if (LEDInvertStatus) led.write(!value); |
| nmlaastad | 0:9e26d3c8b2b1 | 41 | else led.write(value); |
| nmlaastad | 0:9e26d3c8b2b1 | 42 | } |
| nmlaastad | 0:9e26d3c8b2b1 | 43 | void LEDInverted(bool value) { |
| nmlaastad | 0:9e26d3c8b2b1 | 44 | LEDInvertStatus = value; |
| nmlaastad | 0:9e26d3c8b2b1 | 45 | } |
| nmlaastad | 0:9e26d3c8b2b1 | 46 | bool AlarmTest() { |
| nmlaastad | 0:9e26d3c8b2b1 | 47 | return test(); |
| nmlaastad | 0:9e26d3c8b2b1 | 48 | } |
| nmlaastad | 0:9e26d3c8b2b1 | 49 | void Reset() { |
| nmlaastad | 0:9e26d3c8b2b1 | 50 | LEDSet(0); |
| nmlaastad | 0:9e26d3c8b2b1 | 51 | alarmLogged = false; |
| nmlaastad | 0:9e26d3c8b2b1 | 52 | } |
| nmlaastad | 0:9e26d3c8b2b1 | 53 | |
| nmlaastad | 0:9e26d3c8b2b1 | 54 | |
| nmlaastad | 0:9e26d3c8b2b1 | 55 | private: |
| nmlaastad | 0:9e26d3c8b2b1 | 56 | bool test() { |
| nmlaastad | 0:9e26d3c8b2b1 | 57 | bool resultat = false; |
| nmlaastad | 0:9e26d3c8b2b1 | 58 | float portSpenning = port.read(); |
| nmlaastad | 0:9e26d3c8b2b1 | 59 | for (int i = 0; i < AlarmSpenninger.size(); i++) { |
| nmlaastad | 0:9e26d3c8b2b1 | 60 | if (portSpenning + feilmargin > AlarmSpenninger[i] && portSpenning - feilmargin < AlarmSpenninger[i]) { |
| nmlaastad | 0:9e26d3c8b2b1 | 61 | resultat = true; |
| nmlaastad | 0:9e26d3c8b2b1 | 62 | } |
| nmlaastad | 0:9e26d3c8b2b1 | 63 | } |
| nmlaastad | 0:9e26d3c8b2b1 | 64 | if (resultat && !alarmLogged) { |
| nmlaastad | 0:9e26d3c8b2b1 | 65 | alarmLogged = true; |
| nmlaastad | 0:9e26d3c8b2b1 | 66 | std::string melding = alarmNavn + " ble aktivert!"; |
| nmlaastad | 0:9e26d3c8b2b1 | 67 | GlobalLogger.Logg(melding); |
| nmlaastad | 0:9e26d3c8b2b1 | 68 | } |
| nmlaastad | 0:9e26d3c8b2b1 | 69 | return resultat; |
| nmlaastad | 0:9e26d3c8b2b1 | 70 | } |
| nmlaastad | 0:9e26d3c8b2b1 | 71 | |
| nmlaastad | 0:9e26d3c8b2b1 | 72 | }; |