Revision 0:9083554aa264, committed 2020-05-18
- Comitter:
- lolkusus
- Date:
- Mon May 18 17:07:54 2020 +0000
- Commit message:
- Oddanie;
Changed in this revision
STEPPER_LED_GUI.cpp | Show annotated file Show diff for this revision Revisions of this file |
STEPPER_LED_GUI.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 9083554aa264 STEPPER_LED_GUI.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/STEPPER_LED_GUI.cpp Mon May 18 17:07:54 2020 +0000 @@ -0,0 +1,127 @@ +#include "STEPPER_LED_GUI.h" + +DigitalIn CALLIB_BUTTON(USER_BUTTON); + +StepperLedGui::StepperLedGui() +{ + lcd.Clear(LCD_COLOR_WHITE); + ClearLeds(); + uiLedCounter = 0; + SetLed(0); + DrawMotor(); +} + +void StepperLedGui::MotorCallib() +{ + while(ReadDetector() == OFF) + { + LedStep(RIGHT); + wait(STEP_PERIOD); + } + uiZeroPosition = uiLedCounter; +} + +void StepperLedGui::MotorSteps(unsigned int uiStepCount) +{ + for (unsigned int uiStepCounter = 0; uiStepCounter < uiStepCount; uiStepCounter++) + { + LedStep(RIGHT); + wait(STEP_PERIOD); + } +} + +void StepperLedGui::gotoPosition(unsigned int uiPosition) +{ + eDirection eMovementDirection; + unsigned int uiRelativePosition; + uiRelativePosition = uiLedCounter - uiZeroPosition; + + if (uiPosition > uiRelativePosition) + { + eMovementDirection = RIGHT; + } + else if (uiPosition < uiRelativePosition) + { + eMovementDirection = LEFT; + } + + while(uiRelativePosition != uiPosition) + { + LedStep(eMovementDirection); + uiRelativePosition = uiLedCounter - uiZeroPosition; + wait(STEP_PERIOD); + } +} + +////////////////////////////////private/////////////// + +tState StepperLedGui::ReadDetector() +{ + if (CALLIB_BUTTON == 1) + return ON; + else + return OFF; +} + +void StepperLedGui::DrawLed(unsigned char ucLedNumber) +{ + if (ucLedNumber < LED_COUNT) + { + lcd.SetTextColor(LCD_COLOR_BLACK); + lcd.DrawCircle(LED_START_X+LED_DISTANCE*ucLedNumber,LED_Y,LED_RADIUS); + + if (eLedStates[ucLedNumber] == ON) + { + lcd.SetTextColor(LCD_COLOR_GREEN); + } + else + { + lcd.SetTextColor(LCD_COLOR_WHITE); + } + + lcd.FillCircle(LED_START_X+LED_DISTANCE*ucLedNumber,LED_Y,LED_RADIUS-LED_RIM_WIDTH); + } +} + +void StepperLedGui::SetLed(unsigned char ucLedNumber) +{ + if (ucLedNumber < LED_COUNT) + { + ClearLeds(); + eLedStates[ucLedNumber] = ON; + DrawLed(ucLedNumber); + } +} + +void StepperLedGui::ClearLeds() +{ + for (unsigned char ucLedCounter = 0; ucLedCounter < LED_COUNT; ucLedCounter++) + { + eLedStates[ucLedCounter] = OFF; + DrawLed(ucLedCounter); + } +} + +void StepperLedGui::LedStep(eDirection Direction) +{ + if (Direction == LEFT) + uiLedCounter = uiLedCounter - 1; + else if (Direction == RIGHT) + uiLedCounter = uiLedCounter + 1; + SetLed(uiLedCounter % LED_COUNT); + DrawMotor(); +} + +void StepperLedGui::DrawMotor() +{ + double dAngle = (uiLedCounter%STEPS_PER_REV); + dAngle = dAngle / STEPS_PER_REV; + dAngle = dAngle * 2 * PI; + lcd.SetTextColor(LCD_COLOR_BLUE); + lcd.FillCircle(MOTOR_X,MOTOR_Y,MOTOR_RADIUS); + lcd.SetTextColor(LCD_COLOR_RED); + lcd.DrawLine(MOTOR_X,MOTOR_Y,MOTOR_X+MOTOR_RADIUS*cos(dAngle), MOTOR_Y+MOTOR_RADIUS*sin(dAngle)); + lcd.SetTextColor(LCD_COLOR_WHITE); + lcd.FillCircle(MOTOR_X,MOTOR_Y,MOTOR_SMALL_RADIUS); + +}
diff -r 000000000000 -r 9083554aa264 STEPPER_LED_GUI.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/STEPPER_LED_GUI.h Mon May 18 17:07:54 2020 +0000 @@ -0,0 +1,56 @@ +#ifndef __STEPPER_LED_GUI_H +#define __STEPPER_LED_GUI_H + +#include "LCD_DISCO_F429ZI.h" +#include "mbed.h" + +#define LED_COUNT 4 +#define LED_START_X 30 +#define LED_Y 290 +#define LED_RADIUS 20 +#define LED_DISTANCE 60 +#define LED_RIM_WIDTH 1 + +#define MOTOR_X 120 +#define MOTOR_Y 120 +#define MOTOR_RADIUS 100 +#define MOTOR_SMALL_RADIUS 20 +#define STEPS_PER_REV 16 + +#define PI 3.14159 + +#define STEP_PERIOD 0.25 + +typedef enum tState +{ + ON, + OFF +} tState; + +typedef enum eDirection{ + LEFT, + RIGHT, + STOP +} eDirection; + +class StepperLedGui +{ + public: + StepperLedGui(); + void MotorCallib(); + void MotorSteps(unsigned int uiStepCount); + void gotoPosition(unsigned int uiPosition); + private: + tState ReadDetector(); + void DrawLed(unsigned char ucLedNumber); + void SetLed(unsigned char ucLedNumber); + void ClearLeds(); + void LedStep(eDirection Direction); + void DrawMotor(); + tState eLedStates[LED_COUNT]; + unsigned int uiLedCounter; + unsigned int uiZeroPosition; + LCD_DISCO_F429ZI lcd; +}; + +#endif \ No newline at end of file