Raspberry Pi Weighting Control System
This project serves as a simple weighting control system, that was realized as a Bachelor Thesis
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
app_workspace Class Reference

One of the most importat classes in the whole project. Holds variables that define the state of the application and also provides logic for some of the variables held there. For example the hx711 controller is held and used from this class. More...

#include <app_workspace.h>

Public Member Functions

 app_workspace ()
 Construct a new app workspace object. More...
 
void init_labels ()
 This is called after localisation dictionary is loaded. After the dict is loaded arrays defined in app_workspace must be initialized so they can be used in GUI comboboxes. More...
 
int init_hx711_controller ()
 Initializes the hx711 controller. More...
 
void hx711_tare ()
 Function that performs taring on the hx711 controller. More...
 
void hx711_calibrate ()
 Function that performs calibration on the hx711 controller. More...
 
int hx711_measure ()
 Performs hx711 measuring with set values of either samples or timeout. The results is saved to the database and to userspace variable - measured. More...
 
int hx711_continuous_measure ()
 Starts hx711 measuring that will run until its stopped by user. More...
 
int open_db_connection ()
 Tries to open the database connection and init db_driver. More...
 
void set_err_screen_and_activate (const char *title, const char *label)
 Set the err screen and activate the screen on next frame. More...
 
void use_font_size (app_workspace_ns::font_size font_size)
 When this is called following gui elements will use selected font_size until next call of this or font pop. More...
 
app_workspace_ns::font_size get_font_size ()
 Get the font size. More...
 
int load_font_size (app_workspace_ns::font_size key, float size_px)
 Initializes font into the map. More...
 
int log_in_user_rfid (rfid_reader::rfid_tag *tag, bool loading_sub=false)
 Logs in user through scanned RFID tag. Initializes user_workspace if loading_sub is false. More...
 
int verify_user_cred ()
 Tests if user entered correct credentials. More...
 
void log_in_user_cred ()
 Logs in user using credentials. Credentials are gained from screen_1 buffers. More...
 
void login_subuser ()
 When a user is already logged in and their role is employee or admin, they can log in another user as their subuser. More...
 
void logout_subuser ()
 Logs out subuser. More...
 
void logoff_user ()
 Logs out user and subuser if logged in. More...
 
int register_new_user ()
 This function validets screen 6 buffers and if everything checks out then inserts new user into the DB. More...
 
std::vector< double > normalize_raw_values (std::vector< HX711::Value > &raw_vals)
 Takes raw values and normalizes them using HX offset and reference unit. Resulting values are in Grams! More...
 
std::vector< HX711::Value > hx711_values_samples (int samples)
 Reads the HX711 sensor until @samples is collected. Also starts new thread which is used to monitor ongoing measuring. More...
 
std::vector< HX711::Value > hx711_values_timeout (int seconds)
 Reads the HX711 sensor until @timeout is reached. Also starts new thread which is used to monitor ongoing measuring. More...
 
screen_managerget_scr_mgr ()
 Get the screen manager instance. This instance is kind of singleton. Is initalized only once on startup. More...
 
void set_scr_mgr (screen_manager *scr_mgr)
 Set the screen manager instancte. This should only be called once, when the application is starting up. More...
 
bool is_debug_screens ()
 Retruns flag is. More...
 
bool has_user ()
 Checks if user is logged in. This is determined by user_space being initialized and having the user_logged flag set. More...
 
user_workspaceget_userspace ()
 
void print_config ()
 
int test_hx_samples ()
 
int test_hx_timeout ()
 
bool get_kb_testing ()
 
void set_kb_testing (bool val)
 

Static Public Member Functions

static std::unique_ptr< app_workspace > & get_instance ()
 Get the instance app_workspace which is a singleton. More...
 

Public Attributes

std::unique_ptr< app_workspace_ns::kb_input_statekb_in_state
 Input state holder. More...
 
std::unique_ptr< custom_hx711hx711_controller
 HX711 controller. This is an instance of custom_hx711 which extends library's AdvancedHX711. More...
 
std::unique_ptr< db_driverdb_conn
 Database driver holder. More...
 
bool refresh_current_screen = false
 When this is set to true, elements of current screen will be reinitialized for next frame. More...
 
bool capslock_flag = false
 Flags used during text input. More...
 
bool lshift_flag = false
 
std::unique_ptr< app_configmain_config = std::make_unique<app_config>()
 Application config loaded from app_config.conf (main config file). More...
 
bool user_logged = false
 Flag to indicate if a user is logged in. More...
 
std::unique_ptr< user_workspaceuserspace
 User's workspace. Serves similar function as app_workspace but for user data. More...
 
bool hx_measuring = false
 
bool hx_continuous = false
 
bool hx_not_finished_yet = false
 
std::chrono::time_point< std::chrono::high_resolution_clock > m_start
 
std::chrono::time_point< std::chrono::high_resolution_clock > m_end
 
int selected_hx_measure_opt = 0
 
int selected_hx_result_unit = 0
 
std::unique_ptr< measurementobserved_measurement
 This variable is used to observe measuring. More...
 
std::map< app_workspace_ns::font_size, ImFont * > loaded_fonts
 Map of loaded fonts. More...
 
int font_push_cnt = 0
 Whenever a font is used, it is pushed to the font stack of ImGui library. All pushed fonts need to be popped before the end of the frame to prevent application crash. More...
 
char usr_name [DEF_BUFF_SIZE]
 
char usr_passwd [DEF_BUFF_SIZE_SMALL]
 
char subusr_name [DEF_BUFF_SIZE]
 
const char * hx_measure_opts [S2_MEASURE_OTPS_CNT]
 
int samples_in = S2_MIN_SAMPLES_IN
 
int timeout_in = S2_MIN_TIMEOUT_IN
 
int selected_measuring = 0
 
const char * s3_filter_opt_labels [S3_FILTER_OPT_CNT]
 
int s3_selected_filter = 0
 
unsigned long s3_filter_number = 0
 
int selected_unit_option = 1
 
const char * unit_sel_labels [UNIT_SEL_CNT]
 
const HX711::Mass::Unit unit_selection [UNIT_SEL_CNT]
 
int s5_screen_6_indicator = -1
 
int s5_success_indicator = -1
 
int s6_ref_mass_in = 0
 
int s6_samples_in = S6_MIN_SAMPLES_IN
 
int s6_calib_ref_unit = 0
 
int s6_calib_offset = 0
 
int s6_selected_unit_option = 0
 
const char * s6_unit_sel_labels [S6_UNIT_SEL_CNT]
 
bool s6_calibration_finished = false
 
bool s6_tare_complete = false
 
std::string s6_ref_mass_str
 
HX711::Value zero_value = 0
 
HX711::Value ref_unit = 0
 
bool s6_rfid_indicator = false
 
std::unique_ptr< rfid_reader::rfid_tags6_rfid_tag
 
int s6_selected_role = 0
 
int s6_selected_month = 0
 
const char * s6_role_labels [S6_ROLE_CNT]
 
const char * s6_month_labels [S6_MONTH_CNT]
 
char s6_name_in [DEF_BUFF_SIZE] = {0}
 
char s6_uname_in [DEF_BUFF_SIZE_SMALL] = {0}
 
char s6_passwd_in [DEF_BUFF_SIZE_SMALL] = {0}
 
char s6_desc_in [DEF_BUFF_SIZE_EXTRA] = {0}
 
char s6_lastname_in [DEF_BUFF_SIZE_SMALL] = {0}
 
int s6_year_in = 2022
 
int s6_day_in = 1
 
int int_test = 0
 
float float_test = 0.0f
 
char text_test [DEF_BUFF_SIZE]
 
char pass_test [DEF_BUFF_SIZE_SMALL]
 
int kb_delay_ms = 0
 
int kb_delay_us = 0
 
int kb_ev_delay_ms = 0
 
int kb_ev_delay_us = 0
 
bool test_hx_testing = false
 
