![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Copy_Assignment3
Dependencies: mbed MCP23017 WattBob_TextLCD mbed-rtos
source/my_tools.cpp@1:d980a57e422a, 2018-03-28 (annotated)
- Committer:
- aoc2
- Date:
- Wed Mar 28 18:53:05 2018 +0000
- Revision:
- 1:d980a57e422a
- Parent:
- 0:8940db3353d7
Publish
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aoc2 | 0:8940db3353d7 | 1 | #include "my_tools.h" |
aoc2 | 0:8940db3353d7 | 2 | |
aoc2 | 0:8940db3353d7 | 3 | using namespace std; |
aoc2 | 0:8940db3353d7 | 4 | |
aoc2 | 0:8940db3353d7 | 5 | // TASK 1 |
aoc2 | 0:8940db3353d7 | 6 | // Read an analog input and return a float between 0 and 1 |
aoc2 | 0:8940db3353d7 | 7 | float readAnalogInput(AnalogIn pin) |
aoc2 | 0:8940db3353d7 | 8 | { |
aoc2 | 0:8940db3353d7 | 9 | return (pin.read()); |
aoc2 | 0:8940db3353d7 | 10 | } |
aoc2 | 0:8940db3353d7 | 11 | |
aoc2 | 0:8940db3353d7 | 12 | // Delete first value of the given vector (the head) |
aoc2 | 0:8940db3353d7 | 13 | void deleteFirstValueVec(vector<float> &vec) |
aoc2 | 0:8940db3353d7 | 14 | { |
aoc2 | 0:8940db3353d7 | 15 | vec.erase(vec.begin()); |
aoc2 | 0:8940db3353d7 | 16 | } |
aoc2 | 0:8940db3353d7 | 17 | |
aoc2 | 0:8940db3353d7 | 18 | // TASK 4 & 5 |
aoc2 | 0:8940db3353d7 | 19 | int monitorValue(float value, DigitalOut led, float thresholdValue) |
aoc2 | 0:8940db3353d7 | 20 | { |
aoc2 | 0:8940db3353d7 | 21 | if (value > thresholdValue) |
aoc2 | 0:8940db3353d7 | 22 | { |
aoc2 | 0:8940db3353d7 | 23 | led = 1; |
aoc2 | 0:8940db3353d7 | 24 | return (1); |
aoc2 | 0:8940db3353d7 | 25 | } |
aoc2 | 0:8940db3353d7 | 26 | else |
aoc2 | 0:8940db3353d7 | 27 | { |
aoc2 | 0:8940db3353d7 | 28 | led = 0; |
aoc2 | 0:8940db3353d7 | 29 | return (0); |
aoc2 | 0:8940db3353d7 | 30 | } |
aoc2 | 0:8940db3353d7 | 31 | } |
aoc2 | 0:8940db3353d7 | 32 | |
aoc2 | 0:8940db3353d7 | 33 | // TASK 6 |
aoc2 | 0:8940db3353d7 | 34 | void writeOnLCD(WattBob_TextLCD &lcd, float odometer, float avg) |
aoc2 | 0:8940db3353d7 | 35 | { |
aoc2 | 0:8940db3353d7 | 36 | static bool firstTime = true; |
aoc2 | 0:8940db3353d7 | 37 | /* We clear the LCD only the first time this function is run */ |
aoc2 | 0:8940db3353d7 | 38 | if (firstTime) |
aoc2 | 0:8940db3353d7 | 39 | { |
aoc2 | 0:8940db3353d7 | 40 | /* Clear Display */ |
aoc2 | 0:8940db3353d7 | 41 | lcd.cls(); |
aoc2 | 0:8940db3353d7 | 42 | |
aoc2 | 0:8940db3353d7 | 43 | /* Print Distance */ |
aoc2 | 0:8940db3353d7 | 44 | lcd.locate(0, 0); |
aoc2 | 0:8940db3353d7 | 45 | lcd.printf("D km: "); |
aoc2 | 0:8940db3353d7 | 46 | |
aoc2 | 0:8940db3353d7 | 47 | /* Print Avg Values of Speed */ |
aoc2 | 0:8940db3353d7 | 48 | lcd.locate(1, 0); |
aoc2 | 0:8940db3353d7 | 49 | lcd.printf("S kmh: "); |
aoc2 | 0:8940db3353d7 | 50 | |
aoc2 | 0:8940db3353d7 | 51 | firstTime = false; |
aoc2 | 0:8940db3353d7 | 52 | } |
aoc2 | 0:8940db3353d7 | 53 | |
aoc2 | 0:8940db3353d7 | 54 | /* Print Distance */ |
aoc2 | 0:8940db3353d7 | 55 | lcd.locate(0, 7); |
aoc2 | 0:8940db3353d7 | 56 | lcd.printf("%.2f", odometer/1000); |
aoc2 | 0:8940db3353d7 | 57 | |
aoc2 | 0:8940db3353d7 | 58 | /* Print Avg Value of Speed */ |
aoc2 | 0:8940db3353d7 | 59 | lcd.locate(1, 7); |
aoc2 | 0:8940db3353d7 | 60 | lcd.printf("%05.1f", avg*3.6); |
aoc2 | 0:8940db3353d7 | 61 | } |
aoc2 | 0:8940db3353d7 | 62 | |
aoc2 | 0:8940db3353d7 | 63 | // TASK 10 |
aoc2 | 0:8940db3353d7 | 64 | void updateLEDs(carStructure *myCar, bool right, bool left) |
aoc2 | 0:8940db3353d7 | 65 | { |
aoc2 | 0:8940db3353d7 | 66 | if (right && left) |
aoc2 | 0:8940db3353d7 | 67 | { |
aoc2 | 0:8940db3353d7 | 68 | // Both ON - 2Hz |
aoc2 | 0:8940db3353d7 | 69 | myCar->p_rightLight->period_ms(500); |
aoc2 | 0:8940db3353d7 | 70 | myCar->p_rightLight->pulsewidth_ms(250); |
aoc2 | 0:8940db3353d7 | 71 | |
aoc2 | 0:8940db3353d7 | 72 | myCar->p_leftLight->period_ms(500); |
aoc2 | 0:8940db3353d7 | 73 | myCar->p_leftLight->pulsewidth_ms(250); |
aoc2 | 0:8940db3353d7 | 74 | } |
aoc2 | 0:8940db3353d7 | 75 | else if (right) |
aoc2 | 0:8940db3353d7 | 76 | { |
aoc2 | 0:8940db3353d7 | 77 | // Only right - 1Hz |
aoc2 | 0:8940db3353d7 | 78 | myCar->p_rightLight->period_ms(1000); |
aoc2 | 0:8940db3353d7 | 79 | myCar->p_rightLight->pulsewidth_ms(500); |
aoc2 | 0:8940db3353d7 | 80 | |
aoc2 | 0:8940db3353d7 | 81 | myCar->p_leftLight->period_ms(1000); |
aoc2 | 0:8940db3353d7 | 82 | myCar->p_leftLight->pulsewidth_ms(0); |
aoc2 | 0:8940db3353d7 | 83 | } |
aoc2 | 0:8940db3353d7 | 84 | else if (left) |
aoc2 | 0:8940db3353d7 | 85 | { |
aoc2 | 0:8940db3353d7 | 86 | // Only left - 1Hz |
aoc2 | 0:8940db3353d7 | 87 | myCar->p_rightLight->period_ms(1000); |
aoc2 | 0:8940db3353d7 | 88 | myCar->p_rightLight->pulsewidth_ms(0); |
aoc2 | 0:8940db3353d7 | 89 | |
aoc2 | 0:8940db3353d7 | 90 | myCar->p_leftLight->period_ms(1000); |
aoc2 | 0:8940db3353d7 | 91 | myCar->p_leftLight->pulsewidth_ms(500); |
aoc2 | 0:8940db3353d7 | 92 | } |
aoc2 | 0:8940db3353d7 | 93 | else |
aoc2 | 0:8940db3353d7 | 94 | { |
aoc2 | 0:8940db3353d7 | 95 | // Both OFF |
aoc2 | 0:8940db3353d7 | 96 | myCar->p_rightLight->period_ms(1000); |
aoc2 | 0:8940db3353d7 | 97 | myCar->p_rightLight->pulsewidth_ms(0.0); |
aoc2 | 0:8940db3353d7 | 98 | |
aoc2 | 0:8940db3353d7 | 99 | myCar->p_leftLight->period_ms(1000); |
aoc2 | 0:8940db3353d7 | 100 | myCar->p_leftLight->pulsewidth_ms(0.0); |
aoc2 | 0:8940db3353d7 | 101 | } |
aoc2 | 0:8940db3353d7 | 102 | } |