Skip to content

Commit

Permalink
config on SD card - read/save, change meter resolution, add stats in …
Browse files Browse the repository at this point in the history
…lux_only mode (#39)

* add Measurement Resolution setting

* load settings on main_view enter, add peak lux stat

* very simple histogram

* add todo for later

* save/read settings to SD card

* fix config path and button rendering

* increase stack size

convert `flipper_format_*_hex` to `flipper_format_*_int32`

* proper `bh1750_set_mode` on app init

- move `bh1750_set_mode` after config is loaded
- replace hardcoded value with `app->config->measurement_resolution`

* add I2C addr selection, fix sensor init

single callback function that handles sensor mode and address setting

this requires oleksiikutuzov/flipperzero-BH1750#1 to be merged

* bump lib to origin

* inscreasing stack size to be safe

* Revert "Merge branch 'main' into advanced-mode"

This reverts commit 0c0d0b9655310dabb100512ad7bdf23073e9b9e7, reversing
changes made to 7998c32a6941aa1ead4f71d020575979cd139d36.

* Fix conflicts

* More files to the state of the main branch

* Fix formatting

* Update changelog

* Write sensor type to config, set address for MAX sensor

* Switching address for both sensors

* Use shortcut for appdata folder, update changelog

* Update license to current state

* Update docs

* Update screenshots

---------

Co-authored-by: Oleksii Kutuzov <8535871+oleksiikutuzov@users.noreply.github.com>
Co-authored-by: Oleksii Kutuzov <oleksii.kutuzov@icloud.com>
  • Loading branch information
3 people authored Jul 21, 2023
1 parent a476434 commit 8a1ccc4
Show file tree
Hide file tree
Showing 16 changed files with 569 additions and 24 deletions.
191 changes: 191 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
---
Language: Cpp
AccessModifierOffset: -4
AlignAfterOpenBracket: AlwaysBreak
AlignArrayOfStructures: None
AlignConsecutiveMacros: None
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: None
AlignEscapedNewlines: Left
AlignOperands: Align
AlignTrailingComments: false
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes
AttributeMacros:
- __capability
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: true
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: false
ColumnLimit: 99
CommentPragmas: '^ IWYU pragma:'
QualifierAlignment: Leave
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
PackConstructorInitializers: BinPack
BasedOnStyle: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false
AllowAllConstructorInitializersOnNextLine: true
FixNamespaceComments: false
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IfMacros:
- KJ_IF_MAYBE
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '.*'
Priority: 1
SortPriority: 0
CaseSensitive: false
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 1
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseLabels: false
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentRequires: false
IndentWidth: 4
IndentWrappedFunctionNames: true
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
LambdaBodyIndentation: Signature
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 4
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 10
PenaltyBreakBeforeFirstCallParameter: 30
PenaltyBreakComment: 10
PenaltyBreakFirstLessLess: 0
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 10
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 100
PenaltyReturnTypeOnItsOwnLine: 60
PenaltyIndentedWhitespace: 0
PointerAlignment: Left
PPIndentWidth: -1
ReferenceAlignment: Pointer
ReflowComments: false
RemoveBracesLLVM: false
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 1
SortIncludes: Never
SortJavaStaticImport: Before
SortUsingDeclarations: false
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: Never
SpaceBeforeParensOptions:
AfterControlStatements: false
AfterForeachMacros: false
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterIfMacros: false
AfterOverloadedOperator: false
BeforeNonEmptyParentheses: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: Never
SpacesInConditionalStatement: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
BitFieldColonSpacing: Both
Standard: c++03
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 4
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
- NS_SWIFT_NAME
- CF_SWIFT_NAME
...

Binary file added .flipcorg/gallery/gui_config_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .flipcorg/gallery/gui_lux_meter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ App(
requires=[
"gui",
],
stack_size=1 * 1024,
stack_size= 4 * 1024,
order=90,
fap_version=(1, 1),
fap_version=(1, 2),
fap_icon="lightmeter.png",
fap_category="GPIO",
fap_private_libs=[
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## v1.2

* Lux only screen now has statistics
* Settings are now stored on SD card
* You can choose the resolution (BH1750 only) and address for sensor

(thanks to @danielskowronski for contributing to this update)

## v1.1

Added support for MAX44009 sensor (thanks to @wosk)
Expand Down
106 changes: 106 additions & 0 deletions gui/scenes/lightmeter_scene_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,31 @@ static const char* sensor_type[] = {
[SENSOR_MAX44009] = "MAX44009",
};

static const char* measurement_resolution[] = {
[LOW_RES] = "Low",
[HIGH_RES] = "High",
[HIGH_RES2] = "High2",
};

static const char* device_addr_bh1750[] = {
[ADDR_LOW] = "0x23",
[ADDR_HIGH] = "0x5C",
};

static const char* device_addr_max44009[] = {
[ADDR_LOW] = "0x4A",
[ADDR_HIGH] = "0x4B",
};

enum LightMeterSubmenuIndex {
LightMeterSubmenuIndexISO,
LightMeterSubmenuIndexND,
LightMeterSubmenuIndexDome,
LightMeterSubmenuIndexBacklight,
LightMeterSubmenuIndexLuxMeter,
LightMeterSubmenuIndexSensorType,
LightMeterSubmenuIndexMeasurementResolution,
LightMeterSubmenuIndexI2CAddress,
LightMeterSubmenuIndexHelp,
LightMeterSubmenuIndexAbout,
};
Expand Down Expand Up @@ -133,6 +151,60 @@ static void lux_only_cb(VariableItem* item) {
lightmeter_app_set_config(app, config);
}

static void measurement_resolution_cb(VariableItem* item) {
LightMeterApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);

variable_item_set_current_value_text(item, measurement_resolution[index]);

LightMeterConfig* config = app->config;
config->measurement_resolution = index;
lightmeter_app_set_config(app, config);

lightmeter_app_i2c_init_sensor(app);
}

static void update_item_addr(LightMeterApp* app) {
VariableItem* item = app->var_item_addr;
switch(app->config->sensor_type) {
case SENSOR_BH1750:
variable_item_set_current_value_index(item, app->config->device_addr);
variable_item_set_current_value_text(item, device_addr_bh1750[app->config->device_addr]);
break;
case SENSOR_MAX44009:
variable_item_set_current_value_index(item, app->config->device_addr);
variable_item_set_current_value_text(item, device_addr_max44009[app->config->device_addr]);
break;
default:
FURI_LOG_E(TAG, "Invalid sensor type %ld", app->config->sensor_type);
return;
}
}

static void device_addr_cb(VariableItem* item) {
LightMeterApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);

switch(app->config->sensor_type) {
case SENSOR_BH1750:
variable_item_set_current_value_text(item, device_addr_bh1750[index]);
break;
case SENSOR_MAX44009:
variable_item_set_current_value_text(item, device_addr_max44009[index]);
break;
default:
FURI_LOG_E(TAG, "Invalid sensor type %ld", app->config->sensor_type);
return;
}
// variable_item_set_current_value_text(item, device_addr[index]);

LightMeterConfig* config = app->config;
config->device_addr = index;
lightmeter_app_set_config(app, config);

lightmeter_app_i2c_init_sensor(app);
}

static void sensor_type_cb(VariableItem* item) {
LightMeterApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
Expand All @@ -141,6 +213,9 @@ static void sensor_type_cb(VariableItem* item) {

LightMeterConfig* config = app->config;
config->sensor_type = index;

update_item_addr(app);

lightmeter_app_set_config(app, config);
}

Expand Down Expand Up @@ -195,6 +270,36 @@ void lightmeter_scene_config_on_enter(void* context) {
variable_item_set_current_value_index(item, config->sensor_type);
variable_item_set_current_value_text(item, sensor_type[config->sensor_type]);

item = variable_item_list_add(
var_item_list,
"Resolution",
COUNT_OF(measurement_resolution),
measurement_resolution_cb,
app);
variable_item_set_current_value_index(item, config->measurement_resolution);
variable_item_set_current_value_text(
item, measurement_resolution[config->measurement_resolution]);

switch(config->sensor_type) {
case SENSOR_BH1750:
item = variable_item_list_add(
var_item_list, "I2C address", COUNT_OF(device_addr_bh1750), device_addr_cb, app);
variable_item_set_current_value_index(item, config->device_addr);
variable_item_set_current_value_text(item, device_addr_bh1750[config->device_addr]);
break;
case SENSOR_MAX44009:
item = variable_item_list_add(
var_item_list, "I2C address", COUNT_OF(device_addr_max44009), device_addr_cb, app);
variable_item_set_current_value_index(item, config->device_addr);
variable_item_set_current_value_text(item, device_addr_max44009[config->device_addr]);
break;
default:
FURI_LOG_E(TAG, "Invalid sensor type %ld", config->sensor_type);
return;
}
app->var_item_addr = item;
update_item_addr(app);

item = variable_item_list_add(var_item_list, "Help and Pinout", 0, NULL, NULL);
item = variable_item_list_add(var_item_list, "About", 0, NULL, NULL);

Expand Down Expand Up @@ -235,4 +340,5 @@ void lightmeter_scene_config_on_exit(void* context) {
main_view_set_nd(app->main_view, app->config->nd);
main_view_set_dome(app->main_view, app->config->dome);
main_view_set_lux_only(app->main_view, app->config->lux_only);
main_view_set_measurement_resolution(app->main_view, app->config->measurement_resolution);
}
8 changes: 7 additions & 1 deletion gui/scenes/lightmeter_scene_help.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ void lightmeter_scene_help_on_enter(void* context) {
temp_str = furi_string_alloc();
furi_string_printf(
temp_str,
"App works with BH1750 and MAX44409 ambient light sensor connected via I2C interface\n\n");
"App works with BH1750/MAX44009\nambient light sensor\nconnected via I2C interface\n\n");
furi_string_cat(temp_str, "\e#Pinout:\r\n");
furi_string_cat(
temp_str,
" VCC: 3.3V\r\n"
" GND: GND\r\n"
" SDA: 15 [C1]\r\n"
" SCL: 16 [C0]\r\n");
furi_string_cat(temp_str, "\r\n\e#Resolutions:\r\n");
furi_string_cat(
temp_str,
"Low: 4.0lx (16ms, 0-54k)\r\n"
"High: 1.0lx (120ms, 0-54k)\r\n"
"High2: 0.5lx (120ms, 0-27k)\r\n");

widget_add_text_scroll_element(app->widget, 0, 0, 128, 64, furi_string_get_cstr(temp_str));
furi_string_free(temp_str);
Expand Down
Loading

0 comments on commit 8a1ccc4

Please sign in to comment.