std::pair< long, std::string > test_hx_samples_times [HX_TEST_CONST_IT_COUNT+HX_TEST_INC_IT_COUNT]
 
std::pair< long, std::string > test_hx_timeout_collected [HX_TEST_CONST_IT_COUNT+HX_TEST_INC_IT_COUNT]
 

Detailed Description

One of the most importat classes in the whole project. Holds variables that define the state of the application and also provides logic for some of the variables held there. For example the hx711 controller is held and used from this class.

Definition at line 159 of file app_workspace.h.

Constructor & Destructor Documentation

◆ app_workspace()

app_workspace::app_workspace ( )

Construct a new app workspace object.

One of the most importat classes in the whole project. Holds variables that define the state of the application and also provides logic for some of the variables held there. For example the hx711 controller is held and used from this class.

Definition at line 58 of file app_workspace.cpp.

58  {
59 }

Member Function Documentation

◆ get_font_size()

app_workspace_ns::font_size app_workspace::get_font_size ( )

Get the font size.

Returns
app_workspace_ns::font_size enum value

Definition at line 316 of file app_workspace.cpp.

316  {
317  return this->cur_font_size;
318 }

◆ get_instance()

std::unique_ptr< app_workspace > & app_workspace::get_instance ( )
static

Get the instance app_workspace which is a singleton.

Returns
std::unique_ptr<app_workspace>& app_workspace pointer ref.

Definition at line 45 of file app_workspace.cpp.

45  {
46  if (!instance.get()) {
47  spdlog::debug("app_workspace.cpp - Instantiating app_workspace");
48  instance = std::unique_ptr<app_workspace>(new app_workspace());
49  }
50  return instance;
51 }
app_workspace()
Construct a new app workspace object.

◆ get_kb_testing()

bool app_workspace::get_kb_testing ( )

Gets if keyboard testing is happening. This is a wrapper for thread secure function

Definition at line 831 of file app_workspace.cpp.

831  {
833 }
bool get_kb_testing_inner()
Definition: keyboard.cpp:216

◆ get_scr_mgr()

screen_manager * app_workspace::get_scr_mgr ( )

Get the screen manager instance. This instance is kind of singleton. Is initalized only once on startup.

Returns
screen_manager* pointer

Definition at line 286 of file app_workspace.cpp.

286  {
287  return this->scr_mgr;
288  //return nullptr;
289 }

◆ get_userspace()

user_workspace * app_workspace::get_userspace ( )

Definition at line 630 of file app_workspace.cpp.

630  {
631  if (has_user())
632  return this->userspace.get();
633  else
634  return nullptr;
635 }
bool has_user()
Checks if user is logged in. This is determined by user_space being initialized and having the user_l...
std::unique_ptr< user_workspace > userspace
User's workspace. Serves similar function as app_workspace but for user data.

◆ has_user()

bool app_workspace::has_user ( )

Checks if user is logged in. This is determined by user_space being initialized and having the user_logged flag set.

Returns
true
false

Definition at line 626 of file app_workspace.cpp.

626  {
627  return this->user_logged && this->userspace.get() != nullptr && this->userspace->has_user();
628 }
bool user_logged
Flag to indicate if a user is logged in.

◆ hx711_calibrate()

void app_workspace::hx711_calibrate ( )

Function that performs calibration on the hx711 controller.

Definition at line 134 of file app_workspace.cpp.

134  {
135  this->hx_measuring = true;
136  this->refresh_current_screen = true;
137 
138  // this->s6_tare_complete = false; // when calibration is run, tare is reset (cant know if weight is stil zeroed)
139 
140  this->calib_thread.reset(new std::thread(threaded_calibration));
141  this->calib_thread->detach(); // not waiting for the thread to finish, but ignoring other events anyway
142 }
void threaded_calibration()
This function is started in a new thread for calibrating HX711.
bool refresh_current_screen
When this is set to true, elements of current screen will be reinitialized for next frame.

◆ hx711_continuous_measure()

int app_workspace::hx711_continuous_measure ( )

Starts hx711 measuring that will run until its stopped by user.

measures into userspace->measured based on screen2 inputs

Returns
int - 0 on success

Definition at line 193 of file app_workspace.cpp.

193  {
194  if (this->hx711_controller.get() == nullptr) {
196  get_localized_text("ERR_HX_NOT_INIT_DESC"));
197  return 1;
198  }
199 
200  if (!this->user_logged || this->userspace.get() == nullptr || !this->userspace->has_user()) {
201  set_err_screen_and_activate(get_localized_text("ERR_NO_USER_LOGIN"),
202  get_localized_text("ERR_NO_USER_LOGIN_DESC"));
203  return 2; // can't measure if no user is logged in
204  }
205 
206  // reset this flag, if the measurement is stopped in the moment its finishing,
207  // this will stay set to true and cause the new measurement not to save
208  this->hx_not_finished_yet = false;
209  this->hx_measuring = true;
210  this->hx_continuous = true;
211  this->refresh_current_screen = true;
212 
213  this->measuring_thread.reset(new std::thread(threaded_values_read_looped));
214  this->measuring_thread->detach(); // not waiting for the thread to finish, but ignoring other events anyway
215 
216  return 0;
217 }
void threaded_values_read_looped()
This function is started in a new thread for reading HX711 values in a loop. This reading is done in ...
std::unique_ptr< custom_hx711 > hx711_controller
HX711 controller. This is an instance of custom_hx711 which extends library's AdvancedHX711.
void set_err_screen_and_activate(const char *title, const char *label)
Set the err screen and activate the screen on next frame.
bool hx_not_finished_yet
const char * get_localized_text(const char *key)
Get the localized text object.

◆ hx711_measure()

int app_workspace::hx711_measure ( )

Performs hx711 measuring with set values of either samples or timeout. The results is saved to the database and to userspace variable - measured.

Returns
int - 0 on success

Definition at line 144 of file app_workspace.cpp.

144  {
145  if (this->hx711_controller.get() == nullptr) {
147  get_localized_text("ERR_HX_NOT_INIT_DESC"));
148  return 5;
149  }
150 
151  if (!this->user_logged || this->userspace.get() == nullptr || !this->userspace->has_user()) {
152  set_err_screen_and_activate(get_localized_text("ERR_NO_USER_LOGIN"),
153  get_localized_text("ERR_NO_USER_LOGIN_DESC"));
154  return 1; // can't measure if no user is logged in
155  }
156 
157  if (this->selected_hx_measure_opt == 0) {
158  if (this->samples_in < S2_MIN_SAMPLES_IN || this->samples_in > S2_MAX_SAMPLES_IN) {
160  get_localized_text("ERR_S2_SAMPLES_IN_ERR"));
161  spdlog::error("app_workspace.cpp - samples_in: {0}", this->samples_in);
162  this->clear_s2_buffers();
163  return 2;
164  }
165  } else if (this->selected_hx_measure_opt == 1) {
166  if (this->timeout_in < S2_MIN_TIMEOUT_IN || this->timeout_in > S2_MAX_TIMEOUT_IN) {
168  get_localized_text("ERR_S2_TIMEOUT_IN_ERR"));
169  spdlog::error("app_workspace.cpp - timeout_in: {0}", this->timeout_in);
170  this->clear_s2_buffers();
171  return 2;
172  }
173  } else {
175  get_localized_text("ERR_S2_UNKNOWN_DESC"));
176  return 3;
177  }
178 
179  // reset this flag, if the measurement is stopped in the moment its finishing,
180  // this will stay set to true and cause the new measurement not to save
181  this->hx_not_finished_yet = false;
182  this->hx_continuous = false;
183 
184  this->hx_measuring = true;
185  this->refresh_current_screen = true;
186 
187  this->measuring_thread.reset(new std::thread(threaded_values_read));
188  this->measuring_thread->detach(); // not waiting for the thread to finish, but ignoring other events anyway
189 
190  return 0;
191 }
void threaded_values_read()
This function is started in a new thread for reading HX711 values.
#define S2_MAX_SAMPLES_IN
Definition: app_workspace.h:30
#define S2_MAX_TIMEOUT_IN
Definition: app_workspace.h:32
int selected_hx_measure_opt

