
last version
Dependencies: LCD_DISCO_F429ZI mbed BSP_DISCO_F429ZI
servo_gui.cpp@2:e23243b26a23, 2020-06-09 (annotated)
- Committer:
- bolko
- Date:
- Tue Jun 09 11:23:48 2020 +0000
- Revision:
- 2:e23243b26a23
look;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bolko | 2:e23243b26a23 | 1 | #include "servo_gui.h" |
bolko | 2:e23243b26a23 | 2 | |
bolko | 2:e23243b26a23 | 3 | |
bolko | 2:e23243b26a23 | 4 | DigitalIn MyCallibButton(USER_BUTTON); |
bolko | 2:e23243b26a23 | 5 | |
bolko | 2:e23243b26a23 | 6 | ServoGui::ServoGui(){ |
bolko | 2:e23243b26a23 | 7 | lcd.Clear(LCD_COLOR_WHITE); |
bolko | 2:e23243b26a23 | 8 | ui8ActualLedPoint = 0; |
bolko | 2:e23243b26a23 | 9 | DrawDisk(); |
bolko | 2:e23243b26a23 | 10 | LedOn(0); |
bolko | 2:e23243b26a23 | 11 | |
bolko | 2:e23243b26a23 | 12 | } |
bolko | 2:e23243b26a23 | 13 | |
bolko | 2:e23243b26a23 | 14 | enum eState ServoGui::eReadDetector(){ |
bolko | 2:e23243b26a23 | 15 | if(MyCallibButton == 1){ |
bolko | 2:e23243b26a23 | 16 | return ON; |
bolko | 2:e23243b26a23 | 17 | } |
bolko | 2:e23243b26a23 | 18 | else { |
bolko | 2:e23243b26a23 | 19 | return OFF; |
bolko | 2:e23243b26a23 | 20 | } |
bolko | 2:e23243b26a23 | 21 | } |
bolko | 2:e23243b26a23 | 22 | |
bolko | 2:e23243b26a23 | 23 | void ServoGui::ServoCallib(){ |
bolko | 2:e23243b26a23 | 24 | while(eReadDetector() == OFF){ |
bolko | 2:e23243b26a23 | 25 | LedStep(RIGHT); |
bolko | 2:e23243b26a23 | 26 | wait(DELAY); |
bolko | 2:e23243b26a23 | 27 | } |
bolko | 2:e23243b26a23 | 28 | sServo.uiCurrentPosition = ui8ActualLedPoint; |
bolko | 2:e23243b26a23 | 29 | sServo.uiDesiredPosition = ui8ActualLedPoint; |
bolko | 2:e23243b26a23 | 30 | |
bolko | 2:e23243b26a23 | 31 | } |
bolko | 2:e23243b26a23 | 32 | |
bolko | 2:e23243b26a23 | 33 | void ServoGui::ServoGoTo(uint8_t ui8Position){ |
bolko | 2:e23243b26a23 | 34 | sServo.uiDesiredPosition = ui8Position; |
bolko | 2:e23243b26a23 | 35 | while(sServo.uiCurrentPosition != sServo.uiDesiredPosition){ |
bolko | 2:e23243b26a23 | 36 | if(sServo.uiCurrentPosition < sServo.uiDesiredPosition){ |
bolko | 2:e23243b26a23 | 37 | LedStep(LEFT); |
bolko | 2:e23243b26a23 | 38 | sServo.uiCurrentPosition++; |
bolko | 2:e23243b26a23 | 39 | } |
bolko | 2:e23243b26a23 | 40 | else if(sServo.uiCurrentPosition > sServo.uiDesiredPosition){ |
bolko | 2:e23243b26a23 | 41 | LedStep(RIGHT); |
bolko | 2:e23243b26a23 | 42 | sServo.uiCurrentPosition--; |
bolko | 2:e23243b26a23 | 43 | } |
bolko | 2:e23243b26a23 | 44 | wait(DELAY); |
bolko | 2:e23243b26a23 | 45 | } |
bolko | 2:e23243b26a23 | 46 | } |
bolko | 2:e23243b26a23 | 47 | |
bolko | 2:e23243b26a23 | 48 | void ServoGui::DrawLed(uint8_t ui8LedNumber){ |
bolko | 2:e23243b26a23 | 49 | |
bolko | 2:e23243b26a23 | 50 | if(ui8LedNumber <= 3){ |
bolko | 2:e23243b26a23 | 51 | lcd.SetTextColor(LCD_COLOR_BLACK); |
bolko | 2:e23243b26a23 | 52 | lcd.DrawCircle(LED_X + LED_DISTANCE*ui8LedNumber,LED_Y,LED_RADIUS); |
bolko | 2:e23243b26a23 | 53 | } |
bolko | 2:e23243b26a23 | 54 | if(eLedStates[ui8LedNumber] == ON){ |
bolko | 2:e23243b26a23 | 55 | lcd.SetTextColor(LCD_COLOR_BLUE); |
bolko | 2:e23243b26a23 | 56 | } |
bolko | 2:e23243b26a23 | 57 | else{ |
bolko | 2:e23243b26a23 | 58 | lcd.SetTextColor(LCD_COLOR_WHITE); |
bolko | 2:e23243b26a23 | 59 | } |
bolko | 2:e23243b26a23 | 60 | |
bolko | 2:e23243b26a23 | 61 | lcd.FillCircle(LED_X+LED_DISTANCE*ui8LedNumber,LED_Y ,LED_RADIUS-2); |
bolko | 2:e23243b26a23 | 62 | } |
bolko | 2:e23243b26a23 | 63 | |
bolko | 2:e23243b26a23 | 64 | |
bolko | 2:e23243b26a23 | 65 | void ServoGui::LedOn(uint8_t ui8LedNumber){ |
bolko | 2:e23243b26a23 | 66 | for(uint8_t ui8LedCounter = 0; ui8LedCounter <= 3; ui8LedCounter++){ |
bolko | 2:e23243b26a23 | 67 | eLedStates[ui8LedCounter] = OFF; |
bolko | 2:e23243b26a23 | 68 | DrawLed(ui8LedCounter); |
bolko | 2:e23243b26a23 | 69 | } |
bolko | 2:e23243b26a23 | 70 | if(ui8LedNumber <= 3){ |
bolko | 2:e23243b26a23 | 71 | eLedStates[ui8LedNumber] = ON; |
bolko | 2:e23243b26a23 | 72 | DrawLed(ui8LedNumber); |
bolko | 2:e23243b26a23 | 73 | } |
bolko | 2:e23243b26a23 | 74 | } |
bolko | 2:e23243b26a23 | 75 | |
bolko | 2:e23243b26a23 | 76 | void ServoGui::LedStep(enum eDirection eWhere){ |
bolko | 2:e23243b26a23 | 77 | if(eWhere == RIGHT){ |
bolko | 2:e23243b26a23 | 78 | ui8ActualLedPoint = ui8ActualLedPoint + 1; |
bolko | 2:e23243b26a23 | 79 | } |
bolko | 2:e23243b26a23 | 80 | else if(eWhere == LEFT){ |
bolko | 2:e23243b26a23 | 81 | ui8ActualLedPoint = ui8ActualLedPoint - 1; |
bolko | 2:e23243b26a23 | 82 | } |
bolko | 2:e23243b26a23 | 83 | LedOn(ui8ActualLedPoint%4); |
bolko | 2:e23243b26a23 | 84 | DrawDisk(); |
bolko | 2:e23243b26a23 | 85 | } |
bolko | 2:e23243b26a23 | 86 | |
bolko | 2:e23243b26a23 | 87 | void ServoGui::DrawDisk(){ |
bolko | 2:e23243b26a23 | 88 | double dAngleOfPointer = (ui8ActualLedPoint%MAX_STEPS); |
bolko | 2:e23243b26a23 | 89 | dAngleOfPointer = dAngleOfPointer/ MAX_STEPS; |
bolko | 2:e23243b26a23 | 90 | dAngleOfPointer = dAngleOfPointer * 2 * PI; |
bolko | 2:e23243b26a23 | 91 | |
bolko | 2:e23243b26a23 | 92 | lcd.SetTextColor(LCD_COLOR_BLACK); |
bolko | 2:e23243b26a23 | 93 | lcd.FillCircle(DISK_X,DISK_Y,DISK_RADIUS); |
bolko | 2:e23243b26a23 | 94 | lcd.SetTextColor(LCD_COLOR_WHITE); |
bolko | 2:e23243b26a23 | 95 | lcd.DrawLine(DISK_X,DISK_Y,DISK_X+(DISK_RADIUS * cos(dAngleOfPointer)), DISK_Y + (DISK_RADIUS * sin(dAngleOfPointer))); |
bolko | 2:e23243b26a23 | 96 | lcd.FillCircle(DISK_X,DISK_Y,SMALL_DISK_RADIUS); |
bolko | 2:e23243b26a23 | 97 | } |