Skip to content

Commit

Permalink
c version of exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
emilybache committed Jun 1, 2024
1 parent c2b326f commit 929faad
Show file tree
Hide file tree
Showing 19 changed files with 110 additions and 478 deletions.
3 changes: 3 additions & 0 deletions c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ add_subdirectory(test-cmocka)
add_subdirectory(test-catch2)
add_subdirectory(test-cgreen)
add_subdirectory(test-gtest)

add_executable(main main.c)
target_link_libraries(main src)
22 changes: 22 additions & 0 deletions c/main.c
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
struct Stepper_t* stepper = stepper_create();

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

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

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

}

4 changes: 3 additions & 1 deletion c/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.c)
set(SOURCES
stepper.h
stepper.c)
add_library(${SRC_LIB_NAME} ${SOURCES})
target_sources(${SRC_LIB_NAME} PRIVATE ${SOURCES})
target_include_directories(${SRC_LIB_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
5 changes: 0 additions & 5 deletions c/src/sample.c

This file was deleted.

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

This file was deleted.

35 changes: 35 additions & 0 deletions c/src/stepper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "stepper.h"

#include <stdlib.h>

struct Stepper_t *stepper_create() {
int8_t pin1 = 8;
int8_t pin2 = 9;
int8_t pin3 = 10;
int8_t pin4 = 1;
return stepper_create_with_pins(pin1, pin2, pin3, pin4);
}

struct Stepper_t * stepper_create_with_pins(const int8_t pin1, const int8_t pin2, const int8_t pin3, const int8_t pin4) {
struct Stepper_t* stepper = malloc(sizeof(*stepper));
// logic related to the pins is omitted in this exercise
stepper->speed = 10;
return stepper;
}

void setSpeed(struct Stepper_t *stepper, int8_t speed) {
stepper->speed = speed;
}

void move_clockwise(struct Stepper_t *stepper, int16_t steps) {
// logic to activate the relevant pins is omitted in this exercise
}

void move_anticlockwise(struct Stepper_t *stepper, int16_t steps) {
// logic to activate the relevant pins is omitted in this exercise
}

bool isRunning(struct Stepper_t *stepper) {
// logic to read the pins and determine this omitted in this exercise
return false;
}
40 changes: 40 additions & 0 deletions c/src/stepper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef STEPPER_H
#define STEPPER_H

#include <stdbool.h>
#include <unistd.h>

// 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 struct is a placeholder for a real object that would be able to
* control an electric stepper motor.
*/
struct Stepper_t {
int8_t speed;
};

struct Stepper_t *stepper_create_with_pins(
int8_t pin1,
int8_t pin2,
int8_t pin3,
int8_t pin4);

struct Stepper_t *stepper_create();

// Set the speed of the motor in RPM. Maximum is defined above.
void setSpeed(struct Stepper_t* stepper, int8_t speed);

// Move the indicated number of steps. The size of a step is defined above.
void move_clockwise(struct Stepper_t* stepper, int16_t steps);
void move_anticlockwise(struct Stepper_t* stepper, int16_t steps);

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

#endif //STEPPER_H
4 changes: 2 additions & 2 deletions c/test-catch2/sample_catch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

extern "C"
{
#include "sample.h"
#include "stepper.h"
}


TEST_CASE ("Sample") {
SECTION("sample section") {
REQUIRE(42 == add(1,2));
REQUIRE(42 == 0);
}
}

Expand Down
2 changes: 0 additions & 2 deletions c/test-cmocka/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,3 @@ endfunction()


add_subdirectory(simple_test)
add_subdirectory(sample_test)
add_subdirectory(uptime)
6 changes: 0 additions & 6 deletions c/test-cmocka/sample_test/CMakeLists.txt

This file was deleted.

20 changes: 0 additions & 20 deletions c/test-cmocka/sample_test/sample_test.c

This file was deleted.

26 changes: 0 additions & 26 deletions c/test-cmocka/uptime/CMakeLists.txt

This file was deleted.

6 changes: 0 additions & 6 deletions c/test-cmocka/uptime/README.md

This file was deleted.

78 changes: 0 additions & 78 deletions c/test-cmocka/uptime/proc_uptime.c

This file was deleted.

22 changes: 0 additions & 22 deletions c/test-cmocka/uptime/proc_uptime.h

This file was deleted.

Loading

0 comments on commit 929faad

Please sign in to comment.