//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" #include #include "PACSDK.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; const int ROTARY_SWITH_UNIT = 400; const int ARROW_UNIT = 120; const int CIRCLE_UNIT = 150; const int NUMBER_UNIT = 175; const int ORIGIN_X = ROTARY_SWITH_UNIT / 2; const int ORIGIN_Y = ROTARY_SWITH_UNIT / 2; //arrowPoints = vertices of the arrow shape double arrowPointsX[] = {1, 0, 0, -1, -1, 0, 0}; double arrowPointsY[] = {0, -1, -0.5, -0.5, 0.5, 0.5, 1}; const int ARROW_POINT_NUM = 7; TLabel * lbNumbers[10]; //numbers of the rotary switch int CurrentRotaryID = -1; int previousRotaryID = -1; //used to prevent flash //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { double angle; int x, y; for (int i = 0; i < 10; i++) { angle = M_PI * 2 / 10 * i; //labels of number initialization lbNumbers[i] = new TLabel(this); x = (int)(cos(angle) * NUMBER_UNIT + ORIGIN_X); y = (int)(sin(angle) * NUMBER_UNIT + ORIGIN_Y); lbNumbers[i]->Left = x; lbNumbers[i]->Top = y; lbNumbers[i]->Caption = IntToStr(i); lbNumbers[i]->Font->Size = 16; lbNumbers[i]->Font->Style << fsBold; lbNumbers[i]->Font->Color = clWhite; lbNumbers[i]->Color = clBlack; lbNumbers[i]->Width = 20; lbNumbers[i]->Height = 20; lbNumbers[i]->Parent = Form1; } } //--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender) { previousRotaryID = CurrentRotaryID; CurrentRotaryID = pac_GetRotaryID(); if(CurrentRotaryID != previousRotaryID) //to prevent flash RotaryPaint(); } //--------------------------------------------------------------------------- DynamicArray rotate(double x[], double y[], int length, int graduation) { //vector rotation formula: // x2 = x1 * cos - y1 * sin //y2 = y1 * cos + x1 * sin double angle = M_PI * 2 / 10 * graduation; DynamicArray dbArray; dbArray.Length = length * 2; for(int i=0;i rotatedArrowPoints; rotatedArrowPoints.Length = ARROW_POINT_NUM * 2; int x, y; rotatedArrowPoints = rotate(arrowPointsX, arrowPointsY, ARROW_POINT_NUM, graduation); for (int i = 0; i < ARROW_POINT_NUM; i++) { TPoint pt; x = (int)(ARROW_UNIT * rotatedArrowPoints[2 * i] + ORIGIN_X); y = (int)(ARROW_UNIT * rotatedArrowPoints[2 * i + 1] + ORIGIN_Y); pt.x = x; pt.y = y; rotatedArrow[i] = pt; } } //--------------------------------------------------------------------------- void __fastcall TForm1::RotaryPaint() { TPoint arrowPt[ARROW_POINT_NUM]; Form1->Canvas->Brush->Color = clWhite; Form1->Canvas->Ellipse(ORIGIN_X - CIRCLE_UNIT, ORIGIN_Y - CIRCLE_UNIT, ORIGIN_X - CIRCLE_UNIT + CIRCLE_UNIT * 2, ORIGIN_Y - CIRCLE_UNIT + CIRCLE_UNIT * 2 ); CalculateArrowPoints(CurrentRotaryID, arrowPt); arrowPt[ARROW_POINT_NUM] = arrowPt[0]; //eight points with 8th = first Form1->Canvas->Brush->Color = clGray; Form1->Canvas->Polygon(arrowPt, ARROW_POINT_NUM); } //---------------------------------------------------------------------------