◆ hx711_tare()

void app_workspace::hx711_tare ( )

Function that performs taring on the hx711 controller.

Definition at line 123 of file app_workspace.cpp.

123  {
124  this->hx_measuring = true;
125  this->refresh_current_screen = true;
126 
127  this->s6_calibration_finished = false; // running tare means there is not calibration
128 
129  // using calib_thread because tare can't be run at the same time as calibration
130  this->calib_thread.reset(new std::thread(threaded_tare));
131  this->calib_thread->detach(); // not waiting for the thread to finish, but ignoring other events anyway
132 }
void threaded_tare()
This function is started in a new thread for taring HX711.
bool s6_calibration_finished

◆ hx711_values_samples()

std::vector< HX711::Value > app_workspace::hx711_values_samples ( int  samples)

Reads the HX711 sensor until @samples is collected. Also starts new thread which is used to monitor ongoing measuring.

Parameters
samplesto be measured
Returns
std::vector<HX711::Value> measured values

Definition at line 219 of file app_workspace.cpp.

219  {
220  if (!this->hx_continuous)
221  spdlog::info("app_workspace.cpp - Starting HX711 measuring for {0} samples.", samples);
222  std::mutex mtx;
223 
224  bool measuring = true;
225  std::vector<HX711::Value> *watcher = nullptr;
226 
227  this->measuring_watchdog.reset(new std::thread(threaded_measuring_watchdog, &measuring, &watcher));
228  // this->measuring_watchdog->detach(); // join after measuring would be preferrable, but that is potential deadlock
229 
230  spdlog::debug("STARTING measuring");
231 
232  mtx.lock();
233  m_start = std::chrono::high_resolution_clock::now();
234  mtx.unlock();
235  //FIXME this might throw exception if it fails
236  std::vector<HX711::Value> res = hx711_controller->getValues(samples, &watcher);
237  mtx.lock();
238  m_end = std::chrono::high_resolution_clock::now();
239  mtx.unlock();
240 
241  mtx.lock();
242  measuring = false; // needed to stop watchdog thread!!
243  mtx.unlock();
244 
245  this->measuring_watchdog->join();
246 
247  spdlog::debug("ENDING measuring");
248 
249  return res;
250 }
void threaded_measuring_watchdog(bool *msring, std::vector< HX711::Value > **watcher)
When an HX711 reading is started, when creating new thread, this thread is created as well....
std::chrono::time_point< std::chrono::high_resolution_clock > m_start
std::chrono::time_point< std::chrono::high_resolution_clock > m_end

◆ hx711_values_timeout()

std::vector< HX711::Value > app_workspace::hx711_values_timeout ( int  seconds)

Reads the HX711 sensor until @timeout is reached. Also starts new thread which is used to monitor ongoing measuring.

Parameters
secondsfor how long the measuring will be going
Returns
std::vector<HX711::Value> measured values

Definition at line 252 of file app_workspace.cpp.

252  {
253  spdlog::info("app_workspace.cpp - Starting HX711 measuring for {0} seconds.", seconds);
254  std::mutex mtx;
255 
256  bool measuring = true;
257  std::vector<HX711::Value> *watcher = nullptr;
258 
259  this->measuring_watchdog.reset(new std::thread(threaded_measuring_watchdog, &measuring, &watcher));
260  // this->measuring_watchdog->detach(); // join after measuring would be preferrable, but that is potential deadlock
261 
262  spdlog::debug("STARTING measuring");
263 
264  mtx.lock();
265  m_start = std::chrono::high_resolution_clock::now();
266  mtx.unlock();
267  //FIXME this might throw exception if it fails (same as above)
268  std::vector<HX711::Value> res = hx711_controller->getValues(std::chrono::seconds(seconds), &watcher);
269  mtx.lock();
270  m_end = std::chrono::high_resolution_clock::now();
271  mtx.unlock();
272 
273  mtx.lock();
274  measuring = false; // needed to stop watchdog thread!!
275  mtx.unlock();
276 
277  this->measuring_watchdog->join();
278 
279  spdlog::debug("ENDING measuring");
280 
281  // spdlog::info("app_workspace.cpp - HX711 finished taking raw values.");
282 
283  return res;
284 }

◆ init_hx711_controller()

int app_workspace::init_hx711_controller ( )

Initializes the hx711 controller.

Returns
int - 0 on success

Definition at line 93 of file app_workspace.cpp.

93  {
94  try {
95  HX711::Rate rate = HX711::Rate::HZ_80;
96  if (this->main_config->hx_conf.rate == 80) {
97  rate = HX711::Rate::HZ_80;
98  } else if (this->main_config->hx_conf.rate == 10) {
99  rate = HX711::Rate::HZ_10;
100  } else {
101  rate = HX711::Rate::OTHER;
102  }
103 
104  // hx711_controller = std::make_unique<HX711::AdvancedHX711>(
105  hx711_controller = std::make_unique<custom_hx711>(
106  this->main_config->hx_conf.data_pin,
107  this->main_config->hx_conf.clock_pin,
108  this->main_config->hx_conf.ref_unit,
109  this->main_config->hx_conf.offset,
110  rate
111  );
112 
113  return 0;
114  } catch (HX711::GpioException &ex) {
115  std::string what = ex.what();
116  std::string err = "app_workspace.cpp - hx711 initialization failed. Reason: " + what;
117  spdlog::critical(err);
118 
119  return 1;
120  }
121 }
std::unique_ptr< app_config > main_config
Application config loaded from app_config.conf (main config file).

◆ init_labels()

void app_workspace::init_labels ( )

This is called after localisation dictionary is loaded. After the dict is loaded arrays defined in app_workspace must be initialized so they can be used in GUI comboboxes.

Definition at line 61 of file app_workspace.cpp.

61  {
62  this->s6_role_labels[0] = get_localized_text("GT_ROLE_1");
63  this->s6_role_labels[1] = get_localized_text("GT_ROLE_2");
64  this->s6_role_labels[2] = get_localized_text("GT_ROLE_3");
65 
66  this->s6_month_labels[0] = get_localized_text("GT_MONTH_01");
67  this->s6_month_labels[1] = get_localized_text("GT_MONTH_02");
68  this->s6_month_labels[2] = get_localized_text("GT_MONTH_03");
69  this->s6_month_labels[3] = get_localized_text("GT_MONTH_04");
70  this->s6_month_labels[4] = get_localized_text("GT_MONTH_05");
71  this->s6_month_labels[5] = get_localized_text("GT_MONTH_06");
72  this->s6_month_labels[6] = get_localized_text("GT_MONTH_07");
73  this->s6_month_labels[7] = get_localized_text("GT_MONTH_08");
74  this->s6_month_labels[8] = get_localized_text("GT_MONTH_09");
75  this->s6_month_labels[9] = get_localized_text("GT_MONTH_10");
76  this->s6_month_labels[10] = get_localized_text("GT_MONTH_11");
77  this->s6_month_labels[11] = get_localized_text("GT_MONTH_12");
78 
79  this->s6_unit_sel_labels[0] = get_localized_text("GT_G_UNIT");
80  this->s6_unit_sel_labels[1] = get_localized_text("GT_KG_UNIT");
81 
82  this->unit_sel_labels[0] = get_localized_text("GT_MG_UNIT");
83  this->unit_sel_labels[1] = get_localized_text("GT_G_UNIT");
84  this->unit_sel_labels[2] = get_localized_text("GT_KG_UNIT");
85 
86  this->hx_measure_opts[0] = get_localized_text("SCREEN_2_CB1_SAMPLES");
87  this->hx_measure_opts[1] = get_localized_text("SCREEN_2_CB1_TIMEOUT");
88 
89  this->s3_filter_opt_labels[0] = get_localized_text("SCREEN_3_CB1_LARGER");
90  this->s3_filter_opt_labels[1] = get_localized_text("SCREEN_3_CB1_SMALLER");
91 }
const char * s6_month_labels[S6_MONTH_CNT]
const char * s6_role_labels[S6_ROLE_CNT]
const char * s3_filter_opt_labels[S3_FILTER_OPT_CNT]
const char * unit_sel_labels[UNIT_SEL_CNT]
const char * s6_unit_sel_labels[S6_UNIT_SEL_CNT]
const char * hx_measure_opts[S2_MEASURE_OTPS_CNT]

