Skip to content

Commit

Permalink
refactor(source code): Refactor complete source code using clang-form…
Browse files Browse the repository at this point in the history
…at (#196)

chore(source code): Add automatic source code formatting ruleset (clang-format) (#196)
  • Loading branch information
Slider0007 authored Dec 2, 2024
1 parent cb962e2 commit ab3ddc6
Show file tree
Hide file tree
Showing 103 changed files with 6,585 additions and 5,454 deletions.
48 changes: 48 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignConsecutiveMacros:
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
AlignFunctionPointers: false
PadOperators: true
AlignTrailingComments: true
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
BreakAfterReturnType: Automatic
BraceWrapping:
AfterCaseLabel: false
AfterClass: true
AfterControlStatement: Never
AfterEnum: false
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakBeforeBraces: Custom
ColumnLimit: 140
#IndentAccessModifiers: true # If activated struct content is indented twice
IndentCaseLabels: true
IndentWidth: 4
InsertBraces: true
InsertNewlineAtEOF: true
MaxEmptyLinesToKeep: 2
PenaltyBreakAssignment : 60
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 50
PenaltyBreakOpenParenthesis: 60
PenaltyReturnTypeOnItsOwnLine: 200
SortIncludes: Never
35 changes: 35 additions & 0 deletions code/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,38 @@ in `<path>/.platformio/packages`.
esp-coredump info_corefile --gdb <path to tool-xtensa-esp-elf-gdb/bin/xtensa-esp32-elf-gdb.exe> --rom-elf esp32_rev0_rom.elf --core-format raw --core firmware_ESP32CAM_coredump-elf.bin firmware.elf
```
---
## Source Code Style Guide
| Type | Style | Example
|--------------------|----------------------|-----
| Classes | Pascal Case | `ClassName`
| Structs | Pascal Case | `StructName`
| Functions | Camel Case | `callFunction1`
| Variables | Camel Case | `testVariable1`
| Constants | Screaming Snake Case | `#define DEFINITION_1`
## Automatic Source Code Formatting
### Configuration
#### Pre-Condition
- Development environment has an automatic formatter function (e.g. VSCode)
- Formatting rule file (`.clang-format`) needs to be available in project root folder
- Every developer needs to use defined formatting rules to avoid unnecessary style changes
#### VSCode development environment
- No extention necessary (Using VSCode default formatter which is able to handle clang format)
- The formatting is applied automatically whenever pasting or saving the file by adding the following content to project specific `settings.json` file located in project subfolder `.vscode`.
```
"editor.formatOnSave": false,
"editor.formatOnPaste": false,
"[cpp]": {
"editor.formatOnSave": true,
"editor.formatOnPaste": true
}
```
- With this settings it only applies per project and is enabled only for the language C++ (cpp, h files), but could also be configured globally.
- Formatting exlusion: Formating of file `defines.h` is disabled (`// clang-format off`) to keep better readability (nested PPDirectives)
### Formating rules (clang-format)
- Formatting rule file: [.clang-format](../.clang-format)
- [Online Configurator](https://clang-format-configurator.site/)
355 changes: 192 additions & 163 deletions code/components/camera_ctrl/ClassControlCamera.cpp

Large diffs are not rendered by default.

118 changes: 59 additions & 59 deletions code/components/camera_ctrl/ClassControlCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,67 +19,67 @@ typedef struct {

class ClassControlCamera
{
protected:
CfgData::SectionTakeImage::Camera paramCameraInternal;
CfgData::SectionTakeImage::Flashlight paramFlashlightInternal;
bool cameraInitSuccessful;
uint16_t sensorFrameSizeWidth, sensorFrameSizeHeight;
uint16_t outputFrameSizeWidth, outputFrameSizeHeight;

bool demoMode;
std::vector<std::string> demoFiles;

void setStatusLed(bool status);
bool loadNextDemoImage(camera_fb_t *_fb);

public:
ClassControlCamera();
~ClassControlCamera();
void powerResetCamera();
esp_err_t initCam();
esp_err_t deinitCam();

bool testCamera(void);
void printCamInfo(void);
void printCamConfig(void);

esp_err_t setCameraParameter(const CfgData::SectionTakeImage::Camera *paramCamera);
void setCameraFrequency(int _frequency);
void setImageQuality(int _qual);
void setImageSize(int _zoomFactor, int _zoomOffsetX, int _zoomOffsetY);
bool setImageManipulation(int _brightness, int _contrast, int _saturation, int _sharpness, int _exposureControlMode,
int _autoExposureLevel, int _manualExposureValue, int _gainControlMode, int _manualGainValue,
int _specialEffect, bool _mirror, bool _flip);
bool setMirrorFlip(bool _mirror, bool _flip);

bool getCameraInitSuccessful();
camera_model_t getCamModel(void);
std::string getCamType(void);
std::string getCamPID(void);
std::string getCamVersion(void);
int getCamFrequencyMhz(void);
void getOutputFrameSize(int &width, int &height);

esp_err_t captureToBasisImage(CImageBasis *_Image);
esp_err_t captureToFile(std::string _nm);
esp_err_t captureToHTTP(httpd_req_t *_req);
esp_err_t captureToStream(httpd_req_t *_req, bool _flashlightOn);

#ifdef GPIO_FLASHLIGHT_DEFAULT_USE_PWM
void ledcInitFlashlightDefault(void);
#endif
esp_err_t setFlashlightParameter(const CfgData::SectionTakeImage::Flashlight *paramFlashlight);
void setFlashIntensity(int _flashIntensity);
void setFlashTime(int _flashTime);
int getFlashTime();
void setFlashlight(bool _status);

void enableDemoMode(void);
void disableDemoMode(void);
void freeDemoMemoryOnly();
protected:
CfgData::SectionTakeImage::Camera paramCameraInternal;
CfgData::SectionTakeImage::Flashlight paramFlashlightInternal;
bool cameraInitSuccessful;
uint16_t sensorFrameSizeWidth, sensorFrameSizeHeight;
uint16_t outputFrameSizeWidth, outputFrameSizeHeight;

bool demoMode;
std::vector<std::string> demoFiles;

void setStatusLed(bool status);
bool loadNextDemoImage(camera_fb_t *_fb);

public:
ClassControlCamera();
~ClassControlCamera();
void powerResetCamera();
esp_err_t initCam();
esp_err_t deinitCam();

bool testCamera(void);
void printCamInfo(void);
void printCamConfig(void);

esp_err_t setCameraParameter(const CfgData::SectionTakeImage::Camera *paramCamera);
void setCameraFrequency(int _frequency);
void setImageQuality(int _qual);
void setImageSize(int _zoomFactor, int _zoomOffsetX, int _zoomOffsetY);
bool setImageManipulation(int _brightness, int _contrast, int _saturation, int _sharpness, int _exposureControlMode,
int _autoExposureLevel, int _manualExposureValue, int _gainControlMode, int _manualGainValue,
int _specialEffect, bool _mirror, bool _flip);
bool setMirrorFlip(bool _mirror, bool _flip);

bool getCameraInitSuccessful();
camera_model_t getCamModel(void);
std::string getCamType(void);
std::string getCamPID(void);
std::string getCamVersion(void);
int getCamFrequencyMhz(void);
void getOutputFrameSize(int &width, int &height);

esp_err_t captureToBasisImage(CImageBasis *_Image);
esp_err_t captureToFile(std::string _nm);
esp_err_t captureToHTTP(httpd_req_t *_req);
esp_err_t captureToStream(httpd_req_t *_req, bool _flashlightOn);

#ifdef GPIO_FLASHLIGHT_DEFAULT_USE_PWM
void ledcInitFlashlightDefault(void);
#endif // GPIO_FLASHLIGHT_DEFAULT_USE_PWM
esp_err_t setFlashlightParameter(const CfgData::SectionTakeImage::Flashlight *paramFlashlight);
void setFlashIntensity(int _flashIntensity);
void setFlashTime(int _flashTime);
int getFlashTime();
void setFlashlight(bool _status);

void enableDemoMode(void);
void disableDemoMode(void);
void freeDemoMemoryOnly();
};


extern ClassControlCamera cameraCtrl;

#endif
#endif // CLASSCONTROLCAMERA_H
139 changes: 61 additions & 78 deletions code/components/camera_ctrl/ov2640_sharpness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,108 +5,90 @@

#define OV2640_MAXLEVEL_SHARPNESS 6

const static uint8_t OV2640_SHARPNESS_AUTO[]=
{
0xFF, 0x00, 0xff,
0x92, 0x01, 0xff,
0x93, 0x20, 0x20,
0x00, 0x00, 0x00
const static uint8_t OV2640_SHARPNESS_AUTO[] = {
0xFF, 0x00, 0xFF, //
0x92, 0x01, 0xFF, //
0x93, 0x20, 0x20, //
0x00, 0x00, 0x00 //
};

const static uint8_t OV2640_SHARPNESS_MANUAL[]=
{
0xFF, 0x00, 0xff,
0x92, 0x01, 0xff,
0x93, 0x00, 0x20,
0x00, 0x00, 0x00
const static uint8_t OV2640_SHARPNESS_MANUAL[] = {
0xFF, 0x00, 0xFF, //
0x92, 0x01, 0xFF, //
0x93, 0x00, 0x20, //
0x00, 0x00, 0x00 //
};

const static uint8_t OV2640_SHARPNESS_LEVEL0[]=
{
0xFF, 0x00, 0xff,
0x92, 0x01, 0xff,
0x93, 0xc0, 0x1f,
0x00, 0x00, 0x00
const static uint8_t OV2640_SHARPNESS_LEVEL0[] = {
0xFF, 0x00, 0xFF, //
0x92, 0x01, 0xFF, //
0x93, 0xC0, 0x1F, //
0x00, 0x00, 0x00 //
};

const static uint8_t OV2640_SHARPNESS_LEVEL1[]=
{
0xFF, 0x00, 0xff,
0x92, 0x01, 0xff,
0x93, 0xc1, 0x1f,
0x00, 0x00, 0x00
const static uint8_t OV2640_SHARPNESS_LEVEL1[] = {
0xFF, 0x00, 0xFF, //
0x92, 0x01, 0xFF, //
0x93, 0xC1, 0x1F, //
0x00, 0x00, 0x00 //
};

const static uint8_t OV2640_SHARPNESS_LEVEL2[]=
{
0xFF, 0x00, 0xff,
0x92, 0x01, 0xff,
0x93, 0xc2, 0x1f,
0x00, 0x00, 0x00
const static uint8_t OV2640_SHARPNESS_LEVEL2[] = {
0xFF, 0x00, 0xFF, //
0x92, 0x01, 0xFF, //
0x93, 0xC2, 0x1F, //
0x00, 0x00, 0x00 //
};

const static uint8_t OV2640_SHARPNESS_LEVEL3[]=
{
0xFF, 0x00, 0xff,
0x92, 0x01, 0xff,
0x93, 0xc4, 0x1f,
0x00, 0x00, 0x00
const static uint8_t OV2640_SHARPNESS_LEVEL3[] = {
0xFF, 0x00, 0xFF, //
0x92, 0x01, 0xFF, //
0x93, 0xC4, 0x1F, //
0x00, 0x00, 0x00 //
};

const static uint8_t OV2640_SHARPNESS_LEVEL4[]=
{
0xFF, 0x00, 0xff,
0x92, 0x01, 0xff,
0x93, 0xc8, 0x1f,
0x00, 0x00, 0x00
const static uint8_t OV2640_SHARPNESS_LEVEL4[] = {
0xFF, 0x00, 0xFF, //
0x92, 0x01, 0xFF, //
0x93, 0xC8, 0x1F, //
0x00, 0x00, 0x00 //
};

const static uint8_t OV2640_SHARPNESS_LEVEL5[]=
{
0xFF, 0x00, 0xff,
0x92, 0x01, 0xff,
0x93, 0xd0, 0x1f,
0x00, 0x00, 0x00
const static uint8_t OV2640_SHARPNESS_LEVEL5[] = {
0xFF, 0x00, 0xFF, //
0x92, 0x01, 0xFF, //
0x93, 0xD0, 0x1F, //
0x00, 0x00, 0x00 //
};

const static uint8_t OV2640_SHARPNESS_LEVEL6[]=
{
0xFF, 0x00, 0xff,
0x92, 0x01, 0xff,
0x93, 0xdf, 0x1f,
0x00, 0x00, 0x00
const static uint8_t OV2640_SHARPNESS_LEVEL6[] = {
0xFF, 0x00, 0xFF, //
0x92, 0x01, 0xFF, //
0x93, 0xDF, 0x1F, //
0x00, 0x00, 0x00 //
};

const static uint8_t *OV2640_SETTING_SHARPNESS[]=
{
OV2640_SHARPNESS_LEVEL0, // -3 sharpness
OV2640_SHARPNESS_LEVEL1,
OV2640_SHARPNESS_LEVEL2,
OV2640_SHARPNESS_LEVEL3,
OV2640_SHARPNESS_LEVEL4,
OV2640_SHARPNESS_LEVEL5,
OV2640_SHARPNESS_LEVEL6 // +3 sharpness
const static uint8_t *OV2640_SETTING_SHARPNESS[] = {
OV2640_SHARPNESS_LEVEL0, // -3
OV2640_SHARPNESS_LEVEL1, //
OV2640_SHARPNESS_LEVEL2, //
OV2640_SHARPNESS_LEVEL3, //
OV2640_SHARPNESS_LEVEL4, //
OV2640_SHARPNESS_LEVEL5, //
OV2640_SHARPNESS_LEVEL6 // +3
};


static bool table_mask_write(sensor_t *_sensor, const uint8_t* _ptab)
static bool table_mask_write(sensor_t *_sensor, const uint8_t *_ptab)
{
uint8_t address;
uint8_t value;
uint8_t orgval;
uint8_t mask;
const uint8_t *pdata = _ptab;

if (pdata == NULL)
if (pdata == NULL) {
return false;
}

while (1)
{
while (1) {
address = *pdata++;
value = *pdata++;
mask = *pdata++;
if ((address == 0) && (value == 0) && (mask == 0))
if ((address == 0) && (value == 0) && (mask == 0)) {
break;
}
_sensor->set_reg(_sensor, address, mask, value);
}

Expand All @@ -122,8 +104,9 @@ bool ov2640_enable_auto_sharpness(sensor_t *_sensor)

bool ov2640_set_sharpness(sensor_t *_sensor, int _sharpness)
{
if ((_sharpness < -3) || (_sharpness > OV2640_MAXLEVEL_SHARPNESS - 3))
if ((_sharpness < -3) || (_sharpness > OV2640_MAXLEVEL_SHARPNESS - 3)) {
return false;
}

bool retval = false;
retval = table_mask_write(_sensor, OV2640_SHARPNESS_MANUAL);
Expand Down
Loading

0 comments on commit ab3ddc6

Please sign in to comment.