Skip to content

Commit

Permalink
Merge pull request #64 from thatliuser/cleanup
Browse files Browse the repository at this point in the history
Cleanup code and GitHub Actions
  • Loading branch information
thatliuser authored Jan 25, 2025
2 parents a1fdc54 + 8bf5cbf commit 926ae11
Show file tree
Hide file tree
Showing 30 changed files with 171 additions and 2,323 deletions.
12 changes: 11 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4, NamespaceIndentation: All, FixNamespaceComments: false }
BasedOnStyle: LLVM
UseTab: Never
IndentWidth: 4
TabWidth: 4
BreakBeforeBraces: Allman
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
ColumnLimit: 0
AccessModifierOffset: -4
NamespaceIndentation: All
FixNamespaceComments: false
18 changes: 18 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Compile check
on: [push]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Polyfill AWS certificates
run: |
cd components/ClientComponent/
touch ca.crt client.crt client.key aws.url
- name: Build ESP-IDF project
uses: espressif/esp-idf-ci-action@v1
with:
esp_idf_version: v5.3.1
target: esp32
16 changes: 16 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Format check
on: [push]

jobs:
format-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run clang-format
uses: DoozyX/[email protected]
with:
source: 'components main'
exclude: 'managed_components components/hx711'
clangFormatVersion: 18
style: file

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ components/ClientComponent/aws.url
components/ClientComponent/ca.crt
components/ClientComponent/client.crt
components/ClientComponent/client.key

dependencies.lock
sdkconfig*
!sdkconfig.defaults
16 changes: 10 additions & 6 deletions components/ClientComponent/Serialize.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
#include "Serialize.hpp"
#include <esp_mac.h>

