initial commit. start platformio project

This commit is contained in:
len0rd 2025-01-10 09:14:27 -05:00
commit b1e2aecfff
8 changed files with 314 additions and 0 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

10
.vscode/extensions.json vendored Normal file
View file

@ -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"
]
}

39
include/README Normal file
View file

@ -0,0 +1,39 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

46
lib/README Normal file
View file

@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

33
lib/nvm/src/NvmEeprom.cpp Normal file
View file

@ -0,0 +1,33 @@
/** \File NvmEeprom.cpp
* \copyright (c) 2025 len0rd
* \date 2025-01-09
*/
#include "NvmEeprom.hpp"
#include <cstddef>
#include "EEPROM.h"
bool JoystickAxisCalibration::valid() const {
return min < mid < max;
}
void writeNvm(const NonVolatileMemory &nvm)
{
for (size_t ii = 0; ii < sizeof(NonVolatileMemory); ii++)
{
EEPROM.write(ii, nvm.rawBytes[ii]);
}
}
void writeCalibration(const JoystickCalibration &cal) {
NonVolatileMemory nvm = {};
nvm.joystickCal = cal;
writeNvm(nvm);
}
void readNvm(NonVolatileMemory &nvm)
{
for (size_t ii = 0; ii < sizeof(NonVolatileMemory); ii++)
{
nvm.rawBytes[ii] = EEPROM.read(ii);
}
}

44
lib/nvm/src/NvmEeprom.hpp Normal file
View file

@ -0,0 +1,44 @@
/** \File NvmEeprom.hpp
* \copyright (c) 2025 len0rd
* \date 2025-01-09
*
* Very basic non-volatile memory handler to save and retrieve data
* needed by the controller
*/
#ifndef __NVMEEPROM_H__
#define __NVMEEPROM_H__
#include <cstdint>
struct JoystickAxisCalibration
{
/// Minimum value axis could be
uint16_t min;
/// average center point of axis
uint16_t mid;
/// max value axis could be
uint16_t max;
/// Check if the data in this struct makes sense
/// Returns true if data might be valid, otherwise false
bool valid() const;
} __attribute__((packed));
struct JoystickCalibration
{
JoystickAxisCalibration x;
JoystickAxisCalibration y;
} __attribute__((packed));
union NonVolatileMemory
{
JoystickCalibration joystickCal;
uint8_t rawBytes[sizeof(JoystickCalibration)] = {};
};
/// @brief Write the provided struct to EEPROM
void writeNvm(const NonVolatileMemory &nvm);
void writeCalibration(const JoystickCalibration&cal);
void readNvm(NonVolatileMemory &nvm);
#endif /* __NVMEEPROM_H__ */

15
platformio.ini Normal file
View file

@ -0,0 +1,15 @@
; 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
[env:uno_r4_wifi]
platform = renesas-ra
board = uno_r4_wifi
framework = arduino
lib_deps = palladinomarco/AlignedJoy@^1.0.1

122
src/main.cpp Normal file
View file

@ -0,0 +1,122 @@
#include <Arduino.h>
#include <AlignedJoy.h>
#include "NvmEeprom.hpp"
// joystick calibration
#define TIME_CAL_1 2000
#define TIME_CAL_2 3000
// static objects
AlignedJoy joystick_1(0, 1); /// X on A0, Y on A1
void runJoystickCalibrationRoutine(AlignedJoy &joy)
{
// JOYSTICK CALIBRATION
/*
* Start the joystick calibration in the center position. Use this method only if the calibration of the axles is desired (axesCalibration).
*/
// CALIBRATION 1
Serial.print("Center Joystick Calibration \n\nLeave the joystick in the centre position during the calibration\ntime which is set to ");
Serial.print(TIME_CAL_1);
Serial.println(" milliseconds.");
delay(5000);
Serial.println("Calibration started!");
delay(500);
/*
* for calibrate the joystick center point use "centerCalibration" method; objectname.centerCalibration(uint16_t timeOfCal).
* "timeOfCal" is the calibration time in milliseconds
*/
joy.middleCalibration(TIME_CAL_1);
Serial.println("Joystick Centered!");
delay(2000);
// CALIBRATION 2
/*
* Calibration of the axes at the extreme points (min end max for each axis).
* It is recommended to rotate the joystick in a circular way along its circumference throughout
* the calibration time.
*/
Serial.print("Start axes Calibration \n\nRotate the joystick in a circular way along its circumference\nthroughout the calibration time (");
Serial.print(TIME_CAL_2);
Serial.println(" milliseconds).");
delay(5000);
Serial.println("Calibration started!");
delay(500);
/*
* for calibrate the joystick axes points use "axesCalibration" method (bool type); objectname.axesCalibration(uint16_t timeOfCal).
* "timeOfCal" is the calibration time in milliseconds.
*/
if (joy.axesCalibration(TIME_CAL_2))
{
Serial.println("Calibration succesfully!!");
}
else
{
Serial.println("Calibration failed!!");
}
// Print all points calibrated
/*
* You can use these values to save them to eeprom memory. In this way you will avoid requiring the joystick calibration at each boot time.
* To set the parameters read by eeprom you have to use the "setCalibratedPoint" method.
* If your project does not require the re-calibration of the joystick then you can make a sketch like this only to display the calibrated
* values to set them in the final project using the "setCalibratedPoint" method.
* To get the calibrated point values use getCalibratedPoint(axis_t axis, point_t point).
* The parameters: the labels of the "axis_t" shall be X and Y; the labels of "point_t" is MIN, MID and MAX.
*/
Serial.print("X min -> ");
Serial.print(joy.getCalibratedPoint(X, MIN));
Serial.print(" | center -> ");
Serial.print(joy.getCalibratedPoint(X, MID));
Serial.print(" | max -> ");
Serial.println(joy.getCalibratedPoint(X, MAX));
Serial.print("Y min -> ");
Serial.print(joy.getCalibratedPoint(Y, MIN));
Serial.print(" | center -> ");
Serial.print(joy.getCalibratedPoint(Y, MID));
Serial.print(" | max -> ");
Serial.println(joy.getCalibratedPoint(Y, MAX));
// save values to NVM
JoystickCalibration cal = {};
cal.x.min = joy.getCalibratedPoint(X, MIN);
cal.x.mid = joy.getCalibratedPoint(X, MID);
cal.x.max = joy.getCalibratedPoint(X, MAX);
cal.y.min = joy.getCalibratedPoint(Y, MIN);
cal.y.mid = joy.getCalibratedPoint(Y, MID);
cal.y.max = joy.getCalibratedPoint(Y, MAX);
writeCalibration(cal);
}
void setup()
{
// SERIAL INITIALIZE
Serial.begin(9600);
while (!Serial)
{
}
// retrieve joystic calibration values or run calibration and save results
NonVolatileMemory nvm = {};
bool calibrationInvalid = true;
do {
readNvm(nvm);
calibrationInvalid = !nvm.joystickCal.x.valid() || !nvm.joystickCal.y.valid();
if (calibrationInvalid) {
runJoystickCalibrationRoutine(joystick_1);
}
} while (calibrationInvalid);
}
void loop()
{
// print joystick axes value
Serial.print("joystick_1 X -> ");
Serial.print(joystick_1.read(X));
Serial.print(" | Y -> ");
Serial.println(joystick_1.read(Y));
delay(500);
}