◆ is_debug_screens()

bool app_workspace::is_debug_screens ( )

Retruns flag is.

Returns
true
false

Definition at line 621 of file app_workspace.cpp.

621  {
622  return main_config->debug_screens;
623 }

◆ load_font_size()

int app_workspace::load_font_size ( app_workspace_ns::font_size  key,
float  size_px 
)

Initializes font into the map.

Parameters
keyunder which font is inited
size_pxsize of inited font
Returns
int 0 on successs

Definition at line 320 of file app_workspace.cpp.

320  {
321  spdlog::info("app_workspace.cpp - Loading font with size {0}", size_px);
322 
323  ImGuiIO &io = ImGui::GetIO();
324  std::string font_path = this->main_config->fonts_path + this->main_config->font;
325  ImFont* font = io.Fonts->AddFontFromFileTTF(font_path.c_str(), size_px);
326 
327  if (font == NULL) {
328  // FIXME - maybe critical
329  spdlog::error("app_workspace.cpp - Failed to load font size {0}!" , size_px);
330  return 1;
331  }
332 
333  this->loaded_fonts.emplace(key, font);
334 
335  return 0;
336 }
std::map< app_workspace_ns::font_size, ImFont * > loaded_fonts
Map of loaded fonts.

◆ log_in_user_cred()

void app_workspace::log_in_user_cred ( )

Logs in user using credentials. Credentials are gained from screen_1 buffers.

Definition at line 418 of file app_workspace.cpp.

