Skip to content

Commit 3ac01c5

Browse files
authored
Merge pull request #4 from Arekushi/patch-1.2
🔧 Code adjustments
2 parents 520a944 + 277f289 commit 3ac01c5

File tree

12 files changed

+333
-319
lines changed

12 files changed

+333
-319
lines changed
4 Bytes
Binary file not shown.

.pio/build/uno/firmware.elf

124 Bytes
Binary file not shown.

.pio/build/uno/firmware.hex

Lines changed: 264 additions & 263 deletions
Large diffs are not rendered by default.
4.08 KB
Binary file not shown.

.pio/build/uno/lib264/libStates.a

4.08 KB
Binary file not shown.
212 Bytes
Binary file not shown.

.pio/build/uno/libd31/libCar.a

272 Bytes
Binary file not shown.

.pio/build/uno/src/main.cpp.o

2.54 KB
Binary file not shown.

lib/Car/Car.cpp

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,48 @@
11
#include <Arduino.h>
22
#include <Car.h>
3-
43
#include <Ultra.h>
54
#include <StateMachine.h>
65

7-
/* Region Define */
8-
#define ultra_port1 7
9-
#define ultra_port2 6
10-
11-
const String infra_names[] = { "Left", "Center", "Right" };
12-
const int8_t infra_ports[] = { A0, A1, A2 };
6+
Car::Car(State<Car> *states[]) {
7+
setupInfraReds();
8+
setupEngines();
139

14-
const int8_t left_engine_ports[] = { 11, 10 };
15-
const int8_t right_engine_ports[] = { 5, 9 };
10+
memcpy(this->states, states, 10);
1611

17-
/* Methods */
18-
Car::Car(State<Car> *state) {
1912
ultra = new Ultra(ultra_port1, ultra_port2);
20-
machine = new StateMachine<Car>(*this, state);
13+
machine = new StateMachine<Car>(*this, this->states[0]);
14+
}
2115

22-
for(size_t i = 0; i < infra_names->length() - 1; i++) {
23-
infras[i] = new InfraRed(infra_names[i], infra_ports[i]);
16+
void Car::showSensors() {
17+
for(size_t i = 0; i < 3; i++) {
18+
infras[i]->show();
2419
}
2520

26-
for(size_t i = 0; i < 2; i++) {
27-
left_engine[i] = new Engine(left_engine_ports[i]);
28-
right_engine[i] = new Engine(right_engine_ports[i]);
29-
}
21+
ultra->show();
3022
}
3123

32-
void Car::goBack() {
33-
left_engine[0]->write(35);
34-
left_engine[1]->write(0);
24+
//#region [Moviment Methods]
25+
void Car::goBack(int8_t power) {
26+
left_engines[0]->write(power);
27+
left_engines[1]->write(LOW);
3528

36-
right_engine[0]->write(35);
37-
right_engine[1]->write(35);
29+
right_engines[0]->write(power);
30+
right_engines[1]->write(LOW);
3831
}
3932

40-
void Car::goForward() {
41-
left_engine[0]->write(0);
42-
left_engine[1]->write(35);
33+
void Car::goForward(int8_t power) {
34+
left_engines[0]->write(LOW);
35+
left_engines[1]->write(power);
4336

44-
right_engine[0]->write(0);
45-
right_engine[1]->write(35);
37+
right_engines[0]->write(LOW);
38+
right_engines[1]->write(power);
4639
}
4740

4841
void Car::stop() {
49-
left_engine[0]->write(35);
50-
left_engine[1]->write(35);
42+
left_engines[0]->write(HIGH);
43+
left_engines[1]->write(HIGH);
5144

52-
right_engine[0]->write(35);
53-
right_engine[1]->write(35);
54-
}
55-
56-
void Car::showSensors() {
57-
for(size_t i = 0; i < infra_names->length() - 1; i++) {
58-
infras[i]->show();
59-
}
60-
61-
ultra->show();
45+
right_engines[0]->write(HIGH);
46+
right_engines[1]->write(HIGH);
6247
}
48+
//#endregion

lib/Car/Car.h

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,45 @@
77
#include <InfraRed.h>
88
#include <Engine.h>
99

10+
#define ultra_port1 7
11+
#define ultra_port2 6
12+
13+
const String infras_names[] = { "Left", "Center", "Right" };
14+
const int8_t infras_ports[] = { A0, A1, A2 };
15+
16+
const int8_t left_engines_ports[] = { 11, 10 };
17+
const int8_t right_engines_ports[] = { 5, 9 };
18+
1019
class Car {
1120

1221
public:
22+
InfraRed *infras[3];
23+
Engine *left_engines[2];
24+
Engine *right_engines[2];
25+
State<Car> *states[10];
1326
Ultra *ultra;
1427
StateMachine<Car> *machine;
15-
InfraRed *infras[3];
16-
17-
Engine *left_engine[2];
18-
Engine *right_engine[2];
1928

20-
Car(State<Car> *state);
21-
void stop();
22-
void goForward();
23-
void goBack();
29+
Car(State<Car> *states[]);
2430
void showSensors();
31+
32+
void goForward(int8_t power);
33+
void goBack(int8_t power);
34+
void stop();
35+
36+
private:
37+
void setupInfraReds() {
38+
for(size_t i = 0; i < 3; i++) {
39+
infras[i] = new InfraRed(infras_names[i], infras_ports[i]);
40+
}
41+
}
42+
43+
void setupEngines() {
44+
for(size_t i = 0; i < 2; i++) {
45+
left_engines[i] = new Engine(left_engines_ports[i]);
46+
right_engines[i] = new Engine(right_engines_ports[i]);
47+
}
48+
}
2549
};
2650

2751
#endif

0 commit comments

Comments
 (0)