cJSON* Client::serialize(float fullness, uint32_t usage, bool overflow, int32_t weight) {
cJSON *Client::serialize(float fullness, uint32_t usage, bool overflow, int32_t weight)
{
// Use the ESP32's MAC address as a UUID (since it is a UUID)
uint8_t mac_addr_arr[6];
uint64_t mac_addr;
if (esp_base_mac_addr_get(mac_addr_arr) != ESP_OK) {
if (esp_base_mac_addr_get(mac_addr_arr) != ESP_OK)
{
// TODO: Handle mac address being unset properly
mac_addr = 0x112233445566;
} else {
}
else
{
mac_addr =
((uint64_t)mac_addr_arr[5] << 0) |
((uint64_t)mac_addr_arr[4] << 8) |
((uint64_t)mac_addr_arr[5] << 0) |
((uint64_t)mac_addr_arr[4] << 8) |
((uint64_t)mac_addr_arr[3] << 16) |
((uint64_t)mac_addr_arr[2] << 24) |
((uint64_t)mac_addr_arr[1] << 32) |
((uint64_t)mac_addr_arr[0] << 40);
}
cJSON* root = cJSON_CreateObject();
cJSON *root = cJSON_CreateObject();
cJSON_AddNumberToObject(root, "bin_id", mac_addr);
cJSON_AddNumberToObject(root, "fullness", fullness);
cJSON_AddNumberToObject(root, "usage", usage);
Expand Down
2 changes: 1 addition & 1 deletion components/ClientComponent/include/Client.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CLIENT_HPP
#define CLIENT_HPP

#include <stdint.h>
#include <stddef.h>

namespace Client
{
Expand Down
8 changes: 4 additions & 4 deletions components/ClientComponent/include/Credentials.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
// To make sure compilation succeeds, ensure ca.crt, client.crt, aws.url and client.key exist!
// https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/build-system.html#embedding-binary-data
// AWS root CA certificate
extern const uint8_t AWS_CA_CRT[] asm("_binary_ca_crt_start");
extern const uint8_t AWS_CA_CRT[] asm("_binary_ca_crt_start");
// Device-local certificate
extern const uint8_t AWS_CLIENT_CRT[] asm("_binary_client_crt_start");
extern const uint8_t AWS_CLIENT_CRT[] asm("_binary_client_crt_start");
// Device-local private key
extern const uint8_t AWS_CLIENT_KEY[] asm("_binary_client_key_start");
extern const uint8_t AWS_CLIENT_KEY[] asm("_binary_client_key_start");
// URL of AWS endpoint
extern const uint8_t AWS_URL[] asm("_binary_aws_url_start");
extern const uint8_t AWS_URL[] asm("_binary_aws_url_start");

#endif
5 changes: 3 additions & 2 deletions components/ClientComponent/include/Serialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <cJSON.h>
#include <stdint.h>

namespace Client {
namespace Client
{
/*
* @brief Serialize data from sensors for API interaction.
*
Expand All @@ -19,7 +20,7 @@ namespace Client {
*
* @return cJSON* root of a JSON object containing sensor data. Make sure to free this after you are done using it.
*/
cJSON* serialize(float fullness, uint32_t usage, bool overflow, int32_t weight);
cJSON *serialize(float fullness, uint32_t usage, bool overflow, int32_t weight);
}

#endif
2 changes: 1 addition & 1 deletion components/FullnessComponent/include/FullnessMetric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

#include "IDistance.hpp"
#include <array>
#include <cstdint>
#include <cstddef>
#include <cstdint>

namespace Fullness
{
Expand Down
14 changes: 0 additions & 14 deletions components/TaskComponent/cameraTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ static camera_config_t camera_config = {
// CAM_PIN_PWDN
// .pin_reset = CAM_PIN_RESET,



.pin_pwdn = CAM_PIN_PWDN,
.pin_reset = CAM_PIN_RESET,
.pin_xclk = CAM_PIN_XCLK,
Expand Down Expand Up @@ -164,8 +162,6 @@ void buffer_to_string(uint8_t *buffer, size_t buffer_length, char *output, size_
// snprintf(output + pos, output_size - pos, "]");
}



static esp_err_t init_camera(void)
{
// initialize the camera
Expand Down Expand Up @@ -194,20 +190,15 @@ void CameraTask::taskFunction(void *task)
CameraTask *cameraTask = static_cast<CameraTask *>(task);
cameraTask->setup();
cameraTask->loop();

}

void CameraTask::setup()
{
}



void CameraTask::loop()
{



// xTaskCreatePinnedToCore(taskFunction, mName, mStackSize, this, mPriority, nullptr, 1);
gpio_reset_pin(flashPIN);
gpio_set_direction(flashPIN, GPIO_MODE_OUTPUT);
Expand Down Expand Up @@ -235,8 +226,6 @@ void CameraTask::loop()

Client::clientPublishStr("Started Camera");



sensor_t *s = esp_camera_sensor_get();
if (s != NULL)

Expand All @@ -250,8 +239,6 @@ void CameraTask::loop()
Client::clientPublishStr("Adjusted Camera");
}



camera_fb_t *fb = NULL;
while (1)

Expand Down Expand Up @@ -282,5 +269,4 @@ void CameraTask::loop()
}
vTaskDelay(33 / portTICK_PERIOD_MS);
}

}
2 changes: 1 addition & 1 deletion components/TaskComponent/clientTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ static const char *name = "clientTask";
static const int priority = 1;
static const uint32_t stackSize = 4096;

ClientTask::ClientTask(QueueHandle_t &messageQueue): Task(name, priority, stackSize)
ClientTask::ClientTask(QueueHandle_t &messageQueue) : Task(name, priority, stackSize)
{
}

Expand Down
1 change: 0 additions & 1 deletion components/TaskComponent/fullnessTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "FullnessMetric.hpp"
#include "esp_log.h"


using namespace Zotbins;

static const char *name = "fullnessTask";
Expand Down
6 changes: 3 additions & 3 deletions components/TaskComponent/include/cameraTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#ifndef CAMERA_TASK_HPP
#define CAMERA_TASK_HPP

#include "task.hpp"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "driver/gpio.h"
#include "freertos/task.h"
#include "task.hpp"

namespace Zotbins
{
Expand Down
3 changes: 1 addition & 2 deletions components/TaskComponent/include/clientTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#ifndef CLIENT_TASK_HPP
#define CLIENT_TASK_HPP

#include "task.hpp"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "task.hpp"

namespace Zotbins
{
Expand Down Expand Up @@ -57,7 +57,6 @@ namespace Zotbins
*
*/
void loop();

};
}

Expand Down
2 changes: 1 addition & 1 deletion components/TaskComponent/include/fullnessTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#ifndef FULLNESS_TASK_HPP
#define FULLNESS_TASK_HPP

#include "task.hpp"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "task.hpp"

namespace Zotbins
{
Expand Down
2 changes: 1 addition & 1 deletion components/TaskComponent/include/servoTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#ifndef SERVO_TASK_HPP
#define SERVO_TASK_HPP

#include "task.hpp"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "task.hpp"

namespace Zotbins
{
Expand Down
2 changes: 1 addition & 1 deletion components/TaskComponent/include/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace Zotbins
*
*/
const int mPriority;

/**
* @brief Task size of task
*
Expand Down
7 changes: 3 additions & 4 deletions components/TaskComponent/include/usageTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#ifndef USAGE_TASK_HPP
#define USAGE_TASK_HPP

#include "task.hpp"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "driver/gpio.h"
#include "freertos/task.h"
#include "task.hpp"

namespace Zotbins
{
Expand Down Expand Up @@ -67,7 +67,6 @@ namespace Zotbins
*/
QueueHandle_t &mMessageQueue;
gpio_num_t DETECT_PIN = GPIO_NUM_16;

};
}

Expand Down
2 changes: 1 addition & 1 deletion components/TaskComponent/include/weightTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#ifndef WEIGHT_TASK_HPP
#define WEIGHT_TASK_HPP

#include "task.hpp"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "task.hpp"

namespace Zotbins
{
Expand Down
29 changes: 15 additions & 14 deletions components/TaskComponent/servoTask.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include "servoTask.hpp"
#include "driver/mcpwm_prelude.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/mcpwm_prelude.h"

using namespace Zotbins;

// Please consult the datasheet of your servo before changing the following parameters
#define SERVO_MIN_PULSEWIDTH_US 500 // Minimum pulse width in microsecond
#define SERVO_MAX_PULSEWIDTH_US 2500 // Maximum pulse width in microsecond
#define SERVO_MIN_DEGREE -90 // Minimum angle
#define SERVO_MAX_DEGREE 90 // Maximum angle
#define SERVO_MAX_PULSEWIDTH_US 2500 // Maximum pulse width in microsecond
#define SERVO_MIN_DEGREE -90 // Minimum angle
#define SERVO_MAX_DEGREE 90 // Maximum angle

#define SERVO_PULSE_GPIO 15 // GPIO connects to the PWM signal line
#define SERVO_TIMEBASE_RESOLUTION_HZ 1000000 // 1MHz, 1us per tick
#define SERVO_TIMEBASE_PERIOD 20000 // 20000 ticks, 20ms
#define SERVO_PULSE_GPIO 15 // GPIO connects to the PWM signal line
#define SERVO_TIMEBASE_RESOLUTION_HZ 1000000 // 1MHz, 1us per tick
#define SERVO_TIMEBASE_PERIOD 20000 // 20000 ticks, 20ms

static inline uint32_t example_angle_to_compare(int angle)
{
Expand Down Expand Up @@ -49,7 +49,7 @@ void ServoTask::setup() // TODO: could refactor into setup later but there are a

void ServoTask::loop()
{
ESP_LOGI(name, "Create timer and operator");
ESP_LOGI(name, "Create timer and operator");
mcpwm_timer_handle_t timer = NULL;
mcpwm_timer_config_t timer_config = {
.group_id = 0,
Expand All @@ -74,8 +74,7 @@ void ServoTask::loop()
mcpwm_comparator_config_t comparator_config = {
.flags = {
.update_cmp_on_tez = true,
}
};
}};
ESP_ERROR_CHECK(mcpwm_new_comparator(oper, &comparator_config, &comparator));

mcpwm_gen_handle_t generator = NULL;
Expand All @@ -101,12 +100,14 @@ void ServoTask::loop()

int angle = 0;
int step = 2;
while (1) {
while (1)
{
ESP_LOGI(name, "Angle of rotation: %d", angle);
ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comparator, example_angle_to_compare(angle)));
//Add delay, since it takes time for servo to rotate, usually 200ms/60degree rotation under 5V power supply
// Add delay, since it takes time for servo to rotate, usually 200ms/60degree rotation under 5V power supply
vTaskDelay(pdMS_TO_TICKS(500));
if ((angle + step) > 60 || (angle + step) < -60) {
if ((angle + step) > 60 || (angle + step) < -60)
{
step *= -1;
}
angle += step;
Expand Down
Loading

0 comments on commit 926ae11

Please sign in to comment.