418  {
419  if (strlen(usr_name) <= 0 || strlen(usr_passwd) <= 0) {
420  this->set_err_screen_and_activate(get_localized_text("ERR_USER_LOGIN"),
421  get_localized_text("ERR_USER_CREDS_NOT_ENTERED"));
422  return;
423  }
424 
425  if (verify_user_cred()) {
426  spdlog::error("app_workspace.cpp - User login. Failed to verify credentials");
427  return;
428  }
429 
430  int rv;
431  user_cont *usr = (user_cont*) calloc(1, sizeof(user_cont));
432 
433  rv = db_conn->query_user_data(usr, usr_name);
434  if (rv) {
435  // this hould be unreachable, because user was already checked for credentials -> user exists, sql can fail
436  spdlog::error("app_workspace.cpp - Failed to login user. db_driver log should provide more information.");
437  //return 1;
438  return;
439  }
440 
441  rv = db_conn->query_measurement_headers(usr);
442  if (rv) {
443  spdlog::error("app_workspace.cpp - Failed to retrieve user measurement headers.");
444  // return;
445  }
446 
447  // loading_sub is false
448  userspace.reset(new user_workspace(usr));
449  user_logged = true;
450  clear_s1_buffers();
451  scr_mgr->refresh_all_screns();
452 
453  //return 0;
454 }
std::unique_ptr< db_driver > db_conn
Database driver holder.
char usr_passwd[DEF_BUFF_SIZE_SMALL]
int verify_user_cred()
Tests if user entered correct credentials.
char usr_name[DEF_BUFF_SIZE]
Class used as a container for user data, that are selected from database. (and also for insert,...
Container that servers for storing users data and manipulating them.

◆ log_in_user_rfid()

int app_workspace::log_in_user_rfid ( rfid_reader::rfid_tag tag,
bool  loading_sub = false 
)

Logs in user through scanned RFID tag. Initializes user_workspace if loading_sub is false.

Parameters
tagRFID tag structure
loading_subif the user is to be logged as a subuser.
Returns
int 0 on success, otherwise if logging in cannot be done

Definition at line 338 of file app_workspace.cpp.

338  {
339  user_cont *usr = (user_cont*) calloc(1, sizeof(user_cont));
340  int rv;
341 
342  spdlog::info("app_workspace.cpp - Loging in user through RFID. Querrying DB...");
343 
344  rv = db_conn->query_user_data(usr, nullptr, tag->serial, tag->serial_size);
345  if (rv) {
346  spdlog::error("app_workspace.cpp - Failed to login user. db_driver log should provide more information.");
347  free(usr);
348  return 1;
349  }
350 
351  rv = db_conn->query_measurement_headers(usr);
352  if (rv) {
353  spdlog::error("app_workspace.cpp - Failed to retrieve user measurement headers.");
354  // free(usr);
355  // return 1;
356  }
357 
358  if (loading_sub) {
359  if (userspace.get() == nullptr || !userspace->has_user()) {
360  spdlog::error("app_workspace.cpp - Cannot login \"subuser\", because user workspace isn't initialized."
361  " User isn't logged in? flag: user_logged: {0}", user_logged);
362  free(usr);
363  return 1;
364  }
365 
366  if (userspace->get_user_id() == usr->id) {
367  this->set_err_screen_and_activate(get_localized_text("ERR_SUBUSER_LOGIN"),
368  get_localized_text("ERR_SUBUSER_SAME_USER"));
369  free(usr);
370  return 1;
371  }
372 
373  this->userspace->subuser.reset(usr);
374  this->scr_mgr->refresh_all_screns();
375  spdlog::info("app_workspace.cpp - Finished logging in subuser through RFID.");
376 
377  return 0;
378  }
379 
380  // loading_sub is false
381  userspace.reset(new user_workspace(usr));
382  user_logged = true;
383  scr_mgr->refresh_all_screns();
384 
385  spdlog::info("app_workspace.cpp - Finished logging in user through RFID.");
386 
387  return 0;
388 }
unsigned long id
uint8_t serial[10]
Definition: rfid_reader.h:14

◆ login_subuser()

void app_workspace::login_subuser ( )

When a user is already logged in and their role is employee or admin, they can log in another user as their subuser.

Definition at line 456 of file app_workspace.cpp.

456  {
457  // not checking has_user(), because this action is currently only available from user logged in screen
458 
459  if (strlen(subusr_name) <= 0) {
460  this->set_err_screen_and_activate(get_localized_text("ERR_SUBUSER_LOGIN"),
461  get_localized_text("ERR_SUBUSER_INPUT_EMPTY"));
462  return;
463  }
464 
465  int rv = db_conn->can_login_subuser(subusr_name);
466  if (rv == 2) {
467  this->set_err_screen_and_activate(get_localized_text("ERR_SUBUSER_LOGIN"),
468  get_localized_text("ERR_SUBUSER_NOT_FOUND"));
469  return;
470  } else if (rv == 3) {
471  this->set_err_screen_and_activate(get_localized_text("ERR_SUBUSER_LOGIN"),
472  get_localized_text("ERR_SUBUSER_ROLE_TOO_HIGH"));
473  return;
474  }
475  if (rv) {
476  this->set_err_screen_and_activate(get_localized_text("ERR_SUBUSER_LOGIN"),
477  get_localized_text("ERR_SUBUSER_FAILED_TO_FETCH"));
478  return;
479  }
480 
481  user_cont *usr = (user_cont*) calloc(1, sizeof(user_cont));
482 
483  rv = db_conn->query_user_data(usr, subusr_name);
484  if (rv) {
485  spdlog::error("app_workspace.cpp - Failed to login user. db_driver log should provide more information.");
486  return;
487  }
488 
489  rv = db_conn->query_measurement_headers(usr);
490  if (rv) {
491  spdlog::error("app_workspace.cpp - Failed to retrieve user measurement headers.");
492  return;
493  }
494 
495  if (userspace->get_user_id() == usr->id) {
496  this->set_err_screen_and_activate(get_localized_text("ERR_SUBUSER_LOGIN"),
497  get_localized_text("ERR_SUBUSER_SAME_USER"));
498  free(usr);
499  return;
500  }
501 
502  this->userspace->subuser.reset(usr);
503  this->scr_mgr->refresh_all_screns();
504 }
char subusr_name[DEF_BUFF_SIZE]

◆ logoff_user()

void app_workspace::logoff_user ( )

Logs out user and subuser if logged in.

Definition at line 514 of file app_workspace.cpp.

514  {
515  this->logout_subuser();
516  // clear_s1_buffers(); // these should be cleared after login, but just to be sure
517  userspace.release();
518  user_logged = false;
519  scr_mgr->refresh_all_screns();
520  scr_mgr->clear_previous_screen_stack();
521 }
void logout_subuser()
Logs out subuser.
void clear_previous_screen_stack()

◆ logout_subuser()

void app_workspace::logout_subuser ( )

Logs out subuser.

Definition at line 506 of file app_workspace.cpp.

506  {
507  clear_s1_buffers();
508  this->userspace->subuser.release();
509  this->userspace->measured.release();
510  this->userspace->picked.release();
511  this->scr_mgr->refresh_all_screns();
512 }

◆ normalize_raw_values()

std::vector< double > app_workspace::normalize_raw_values ( std::vector< HX711::Value > &  raw_vals)

Takes raw values and normalizes them using HX offset and reference unit. Resulting values are in Grams!

Parameters
raw_valsvalues before normalization
Returns
std::vector<double> normalized values in Grams

Definition at line 673 of file app_workspace.cpp.

673  {
674  std::vector<double> res;
675 
676  for (size_t i = 0; i < raw_vals.size(); i++) {
677  res.push_back(this->hx711_controller->normalise(raw_vals[i]));
678  }
679 
680  return res;
681 }

◆ open_db_connection()

int app_workspace::open_db_connection ( )

Tries to open the database connection and init db_driver.

Returns
int - 0 on success

Definition at line 637 of file app_workspace.cpp.

637  {
638  db_conn.reset(new db_driver(&(main_config->db_conf)));
639  return db_conn->is_open() ? 0 : 1;
640 }
Handles database querries.
Definition: db_driver.h:41

◆ print_config()

void app_workspace::print_config ( )

Prints application config on debug level

Definition at line 688 of file app_workspace.cpp.

688  {
689  spdlog::debug("========== (debug) config file start ==========");
690  spdlog::debug("general settings:");
691  spdlog::debug("\tdebug_screens: {0}", main_config->debug_screens);
692  spdlog::debug("\tfonts_path: {0}", main_config->fonts_path);
693  spdlog::debug("\tfont: {0}", main_config->font);
694  spdlog::debug("\tlang_path: {0}", main_config->lang_path);
695  spdlog::debug("\tavailable_langs:");
696  for (size_t i = 0; i < main_config->lang_options.size(); i++) {
697  spdlog::debug("\t\topt {0}: {1}", i, main_config->lang_options[i]);
698  }
699  //spdlog::debug("\tavailable_langs: {0}", main_config->lang_options);
700  spdlog::debug("\tdefault_lang: {0}", main_config->lang_default);
701  spdlog::debug("\tcurrent_lang: {0}", main_config->lang_current);
702  spdlog::debug("spi keyboard settings:");
703  spdlog::debug("\tdev: {0}", main_config->keyb_conf.dev);
704  spdlog::debug("\tmode: {0}", main_config->keyb_conf.config->mode);
705  spdlog::debug("\tbits: {0}", main_config->keyb_conf.config->bits_per_word);
706  spdlog::debug("\tspeed: {0}", main_config->keyb_conf.config->speed);
707  spdlog::debug("\tdelay: {0}", main_config->keyb_conf.config->delay);
708  spdlog::debug("spi rfid settings:");
709  spdlog::debug("\tdev: {0}", main_config->rfid_conf.dev);
710  spdlog::debug("\tmode: {0}", main_config->rfid_conf.config->mode);
711  spdlog::debug("\tbits: {0}", main_config->rfid_conf.config->bits_per_word);
712  spdlog::debug("\tspeed: {0}", main_config->rfid_conf.config->speed);
713  spdlog::debug("\tdelay: {0}", main_config->rfid_conf.config->delay);
714  spdlog::debug("database settings:");
715  spdlog::debug("\thost: {0}", main_config->db_conf.host);
716  spdlog::debug("\tuser: {0}", main_config->db_conf.user);
717  spdlog::debug("\tpassword: {0}", main_config->db_conf.passwd);
718  spdlog::debug("\tdatabase: {0}", main_config->db_conf.db);
719  spdlog::debug("\tport: {0}", main_config->db_conf.port);
720  spdlog::debug("\tsocket: {0}", main_config->db_conf.socket);
721  spdlog::debug("\tclientflags: {0}", main_config->db_conf.clientflags);
722  spdlog::debug("hx711 settings:");
723  spdlog::debug("\tdata pin: {0}", main_config->hx_conf.data_pin);
724  spdlog::debug("\tclock pin: {0}", main_config->hx_conf.clock_pin);
725  spdlog::debug("\trate: {0}", main_config->hx_conf.rate);
726  spdlog::debug("\tref unit: {0}", main_config->hx_conf.ref_unit);
727  spdlog::debug("\toffset: {0}", main_config->hx_conf.offset);
728  spdlog::debug("========== (debug) config file end ==========");
729 }

◆ register_new_user()

int app_workspace::register_new_user ( )

This function validets screen 6 buffers and if everything checks out then inserts new user into the DB.

Returns
int 0 on success

Definition at line 523 of file app_workspace.cpp.

523  {
524  if (strlen(this->s6_uname_in) <= 0 ||
525  strlen(this->s6_passwd_in) <= 0 ||
526  strlen(this->s6_name_in) <= 0 ||
527  strlen(this->s6_lastname_in) <= 0 ||
528  this->s6_year_in <= 0 ||
529  this->s6_day_in <= 0)
530  {
531  this->set_err_screen_and_activate(get_localized_text("ERR_S6_USR_ADD_INPUT_ERR"),
532  get_localized_text("ERR_S6_USR_ADD_INPUT_DESC"));
533  return 1;
534  }
535 
536  user_cont *usr_tmp = (user_cont*) calloc(1, sizeof(user_cont));
537  std::unique_ptr<user_cont> usr(usr_tmp);
538 
539  int rv = this->db_conn->is_username_available(this->s6_uname_in);
540  if (rv == 3) {
541  this->set_err_screen_and_activate(get_localized_text("ERR_S6_USR_ADD"),
542  get_localized_text("ERR_S6_USR_NAME_TAKEN"));
543  return 1;
544  } else if (rv) {
545  spdlog::error("screen_definitions.cpp - Failed to verify username availability");
546  this->set_err_screen_and_activate(get_localized_text("ERR_DB_ERROR"),
547  get_localized_text("ERR_DB_UNAME_CHECK_FAIL"));
548  return 1;
549  } else {
550  spdlog::debug("screen_definitions.cpp - Username is available.");
551  }
552 
553  if (this->s6_rfid_indicator) {
554  rv = this->db_conn->is_rfid_serial_available(this->s6_rfid_tag->serial, this->s6_rfid_tag->serial_size);
555 
556  if (rv == 3) {
557  this->set_err_screen_and_activate(get_localized_text("ERR_S6_USR_ADD"),
558  get_localized_text("ERR_S6_USR_RFID_TAKEN"));
559  return 1;
560  } else if (rv) {
561  spdlog::error("screen_definitions.cpp - Failed to verify username availability");
562  this->set_err_screen_and_activate(get_localized_text("ERR_DB_ERROR"),
563  get_localized_text("ERR_DB_RFID_CHECK_FAIL"));
564  return 1;
565  } else {
566  spdlog::debug("screen_definitions.cpp - RFID is available.");
567  memcpy(usr->rfid_serial_bin, this->s6_rfid_tag->serial, this->s6_rfid_tag->serial_size);
568  usr->rfid_ser_len = this->s6_rfid_tag->serial_size;
569  }
570  }
571 
572  std::time_t ct_tmp = std::time(nullptr);
573  std::tm *current_time = std::localtime(&ct_tmp);
574 
575  // tm_year is "current_year - 1900"
576  if (this->s6_year_in > (current_time->tm_year + 1900) || this->s6_year_in < YEAR_MIN) {
577  // spdlog::error("screen_definitions.cpp - Year out of range {0} (current_year: {1})",
578  // this->s6_year_in, (current_time->tm_year + 1900));
580  get_localized_text("ERR_DOB_YEAR_OOR"));
581  return 1;
582  }
583  if (this->s6_day_in > DAY_MAX || this->s6_day_in < DAY_MIN) {
584  spdlog::error("screen_definitions.cpp - Year out of range");
586  get_localized_text("ERR_DOB_DAY_OOR"));
587  return 1;
588  }
589 
590  char buf[64];
591  sprintf(buf, "%04d-%02d-%02d", this->s6_year_in, this->s6_selected_month + 1, this->s6_day_in);
592 
593  std::tm dob;
594  memset(&dob, 0, sizeof(std::tm));
595  strptime(buf, "%Y-%m-%d", &dob);
596 
597  usr->role = this->s6_selected_role;
598  usr->username = this->s6_uname_in;
599  usr->name = this->s6_name_in;
600  usr->lastname = this->s6_lastname_in;
601  if (strlen(this->s6_desc_in) > 0) {
602  usr->description = this->s6_desc_in;
603  }
604  usr->date_of_birth = dob;
605 
606  rv = this->db_conn->insert_user(usr.get(), this->s6_passwd_in);
607  if (rv) {
608  spdlog::error("screen_definitions.cpp - Failed to execute user insert.");
609  this->set_err_screen_and_activate(get_localized_text("ERR_DB_ERROR"),
610  get_localized_text("ERR_DB_INSERT_USER_FAIL"));
611  return 1;
612  } else {
613  this->clear_s6_buffers();
614  spdlog::info("screen_definitions.cpp - Successfully added new user.");
615  this->scr_mgr->set_selected_screen(5);
616  }
617 
618  return 0;
619 }
#define DAY_MIN
Definition: app_workspace.h:44
#define YEAR_MIN
Definition: app_workspace.h:46
#define DAY_MAX
Definition: app_workspace.h:45
bool s6_rfid_indicator
char s6_passwd_in[DEF_BUFF_SIZE_SMALL]
char s6_name_in[DEF_BUFF_SIZE]
char s6_lastname_in[DEF_BUFF_SIZE_SMALL]
std::unique_ptr< rfid_reader::rfid_tag > s6_rfid_tag
char s6_uname_in[DEF_BUFF_SIZE_SMALL]
char s6_desc_in[DEF_BUFF_SIZE_EXTRA]
void set_selected_screen(uint8_t screen)

