Skip to content

Commit

Permalink
make cpp version work
Browse files Browse the repository at this point in the history
  • Loading branch information
emilybache committed Jun 1, 2024
1 parent 7900c9c commit c2b326f
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 13 deletions.
2 changes: 2 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ add_subdirectory(test-catch2)
add_subdirectory(test-doctest)
add_subdirectory(test-gtest)

add_executable(main main.cpp)
target_link_libraries(main src)
22 changes: 22 additions & 0 deletions cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "Stepper.h"
#include <unistd.h>

int main(int argc, char *argv[]) {

// create an instance of the stepper class with suitable pins set up
Stepper stepper(8, 10, 9, 11);

// configure the stepper motor speed in RPM
stepper.setSpeed(10);

// move the motor the number of steps indicated by val
uint8_t val = 42;
stepper.move_clockwise(val);

// wait for the stepper motor to finish moving
while(stepper.isRunning()) {
sleep(1);
}

}

4 changes: 3 additions & 1 deletion cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
set(SRC_LIB_NAME src)
set(SOURCES sample.cpp)
set(SOURCES
Stepper.cpp
Stepper.h)
add_library(${SRC_LIB_NAME} ${SOURCES})
target_sources(${SRC_LIB_NAME} PRIVATE ${SOURCES})
target_include_directories(${SRC_LIB_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
22 changes: 22 additions & 0 deletions cpp/src/Stepper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@


#include "Stepper.h"

void Stepper::setSpeed(uint8_t speed) {
_speed = speed;
}

void Stepper::move_clockwise(uint8_t steps) {
// logic to activate the relevant pins is omitted in this exercise
}

void Stepper::move_anticlockwise(uint8_t steps) {
// logic to activate the relevant pins is omitted in this exercise
}

bool Stepper::isRunning() {
// logic to read the pins and determine this omitted in this exercise
return false;
}


42 changes: 42 additions & 0 deletions cpp/src/Stepper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef STEPPER_H
#define STEPPER_H

#include <cstdint>

// the number of steps to rotate a complete circle of 360 degrees
#define STEPS 2048

// the maximum speed of the motor in RPM
#define MAX_SPEED = 15

/*
* This class is a placeholder for a real object that would be able to
* control an electric stepper motor.
*/
class Stepper {
public:
explicit Stepper(
const uint8_t pin1 = 8,
const uint8_t pin2 = 9,
const uint8_t pin3 = 10,
const uint8_t pin4 = 11): _speed(10) {
};

// Set the speed of the motor in RPM. Maximum is defined above.
void setSpeed(uint8_t);

// Move the indicated number of steps. The size of a step is defined above.
void move_clockwise(uint8_t steps);

void move_anticlockwise(uint8_t steps);

// Whether the motor is currently moving.
// Motors that are moving should not be given new instructions.
bool isRunning();

private:
int _speed;
};


#endif //STEPPER_H
4 changes: 0 additions & 4 deletions cpp/src/sample.cpp

This file was deleted.

6 changes: 0 additions & 6 deletions cpp/src/sample.h

This file was deleted.

2 changes: 0 additions & 2 deletions cpp/test-gtest/sample_gtest.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include <gtest/gtest.h>

#include "sample.h"

using namespace std;

TEST(SampleSuite, SampleTest) {
Expand Down

0 comments on commit c2b326f

Please sign in to comment.