Finalny program mbed2
Dependencies: LCD_DISCO_F429ZI mbed TS_DISCO_F429ZI BSP_DISCO_F429ZI
Revision 0:33ff53112cc9, committed 2020-06-09
- Comitter:
- azmuth_sd
- Date:
- Tue Jun 09 08:24:40 2020 +0000
- Commit message:
- Ostatni program drugiej czesci mbed
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BSP_DISCO_F429ZI.lib Tue Jun 09 08:24:40 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/teams/ST/code/BSP_DISCO_F429ZI/#53d9067a4feb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LCD_DISCO_F429ZI.lib Tue Jun 09 08:24:40 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/teams/ST/code/LCD_DISCO_F429ZI/#dc55a068bc1a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TS_DISCO_F429ZI.lib Tue Jun 09 08:24:40 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/teams/ST/code/TS_DISCO_F429ZI/#4f8b6df8e235
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/command_decoder.cpp Tue Jun 09 08:24:40 2020 +0000 @@ -0,0 +1,120 @@ +#include "command_decoder.h" +#include <string.h> +#include <stdlib.h> + +#define TERMINATOR 0x20 + +enum Result { rOK, rERROR }; + +struct Keyword asKeywordList[MAX_KEYWORD_NR] = { + {ID, "id"}, + {CALIB,"calib"}, + {GOTO, "goto" }, + {STEP, "step" } +}; + +unsigned char Token::ucFindTokensInString (char *pcString){ + + unsigned char ucCharacterCounter = 0, ucCurrentCharacter; + enum CharacterType eCharacterType = DELIMITER; + unsigned char ucTokenCounter = 0; + + for(ucCharacterCounter = 0; ;ucCharacterCounter++){ + + ucCurrentCharacter = pcString[ucCharacterCounter]; + + switch(eCharacterType){ + + case TOKEN: + if(ucCurrentCharacter == NULL){ + return ucTokenCounter; + } + else if(ucTokenCounter == MAX_TOKEN_NR){ + return ucTokenCounter; + } + else if(ucCurrentCharacter == ' '){ + eCharacterType = DELIMITER; + } + break; + + case DELIMITER: + if(ucCurrentCharacter == NULL){ + return ucTokenCounter; + } + else if(ucCurrentCharacter == ' '){ + eCharacterType = DELIMITER; + } + else{ + asToken[ucTokenCounter].uValue.pcString = (pcString + ucCharacterCounter); + ucTokenCounter++; + eCharacterType = TOKEN; + } + break; + } + } +} + +enum Result Token::eSringToKeyword (char pcStr[],enum KeywordCode *peKeywordCode){ + + unsigned char ucKeywordCounter; + + for(ucKeywordCounter = 0; ucKeywordCounter < MAX_KEYWORD_NR; ucKeywordCounter++){ + + if(0 == strcmp(asKeywordList[ucKeywordCounter].cString, pcStr)){ + *peKeywordCode= asKeywordList[ucKeywordCounter].eCode; + return rOK; + } + } + return rERROR; +} + +enum Result Token::eHexStringToUInt(char pcStr[],unsigned int *puiValue){ + + if((pcStr[0] != '0') || (pcStr[1] != 'x') || (pcStr[2] == NULL)) { + return rERROR; + } + + pcStr[0] = pcStr[3]; + pcStr[1] = pcStr[4]; + pcStr[2] = NULL; + + *puiValue = atoi(pcStr); + + return rOK; +} + +void Token::ReplaceCharactersInString(char pcString[],char cOldChar,char cNewChar){ + + unsigned char ucCharacterCounter; + + for( ucCharacterCounter = 0; pcString[ucCharacterCounter] != NULL; ucCharacterCounter++ ) { + if( pcString[ucCharacterCounter] == cOldChar) pcString[ucCharacterCounter] = cNewChar; + } +} + +void Token::DecodeTokens(void){ + + unsigned char ucTokenCounter; + struct tToken * pToken; + + for(ucTokenCounter = 0; ucTokenCounter < ucTokenNr; ucTokenCounter++){ + pToken = &asToken[ucTokenCounter]; + + if(rOK == eSringToKeyword(pToken->uValue.pcString, &pToken->uValue.eKeyword)){ + pToken->eType = KEYWORD; + } + else if(rOK == eHexStringToUInt(pToken->uValue.pcString, &pToken->uValue.uiNumber)){ + pToken->eType = NUMBER; + } + else{ + pToken->eType = STRING; + } + } +} + +void Token::DecodeMsg(char *pcString){ + + ucTokenNr = ucFindTokensInString(pcString); + ReplaceCharactersInString(pcString, ' ', NULL); + DecodeTokens(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/command_decoder.h Tue Jun 09 08:24:40 2020 +0000 @@ -0,0 +1,48 @@ +#ifndef COMMAND_DECODER_H +#define COMMAND_DECODER_H + +#define MAX_TOKEN_NR 2 +#define MAX_KEYWORD_STRING_LTH 10 +#define MAX_KEYWORD_NR 4 + + +enum CharacterType {TOKEN, DELIMITER}; +enum TokenType {KEYWORD, NUMBER, STRING}; +enum KeywordCode { ID, CALIB, GOTO, STEP }; + +struct Keyword { + + enum KeywordCode eCode; + char cString[MAX_KEYWORD_STRING_LTH + 1]; +}; + + +union TokenValue { + + enum KeywordCode eKeyword; // jezeli KEYWORD + unsigned int uiNumber; // jezeli NUMBER + char * pcString; // jezeli STRING +}; + +struct tToken { + + enum TokenType eType; // KEYWORD, NUMBER, STRING + union TokenValue uValue; // enum, unsigned int, char* +}; + +class Token { + +private: + unsigned char ucFindTokensInString (char *pcString); + void DecodeTokens(void); + enum Result eSringToKeyword (char pcStr[],enum KeywordCode *peKeywordCode); + enum Result eHexStringToUInt(char pcStr[],unsigned int *puiValue); + void ReplaceCharactersInString(char pcString[],char cOldChar,char cNewChar); +public: + struct tToken asToken[MAX_TOKEN_NR]; + unsigned char ucTokenNr; + void DecodeMsg(char *pcString); +}; + + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/led.cpp Tue Jun 09 08:24:40 2020 +0000 @@ -0,0 +1,76 @@ +#include "led.h" + +Led::Led (void){ + ucServoPos = 0; + unsigned char ucCurrentLed; + lcd.SetFont(&Font24); + lcd.Clear(LCD_COLOR_WHITE); + + lcd.SetTextColor(LCD_COLOR_BLACK); + for( ucCurrentLed = 1; ucCurrentLed < 5; ucCurrentLed++ ){ + lcd.FillCircle((ucCurrentLed*50)-5,260,20); + } +} + +void Led::On(unsigned char ucLedIndeks){ + + unsigned char ucCurrentLed; + float posX, posY; + lcd.SetTextColor(LCD_COLOR_BLACK); + lcd.FillCircle(120,120,90); + + lcd.SetTextColor(LCD_COLOR_WHITE); + lcd.FillCircle(120, 120, 10); + + for( ucCurrentLed = 1; ucCurrentLed < 5; ucCurrentLed++ ) + { + lcd.FillCircle((ucCurrentLed*50)-5,260,16); + } + + posX = 120 + (90 * sin((0.131 * ucServoPos))); + posY = 120 + (90 * cos((0.131 * ucServoPos))); + lcd.DrawLine(120, 120, posX, posY); + + lcd.SetTextColor(LCD_COLOR_BLUE); + lcd.FillCircle(((ucLedIndeks+1)*50)-5,260,16); + +} + +void Led::Step (enum LedDirection Direction){ + + static unsigned int uiLedCounter = 0; + + if(Direction == LEFT) + { + uiLedCounter++; + } + else + { + uiLedCounter--; + } + On(uiLedCounter%4); +} + +void Led::StepLeft(void){ + if(ucServoPos < 47) + { + ucServoPos++; + } + else + { + ucServoPos = 0; + } + Step(LEFT); +} + +void Led::StepRight(void){ + if(ucServoPos > 0) + { + ucServoPos--; + } + else + { + ucServoPos = 47; + } + Step(RIGHT); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/led.h Tue Jun 09 08:24:40 2020 +0000 @@ -0,0 +1,22 @@ +#ifndef LED_H +#define LED_H + +#include "LCD_DISCO_F429ZI.h" + +enum LedDirection {LEFT, RIGHT}; + +class Led { + +private: + LCD_DISCO_F429ZI lcd; + char ucServoPos; + void Step (enum LedDirection Direction); + void On(unsigned char ucLedIndeks); + +public: + Led( void ); + void StepLeft(void); + void StepRight(void); +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Jun 09 08:24:40 2020 +0000 @@ -0,0 +1,89 @@ +#include "mbed.h" +#include <string.h> +#include "servo.h" +#include "command_decoder.h" + +DigitalOut led_green(LED1); +DigitalOut led_red(LED2); + +Serial uart(USBTX, USBRX); + +Servo myServo; +Ticker myTimer; +Token myToken; + +unsigned char UART_puts(char * pcString, unsigned char ucSize) +{ + unsigned char ucLoopCounter; + if( *(pcString + ucSize - 1) != NULL ) return 1; + for(ucLoopCounter = 0; ucLoopCounter < (ucSize-1); ucLoopCounter++) { + uart.putc(pcString[ucLoopCounter]); + } + uart.putc('\r'); + return 0; +} + +unsigned char UART_gets(char *pcDestString, unsigned char ucSize) +{ + unsigned char ucLoopCounter; + for(ucLoopCounter = 0; ucLoopCounter < (ucSize-1); ucLoopCounter++) { + pcDestString[ucLoopCounter] = uart.getc(); + if(pcDestString[ucLoopCounter] == '\r') { + pcDestString[ucLoopCounter] = NULL; + return 0; + } + } + return 1; +} + +void ServoUpdate (void){ + myServo.Automat(); +} + +int main() +{ + uart.baud(9600); + char acUartBuffor[15]; + + myTimer.attach(&ServoUpdate, 0.2); + + myServo.Callib(); + + while(1) { + + if (uart.readable()){ + UART_gets(acUartBuffor,15); + myToken.DecodeMsg(acUartBuffor); + + if( (myToken.ucTokenNr != 0) && (KEYWORD == myToken.asToken[0].eType) ) + { + switch(myToken.asToken[0].uValue.eKeyword) + { + case ID: + UART_puts("ID 1234\n", 9); + break; + + case CALIB: + myServo.Callib(); + UART_puts("ok\n", 4); + break; + + case GOTO: + UART_puts("ok\n", 4); + myServo.GoTo(myToken.asToken[1].uValue.uiNumber); + break; + + case STEP: + UART_puts("ok\n", 4); + myServo.StepRight(myToken.asToken[1].uValue.uiNumber); + break; + } + } + else if((myToken.ucTokenNr != 0)){ + UART_puts("unknowncommand\n", 16); + } + + } + + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Jun 09 08:24:40 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/servo.cpp Tue Jun 09 08:24:40 2020 +0000 @@ -0,0 +1,67 @@ +#include "servo.h" + +InterruptIn user_button(USER_BUTTON); + +void Servo::Automat(){ + + switch(eState){ + + case CALLIB: + if(user_button == 0){ + myLed.StepLeft(); + } + else{ + uiCurrentPosition = 0; + uiDesiredPosition = 0; + eState = IDLE; + } + break; + + case IDLE: + + if(uiCurrentPosition != uiDesiredPosition){ + eState = IN_PROGRESS; + } + break; + + case IN_PROGRESS: + if(uiCurrentPosition < uiDesiredPosition){ + uiCurrentPosition++; + myLed.StepRight(); + } + else if(uiCurrentPosition > uiDesiredPosition){ + uiCurrentPosition--; + myLed.StepLeft(); + } + else{ + eState = IDLE; + } + break; + } +} + +void Servo::Callib(void){ + + eState = CALLIB; + while(eState != IDLE){ + wait_ms(1); + } +} + +void Servo::GoTo(unsigned int uiPosition){ + + uiDesiredPosition = uiPosition; + eState = IN_PROGRESS; + while(eState != IDLE){ + wait_ms(1); + } +} + +void Servo::StepRight(unsigned int uiSteps){ + + uiDesiredPosition += uiSteps; + eState = IN_PROGRESS; + while(eState != IDLE){ + wait_ms(1); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/servo.h Tue Jun 09 08:24:40 2020 +0000 @@ -0,0 +1,24 @@ +#ifndef SERVO_H +#define SERVO_H +#include "led.h" + +enum ServoState {CALLIB, IDLE, IN_PROGRESS}; +enum DetectorState {ACTIVE, INACTIVE}; + +class Servo { + +private: + Led myLed; + enum DetectorState eReadDetector(); + enum ServoState eState; + unsigned int uiCurrentPosition; + unsigned int uiDesiredPosition; + +public: + void Automat(); + void Callib(void); + void GoTo(unsigned int uiPosition); + void StepRight(unsigned int uiSteps); +}; + +#endif