◆ set_err_screen_and_activate()

void app_workspace::set_err_screen_and_activate ( const char *  title,
const char *  label 
)

Set the err screen and activate the screen on next frame.

Parameters
titleof the error
labeldescription of the error

Definition at line 302 of file app_workspace.cpp.

302  {
303  extern gui_label *err_title;
304  extern gui_label *err_desc;
305  err_title->set_label(title);
306  err_desc->set_label(label);
307  this->scr_mgr->set_selected_screen(0);
308 }
This is a wrapper for ImGui Text which serves as unchangeble label.
Definition: gui_label.h:11
void set_label(const char *label)
Definition: gui_label.cpp:54
gui_label * err_title
gui_label * err_desc

◆ set_kb_testing()

void app_workspace::set_kb_testing ( bool  val)

Sets keyboard testing. This is a wrapper for thread secure function

Definition at line 835 of file app_workspace.cpp.

835  {
837 }
void set_kb_testing_inner(bool val)
Definition: keyboard.cpp:222

◆ set_scr_mgr()

void app_workspace::set_scr_mgr ( screen_manager scr_mgr)

Set the screen manager instancte. This should only be called once, when the application is starting up.

This function should only be used once in the program. Function is required, because screen_manager cannot be initialized during construction due to cycling.

Parameters
scr_mgr

Definition at line 298 of file app_workspace.cpp.

298  {
299  this->scr_mgr = scr_mgr;
300 }

◆ test_hx_samples()

int app_workspace::test_hx_samples ( )

Tests how long (in ms) it takes hx to take X (def 160) samples. Does 5 iterations with X and 5 iterations with X * it

Definition at line 731 of file app_workspace.cpp.

731  {
732  if (this->hx711_controller.get() == nullptr) {
734  get_localized_text("ERR_HX_NOT_INIT_DESC"));
735  return 5;
736  }
737 
738  if (!this->has_user()) {
739  set_err_screen_and_activate(get_localized_text("ERR_NO_USER_LOGIN"),
740  get_localized_text("ERR_NO_USER_LOGIN_DESC"));
741  return 1; // can't measure if no user is logged in, this shouldn't be reachable
742  }
743 
744  this->hx_measuring = true;
745  this->test_hx_testing = true;
747 
748  this->measuring_thread.reset(new std::thread([]() {
749  std::mutex mtx;
750  mtx.lock();
751  app_workspace *app_wrk = app_workspace::get_instance().get();
752 
753  // constant iterations
754  for (int i = 0; i < HX_TEST_CONST_IT_COUNT; i++) {
756  auto res = std::chrono::duration_cast<std::chrono::milliseconds>(app_wrk->m_end - app_wrk->m_start).count();
757  app_wrk->test_hx_samples_times[i].first = res;
758  app_wrk->test_hx_samples_times[i].second =
759  "Smps: " + std::to_string(HX_TEST_SAMP_COUNT) + " - " + std::to_string(res) + " ms";
760  }
761 
762  // increasing iterations
763  for (int i = 0; i < HX_TEST_INC_IT_COUNT; i++) {
764  app_wrk->hx711_values_samples(HX_TEST_SAMP_COUNT * (i + 1));
765  auto res = std::chrono::duration_cast<std::chrono::milliseconds>(app_wrk->m_end - app_wrk->m_start).count();
766  app_wrk->test_hx_samples_times[i + HX_TEST_CONST_IT_COUNT].first = res;
767  app_wrk->test_hx_samples_times[i + HX_TEST_CONST_IT_COUNT].second =
768  "Smps: " + std::to_string(HX_TEST_SAMP_COUNT * (i + 1)) + " - " + std::to_string(res) + " ms";
769  }
770 
771  app_wrk->hx_measuring = false;
772  app_wrk->test_hx_testing = false;
774  mtx.unlock();
775  }));
776 
777  this->measuring_thread->detach(); // not waiting for the thread to finish, but ignoring other events anyway
778 
779  return 0;
780 }
#define HX_TEST_CONST_IT_COUNT
Definition: app_workspace.h:52
#define HX_TEST_INC_IT_COUNT
Definition: app_workspace.h:53
#define HX_TEST_SAMP_COUNT
Definition: app_workspace.h:54
One of the most importat classes in the whole project. Holds variables that define the state of the a...
std::vector< HX711::Value > hx711_values_samples(int samples)
Reads the HX711 sensor until @samples is collected. Also starts new thread which is used to monitor o...
static std::unique_ptr< app_workspace > & get_instance()
Get the instance app_workspace which is a singleton.
std::pair< long, std::string > test_hx_samples_times[HX_TEST_CONST_IT_COUNT+HX_TEST_INC_IT_COUNT]
screen_manager * get_scr_mgr()
Get the screen manager instance. This instance is kind of singleton. Is initalized only once on start...
void refresh_screen_elements(uint8_t screen)
#define ADMIN_CTRL_SCREEN

◆ test_hx_timeout()

int app_workspace::test_hx_timeout ( )

