1////////////////////////////////////////////////////////////////////////////////
2// //
3// This software is supplied under the terms of a license agreement or //
4// nondisclosure agreement with Mitov Software and may not be copied //
5// or disclosed except in accordance with the terms of that agreement. //
6// Copyright(c) 2002-2016 Mitov Software. All Rights Reserved. //
7// //
8////////////////////////////////////////////////////////////////////////////////
9
10#ifndef _MITOV_STEPPER_h
11#define _MITOV_STEPPER_h
12
13#include <Mitov.h>
14
15namespace Mitov
16{
17
18/*
19 enum StepperMotorType =
20 {
21 smtDirectional,
22 smtFull2Wire,
23 smtFull3Wire,
24 smtFull4Wire,
25 smtHalf3Wire,
26 smtHalf4Wire
27 };
28*/
29 const unsigned char C_StepperBits4[] =
30 {
31 0b1100,
32 0b0110,
33 0b0011,
34 0b1001
35 };
36
37 const unsigned char C_StepperBits8[] =
38 {
39 0b1000,
40 0b1100,
41 0b0100,
42 0b0110,
43 0b0010,
44 0b0011,
45 0b0001,
46 0b1001
47 };
48//---------------------------------------------------------------------------
49 template <int C_NUMOUTPINS> class BasicStepperMotor : public OpenWire::Component
50 {
51 public:
52 OpenWire::SourcePin OutputPins[ C_NUMOUTPINS ];
53 OpenWire::SinkPin StepInputPin;
54
55 protected:
56 unsigned long FLastTime = 0;
57 uint8_t FStep : 4;
58
59 public:
60 bool Enabled : 1;
61 bool Reversed : 1;
62 float StepsPerSecond = 300.f;
63
64 protected:
65 virtual void UpdatePins() = 0;
66
67 void DoStepReceive( void *_Data )
68 {
69 Step();
70 }
71
72 void SetPinsValue( unsigned char AValue )
73 {
74 for( int i = 0; i < C_NUMOUTPINS; ++i )
75 {
76 bool APinValue = ( AValue & 1 );
77 OutputPins[ i ].Notify( &APinValue );
78 AValue >>= 1;
79 }
80 }
81
82 virtual void SystemLoopBegin( unsigned long currentMicros ) override
83 {
84 if( !Enabled )
85 FLastTime = currentMicros;
86
87 else
88 {
89 unsigned long APeriod = ( 1000000 / abs( StepsPerSecond )) + 0.5;
90 if( ( currentMicros - FLastTime ) >= APeriod )
91 {
92 FLastTime += APeriod;
93 if(( StepsPerSecond > 0 ) ^ Reversed )
94 ++FStep;
95
96 else
97 --FStep;
98
99 UpdatePins();
100 }
101 }
102 }
103
104 public:
105 BasicStepperMotor() :
106 Enabled( true ),
107 Reversed( true ),
108 FStep( 0 )
109 {
110 StepInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&BasicStepperMotor::DoStepReceive );
111 }
112
113 public:
114 void Step()
115 {
116 if( Reversed ) //^ ( StepsPerSecond > 0 ) )
117 --FStep;
118
119 else
120 ++FStep;
121
122 UpdatePins();
123 }
124 };
125//---------------------------------------------------------------------------
126 class StepperMotorDirectional : public BasicStepperMotor<2>
127 {
128 };
129//---------------------------------------------------------------------------
130 template <int C_NUMOUTPINS> class BasicStepperMotorHalfStep : public BasicStepperMotor<C_NUMOUTPINS>
131 {
132 public:
133 bool HalfStep = true;
134
135 };
136//---------------------------------------------------------------------------
137 class StepperMotor2Wire : public BasicStepperMotorHalfStep<2>
138 {
139 };
140//---------------------------------------------------------------------------
141 class StepperMotor4Wire : public BasicStepperMotorHalfStep<4>
142 {
143 protected:
144 virtual void UpdatePins() override
145 {
146 unsigned char AValue = HalfStep ? C_StepperBits8[ FStep & 0b0111 ] : C_StepperBits4[ FStep & 0b0011 ];
147 SetPinsValue( AValue );
148 }
149
150 };
151//---------------------------------------------------------------------------
152}
153
154#endif