Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base firmware project #23

Merged
merged 2 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions carpincho_firmware/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
10 changes: 10 additions & 0 deletions carpincho_firmware/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
58 changes: 58 additions & 0 deletions carpincho_firmware/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# carpincho_firmware

This folder contains the firmware project for the low-level code that runs on the Arduino Uno (ATmega328) microcontroller.

## Getting Started

### Prerequisites

The firmware project is based on [PlatformIO IDE for VSCode](https://docs.platformio.org/en/latest/integration/ide/vscode.html). See [here](https://docs.platformio.org/en/latest/integration/ide/vscode.html#installation) for installation steps.

In case you are using Ubuntu/Debian, you will need to install the PlatformIO udev rules, find the required steps to do so [here](https://docs.platformio.org/en/stable/core/installation/udev-rules.html).

### Project Structure

```
.
├── platformio.ini # Project configuration file
├── include # Application header files
├── lib # Libraries
├── src # Application source code
└── test # Unit testing directory
├── test_desktop # Desktop (Linux) unit tests (GoogleTest used as framework)
└── test_embedded # Embedded (Arduino Uno) unit tests (Unity used as framework)
```

### Building

To build the application execute the following steps on the [PlatformIO Toolbar](https://docs.platformio.org/en/latest/integration/ide/vscode.html#platformio-toolbar):
* Select `uno` as the project environment.
* Click on the `Build` button.

### Installing & Running

To install and run the application execute the following steps on the [PlatformIO Toolbar](https://docs.platformio.org/en/latest/integration/ide/vscode.html#platformio-toolbar):
* Select `uno` as the project environment.
* Connect the Arduino Uno device.
* Click on the `Upload` button.

You should see that the built-in led starts to blink.

### Running tests

This project supports unit testing, both on desktop and embedded devices. The following unit testing frameworks are used:
* Desktop tests: [GoogleTest](https://google.github.io/googletest/).
* Embedded tests: [Unity](http://www.throwtheswitch.org/unity/).

To build and run the desktop tests execute the following steps on the [PlatformIO Toolbar](https://docs.platformio.org/en/latest/integration/ide/vscode.html#platformio-toolbar):
* Select `native` as the project environment.
* Click on the `Test` button.

To build and run the embedded tests execute the following steps on the [PlatformIO Toolbar](https://docs.platformio.org/en/latest/integration/ide/vscode.html#platformio-toolbar):
* Select `uno` as the project environment.
* Connect the Arduino Uno device.
* Click on the `Test` button.

## Wiring Diagram

TODO(jballoffet): Complete this section.
8 changes: 8 additions & 0 deletions carpincho_firmware/include/hw.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef CARPINCHOBOT_INCLUDE_HW_H_
#define CARPINCHOBOT_INCLUDE_HW_H_

#include <Arduino.h>

#define LED_PIN LED_BUILTIN

#endif // CARPINCHOBOT_INCLUDE_HW_H_
9 changes: 9 additions & 0 deletions carpincho_firmware/lib/foo/foo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <foo.h>

namespace carpinchobot {

int Foo::get() { return value_; }

void Foo::set(int value) { value_ = value; }

} // namespace carpinchobot
18 changes: 18 additions & 0 deletions carpincho_firmware/lib/foo/foo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef CARPINCHOBOT_FOO_H_
#define CARPINCHOBOT_FOO_H_

namespace carpinchobot {

class Foo {
public:
Foo() : value_(0) {}
int get();
void set(int value);

private:
int value_;
};

} // namespace carpinchobot

#endif // CARPINCHOBOT_FOO_H_
29 changes: 29 additions & 0 deletions carpincho_firmware/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

;
; Desktop platforms (Win, Mac, Linux, Raspberry Pi, etc)
; See https://platformio.org/platforms/native
;

[env:native]
platform = native
test_ignore = test_embedded
test_framework = googletest

;
; Embedded platforms
;

[env:uno]
platform = atmelavr
board = uno
framework = arduino
test_ignore = test_desktop
35 changes: 35 additions & 0 deletions carpincho_firmware/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <Arduino.h>
#include <foo.h>

#include "hw.h"

void blink(int times, int delay_ms) {
for (int i = 0; i < times; i++) {
digitalWrite(LED_PIN, HIGH);
delay(delay_ms);
digitalWrite(LED_PIN, LOW);
delay(delay_ms);
}
}

void setup() {
Serial.begin(9600);
Serial.println("Program running...");
pinMode(LED_PIN, OUTPUT);
}

void loop() {
carpinchobot::Foo foo;

foo.set(3);
Serial.print("Blinks: ");
Serial.println(foo.get());
blink(foo.get(), 200);
delay(2000);

foo.set(2);
Serial.print("Blinks: ");
Serial.println(foo.get());
blink(foo.get(), 200);
delay(2000);
}
22 changes: 22 additions & 0 deletions carpincho_firmware/test/test_desktop/foo_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <foo.h>
#include <gtest/gtest.h>

TEST(FooTest, ConstructOk) {
carpinchobot::Foo foo;
EXPECT_EQ(0, foo.get());
}

TEST(FooTest, SetValue) {
carpinchobot::Foo foo;
foo.set(1);
EXPECT_EQ(1, foo.get());
}

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
if (RUN_ALL_TESTS()) {
}

// Always return zero-code and allow PlatformIO to parse results.
return 0;
}
44 changes: 44 additions & 0 deletions carpincho_firmware/test/test_embedded/foo_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <Arduino.h>
#include <foo.h>
#include <unity.h>

void setUp(void) {
// Set stuff up here.
}

void tearDown(void) {
// Clean stuff up here.
}

void foo_test_construct_ok(void) {
carpinchobot::Foo foo;
TEST_ASSERT_EQUAL(0, foo.get());
}

void foo_test_set_value(void) {
carpinchobot::Foo foo;
foo.set(1);
TEST_ASSERT_EQUAL(1, foo.get());
}

void RUN_ALL_TESTS() {
UNITY_BEGIN();
RUN_TEST(foo_test_construct_ok);
RUN_TEST(foo_test_set_value);
UNITY_END();
}

void setup() {
// Wait for 2 secs in case board doesn't support software reset via
// Serial.DTR/RTS.
delay(2000);

RUN_ALL_TESTS();
}

void loop() {
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(500);
}