Tests how many samples hx can create in X (def 2) seconds. Does 5 iterations with X and 5 iterations with X * it

Definition at line 782 of file app_workspace.cpp.

782  {
783  if (this->hx711_controller.get() == nullptr) {
785  get_localized_text("ERR_HX_NOT_INIT_DESC"));
786  return 5;
787  }
788 
789  if (!this->has_user()) {
790  set_err_screen_and_activate(get_localized_text("ERR_NO_USER_LOGIN"),
791  get_localized_text("ERR_NO_USER_LOGIN_DESC"));
792  return 1; // can't measure if no user is logged in, this shouldn't be reachable
793  }
794 
795  this->hx_measuring = true;
796  this->test_hx_testing = true;
798 
799  this->measuring_thread.reset(new std::thread([]() {
800  std::mutex mtx;
801  mtx.lock();
802  app_workspace *app_wrk = app_workspace::get_instance().get();
803 
804  // constant iterations
805  for (int i = 0; i < HX_TEST_CONST_IT_COUNT; i++) {
806  auto res = app_wrk->hx711_values_timeout(HX_TEST_TIME_COUNT);
807  app_wrk->test_hx_timeout_collected[i].first = res.size();
808  app_wrk->test_hx_timeout_collected[i].second =
809  "In: " + std::to_string(HX_TEST_TIME_COUNT) + "s - " + std::to_string(res.size()) + " samples";
810  }
811 
812  // increasing iterations
813  for (int i = 0; i < HX_TEST_INC_IT_COUNT; i++) {
814  auto res = app_wrk->hx711_values_timeout(HX_TEST_TIME_COUNT * (i + 1));
815  app_wrk->test_hx_timeout_collected[i + HX_TEST_CONST_IT_COUNT].first = res.size();
816  app_wrk->test_hx_timeout_collected[i + HX_TEST_CONST_IT_COUNT].second =
817  "In: " + std::to_string(HX_TEST_TIME_COUNT * (i + 1)) + "s - " + std::to_string(res.size())+" samples";
818  }
819 
820  app_wrk->hx_measuring = false;
821  app_wrk->test_hx_testing = false;
823  mtx.unlock();
824  }));
825 
826  this->measuring_thread->detach(); // not waiting for the thread to finish, but ignoring other events anyway
827 
828  return 0;
829 }
#define HX_TEST_TIME_COUNT
Definition: app_workspace.h:55
std::pair< long, std::string > test_hx_timeout_collected[HX_TEST_CONST_IT_COUNT+HX_TEST_INC_IT_COUNT]
std::vector< HX711::Value > hx711_values_timeout(int seconds)
Reads the HX711 sensor until @timeout is reached. Also starts new thread which is used to monitor ong...

◆ use_font_size()

void app_workspace::use_font_size ( app_workspace_ns::font_size  font_size)

When this is called following gui elements will use selected font_size until next call of this or font pop.

Parameters
font_sizeto be used

Definition at line 310 of file app_workspace.cpp.

310  {
311  ImGui::PushFont(this->loaded_fonts.at(font_size));
312  this->font_push_cnt++;
313  cur_font_size = font_size;
314 }
int font_push_cnt
Whenever a font is used, it is pushed to the font stack of ImGui library. All pushed fonts need to be...
font_size
This enum defines sizes of corresponding fonts. E.g.: SMALL_FONT is 12px.

◆ verify_user_cred()

int app_workspace::verify_user_cred ( )

Tests if user entered correct credentials.

Returns
int 0 on success

Definition at line 390 of file app_workspace.cpp.

390  {
391  int rv;
392  user_cred credentials;
393 
394  rv = db_conn->query_user_credentials(&credentials, usr_name);
395  if (rv == 2) {
396  this->set_err_screen_and_activate(get_localized_text("ERR_USER_CRED"),
397  get_localized_text("ERR_USER_NOT_FOUND_DESC"));
398  return 1;
399  }
400 
401  if (rv) {
402  this->set_err_screen_and_activate(get_localized_text("ERR_USER_CRED"),
403  get_localized_text("ERR_USER_CRED_GENERAL_DESC"));
404  return 1;
405  }
406 
407  if (strcmp(credentials.passwd, usr_passwd)) {
408  // FIXME - "credentials are wrong" - maybe do "wrong password" (but thats weaker security)
409  this->set_err_screen_and_activate(get_localized_text("ERR_USER_CRED"),
410  get_localized_text("ERR_USER_CRED_WRONG_DESC"));
411  return 1;
412  }
413 
414  return 0;
415 }
Struct used to contain username, password and user status until credentials are verified.
Definition: db_driver.h:30
char passwd[64]
Definition: db_driver.h:33

Member Data Documentation

◆ capslock_flag

bool app_workspace::capslock_flag = false

Flags used during text input.

Definition at line 191 of file app_workspace.h.

◆ db_conn

std::unique_ptr<db_driver> app_workspace::db_conn

Database driver holder.

Definition at line 180 of file app_workspace.h.

◆ float_test

float app_workspace::float_test = 0.0f

Definition at line 282 of file app_workspace.h.

◆ font_push_cnt

int app_workspace::font_push_cnt = 0

Whenever a font is used, it is pushed to the font stack of ImGui library. All pushed fonts need to be popped before the end of the frame to prevent application crash.

Definition at line 233 of file app_workspace.h.

◆ hx711_controller

std::unique_ptr<custom_hx711> app_workspace::hx711_controller

HX711 controller. This is an instance of custom_hx711 which extends library's AdvancedHX711.

Definition at line 175 of file app_workspace.h.

◆ hx_continuous

bool app_workspace::hx_continuous = false

Definition at line 212 of file app_workspace.h.

◆ hx_measure_opts

const char* app_workspace::hx_measure_opts[S2_MEASURE_OTPS_CNT]

screen_2 vars - these variables are used for gui elemnts on screen 2

Definition at line 239 of file app_workspace.h.

◆ hx_measuring

bool app_workspace::hx_measuring = false

hx711 (scale) variables

Definition at line 211 of file app_workspace.h.

◆ hx_not_finished_yet

bool app_workspace::hx_not_finished_yet = false

Definition at line 213 of file app_workspace.h.

◆ int_test

int app_workspace::int_test = 0

Following variables are used for testing and debugging

Definition at line 281 of file app_workspace.h.

◆ kb_delay_ms

int app_workspace::kb_delay_ms = 0

Definition at line 286 of file app_workspace.h.

◆ kb_delay_us

int app_workspace::kb_delay_us = 0

Definition at line 286 of file app_workspace.h.

◆ kb_ev_delay_ms

int app_workspace::kb_ev_delay_ms = 0

Definition at line 286 of file app_workspace.h.

◆ kb_ev_delay_us

int app_workspace::kb_ev_delay_us = 0

Definition at line 286 of file app_workspace.h.

◆ kb_in_state

std::unique_ptr<app_workspace_ns::kb_input_state> app_workspace::kb_in_state
Initial value:
=
std::make_unique<app_workspace_ns::kb_input_state>()

Input state holder.

Definition at line 169 of file app_workspace.h.

◆ loaded_fonts

std::map<app_workspace_ns::font_size, ImFont*> app_workspace::loaded_fonts

Map of loaded fonts.

Definition at line 227 of file app_workspace.h.

◆ lshift_flag

bool app_workspace::lshift_flag = false

Definition at line 191 of file app_workspace.h.

◆ m_end

std::chrono::time_point<std::chrono::high_resolution_clock> app_workspace::m_end

Definition at line 214 of file app_workspace.h.

◆ m_start

std::chrono::time_point<std::chrono::high_resolution_clock> app_workspace::m_start

Definition at line 214 of file app_workspace.h.

◆ main_config

std::unique_ptr<app_config> app_workspace::main_config = std::make_unique<app_config>()

Application config loaded from app_config.conf (main config file).

Definition at line 197 of file app_workspace.h.

◆ observed_measurement

