-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBarrelSpinner.cpp
More file actions
84 lines (69 loc) · 2.15 KB
/
Copy pathBarrelSpinner.cpp
File metadata and controls
84 lines (69 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
BarrelSpinner.h - Library for manualy moving an specific stepper motor to the right or to the left
Created by Blindle, 22 Sept, 2017.
Released into the public domain.
*/
#include "Arduino.h"
#include "BarrelSpinner.h"
#include "MultiplexorHandler.h"
#define PARSE_TO_INT(character) ((int)character - 48)
const char MOVE_BARREL_CHARACTER = 'd';
const int STEPS_PER_MOVE = 100;
const char LEFT_CHARACTER = 'L';
const char RIGHT_CHARACTER = 'R';
const int LEFT_DIRECTION = LOW;
const int RIGHT_DIRECTION = HIGH;
const int DEFAULT_DIRECTION = RIGHT_DIRECTION;
const int MULTIPLEXOR_DATA_BY_MOTOR_NUMBER[8] = {
0b10000000,
0b01000000,
0b00100000,
0b00010000,
0b00001000,
0b00000100,
0b00000010,
0b00000001,
};
BarrelSpinner::BarrelSpinner(MultiplexorHandler &multiplexorHandler, int directionPin) : _multiplexorHandler(multiplexorHandler)
{
_directionPin = directionPin;
_lastDirection = DEFAULT_DIRECTION;
pinMode(_directionPin, OUTPUT);
digitalWrite(_directionPin, DEFAULT_DIRECTION);
}
bool BarrelSpinner::hasToMoveBarrel(String word)
{
return word[0] == MOVE_BARREL_CHARACTER;
}
/* expected shape of word when moving a barrel --> dL1:
* d: is the character that Raspberry send us in order to move a barrel.
* L/R: is the direction that we want to spin.
* [0, 1, 2, ..., 7]: is the motor number, this goes from 0 to 8.
*/
void BarrelSpinner::moveBarrel(String word)
{
int direction = getDirection(word[1]);
int motorNumber = PARSE_TO_INT(word[2]);
moveMotor(motorNumber, direction);
setDirection(DEFAULT_DIRECTION);
}
int BarrelSpinner::getDirection(char directionCharacter)
{
return directionCharacter == LEFT_CHARACTER ? LEFT_DIRECTION : RIGHT_DIRECTION;
}
void BarrelSpinner::moveMotor(int motorNumber, int direction)
{
setDirection(direction);
for (int side = 0; side < STEPS_PER_MOVE; side++)
{
_multiplexorHandler.sendMultiplexorData(MULTIPLEXOR_DATA_BY_MOTOR_NUMBER[motorNumber]);
}
}
void BarrelSpinner::setDirection(int direction)
{
if (_lastDirection != direction)
{
digitalWrite(_directionPin, direction);
_lastDirection = direction;
}
}