std::unique_ptr<measurement> app_workspace::observed_measurement

This variable is used to observe measuring.

Definition at line 221 of file app_workspace.h.

◆ pass_test

char app_workspace::pass_test[DEF_BUFF_SIZE_SMALL]

Definition at line 284 of file app_workspace.h.

◆ ref_unit

HX711::Value app_workspace::ref_unit = 0

Definition at line 266 of file app_workspace.h.

◆ refresh_current_screen

bool app_workspace::refresh_current_screen = false

When this is set to true, elements of current screen will be reinitialized for next frame.

Definition at line 186 of file app_workspace.h.

◆ s3_filter_number

unsigned long app_workspace::s3_filter_number = 0

Definition at line 246 of file app_workspace.h.

◆ s3_filter_opt_labels

const char* app_workspace::s3_filter_opt_labels[S3_FILTER_OPT_CNT]

Definition at line 244 of file app_workspace.h.

◆ s3_selected_filter

int app_workspace::s3_selected_filter = 0

Definition at line 245 of file app_workspace.h.

◆ s5_screen_6_indicator

int app_workspace::s5_screen_6_indicator = -1

screen_5 vars - these variables are used for gui elemnts on screen 5

Definition at line 256 of file app_workspace.h.

◆ s5_success_indicator

int app_workspace::s5_success_indicator = -1

Definition at line 257 of file app_workspace.h.

◆ s6_calib_offset

int app_workspace::s6_calib_offset = 0

Definition at line 260 of file app_workspace.h.

◆ s6_calib_ref_unit

int app_workspace::s6_calib_ref_unit = 0

Definition at line 260 of file app_workspace.h.

◆ s6_calibration_finished

bool app_workspace::s6_calibration_finished = false

Definition at line 263 of file app_workspace.h.

◆ s6_day_in

int app_workspace::s6_day_in = 1

Definition at line 276 of file app_workspace.h.

◆ s6_desc_in

char app_workspace::s6_desc_in[DEF_BUFF_SIZE_EXTRA] = {0}

Definition at line 275 of file app_workspace.h.

◆ s6_lastname_in

char app_workspace::s6_lastname_in[DEF_BUFF_SIZE_SMALL] = {0}

Definition at line 275 of file app_workspace.h.

◆ s6_month_labels

const char* app_workspace::s6_month_labels[S6_MONTH_CNT]

Definition at line 272 of file app_workspace.h.

◆ s6_name_in

char app_workspace::s6_name_in[DEF_BUFF_SIZE] = {0}

Definition at line 273 of file app_workspace.h.

◆ s6_passwd_in

char app_workspace::s6_passwd_in[DEF_BUFF_SIZE_SMALL] = {0}

Definition at line 274 of file app_workspace.h.

◆ s6_ref_mass_in

int app_workspace::s6_ref_mass_in = 0

screen_6 vars - these variables are used for gui elemnts on screen 6

Definition at line 260 of file app_workspace.h.

◆ s6_ref_mass_str

std::string app_workspace::s6_ref_mass_str

Definition at line 264 of file app_workspace.h.

◆ s6_rfid_indicator

bool app_workspace::s6_rfid_indicator = false

Definition at line 268 of file app_workspace.h.

◆ s6_rfid_tag

std::unique_ptr<rfid_reader::rfid_tag> app_workspace::s6_rfid_tag

if available rfid is scanned for user set it to true

Definition at line 269 of file app_workspace.h.

◆ s6_role_labels

const char* app_workspace::s6_role_labels[S6_ROLE_CNT]

Definition at line 271 of file app_workspace.h.

◆ s6_samples_in

int app_workspace::s6_samples_in = S6_MIN_SAMPLES_IN

Definition at line 260 of file app_workspace.h.

◆ s6_selected_month

int app_workspace::s6_selected_month = 0

Definition at line 270 of file app_workspace.h.

◆ s6_selected_role

int app_workspace::s6_selected_role = 0

Definition at line 270 of file app_workspace.h.

◆ s6_selected_unit_option

int app_workspace::s6_selected_unit_option = 0

Definition at line 261 of file app_workspace.h.

◆ s6_tare_complete

bool app_workspace::s6_tare_complete = false

Definition at line 263 of file app_workspace.h.

◆ s6_uname_in

char app_workspace::s6_uname_in[DEF_BUFF_SIZE_SMALL] = {0}

Definition at line 273 of file app_workspace.h.

◆ s6_unit_sel_labels

const char* app_workspace::s6_unit_sel_labels[S6_UNIT_SEL_CNT]

Definition at line 262 of file app_workspace.h.

◆ s6_year_in

int app_workspace::s6_year_in = 2022

Definition at line 276 of file app_workspace.h.

◆ samples_in

int app_workspace::samples_in = S2_MIN_SAMPLES_IN

Definition at line 240 of file app_workspace.h.

◆ selected_hx_measure_opt

int app_workspace::selected_hx_measure_opt = 0

Definition at line 215 of file app_workspace.h.

◆ selected_hx_result_unit

int app_workspace::selected_hx_result_unit = 0

0 = samples, 1 = timeout

Definition at line 216 of file app_workspace.h.

◆ selected_measuring

int app_workspace::selected_measuring = 0

screen_3 vars - these variables are used for gui elemnts on screen 3

Definition at line 243 of file app_workspace.h.

◆ selected_unit_option

int app_workspace::selected_unit_option = 1

screen_4 vars - these variables are used for gui elemnts on screen 4

Definition at line 249 of file app_workspace.h.

◆ subusr_name

char app_workspace::subusr_name[DEF_BUFF_SIZE]

Definition at line 236 of file app_workspace.h.

◆ test_hx_samples_times

std::pair<long, std::string> app_workspace::test_hx_samples_times[HX_TEST_CONST_IT_COUNT+HX_TEST_INC_IT_COUNT]

Time in [ms] it took to collected samples. First 5 is constant time. Second 5 is increasing timeout

Definition at line 290 of file app_workspace.h.

◆ test_hx_testing

bool app_workspace::test_hx_testing = false

Definition at line 288 of file app_workspace.h.

◆ test_hx_timeout_collected

std::pair<long, std::string> app_workspace::test_hx_timeout_collected[HX_TEST_CONST_IT_COUNT+HX_TEST_INC_IT_COUNT]

Number of collected samples in time. First 5 during constant timeout. Second 5 during increasing timeout

Definition at line 292 of file app_workspace.h.

◆ text_test

char app_workspace::text_test[DEF_BUFF_SIZE]

Definition at line 283 of file app_workspace.h.

◆ timeout_in

int app_workspace::timeout_in = S2_MIN_TIMEOUT_IN

Definition at line 240 of file app_workspace.h.

◆ unit_sel_labels

const char* app_workspace::unit_sel_labels[UNIT_SEL_CNT]

Definition at line 251 of file app_workspace.h.

◆ unit_selection

const HX711::Mass::Unit app_workspace::unit_selection[UNIT_SEL_CNT]
Initial value:
=
{HX711::Mass::Unit::MG, HX711::Mass::Unit::G, HX711::Mass::Unit::KG}

Definition at line 252 of file app_workspace.h.

◆ user_logged

bool app_workspace::user_logged = false

Flag to indicate if a user is logged in.

Definition at line 203 of file app_workspace.h.

◆ userspace

std::unique_ptr<user_workspace> app_workspace::userspace

User's workspace. Serves similar function as app_workspace but for user data.

Definition at line 208 of file app_workspace.h.

◆ usr_name

char app_workspace::usr_name[DEF_BUFF_SIZE]

screen_1 vars - these variables are used for gui elemnts on screen 1

Definition at line 236 of file app_workspace.h.

◆ usr_passwd

char app_workspace::usr_passwd[DEF_BUFF_SIZE_SMALL]

Definition at line 236 of file app_workspace.h.

◆ zero_value

HX711::Value app_workspace::zero_value = 0

Definition at line 266 of file app_workspace.h.


The documentation for this class was generated from the following files: