Raspberry Pi Weighting Control System
This project serves as a simple weighting control system, that was realized as a Bachelor Thesis
app_workspace.h
Go to the documentation of this file.
1 #ifndef APP_WORKSPACE_H
2 #define APP_WORKSPACE_H
3 
4 #include <memory>
5 #include <string>
6 #include <chrono>
7 #include <thread>
8 #include <hx711/common.h>
9 #include "user_workspace.h"
10 #include "keyboard.h"
11 #include "rfid_reader.h"
12 #include "screen_manager.h"
13 #include "spidev_lib++.h"
14 #include "db_driver.h"
15 #include "custom_hx711.h"
16 
17 #include "imgui.h"
18 #include <map>
19 
20 #define DEF_BUFF_SIZE_TINY 16
21 #define DEF_BUFF_SIZE_V_SMALL 32
22 #define DEF_BUFF_SIZE_SMALL 64
23 #define DEF_BUFF_SIZE 128
24 #define DEF_BUFF_SIZE_BIG 256
25 #define DEF_BUFF_SIZE_LARGE 512
26 #define DEF_BUFF_SIZE_HUGE 1024
27 #define DEF_BUFF_SIZE_EXTRA 2048
28 
29 #define S2_MIN_SAMPLES_IN 50
30 #define S2_MAX_SAMPLES_IN 5000
31 #define S2_MIN_TIMEOUT_IN 1
32 #define S2_MAX_TIMEOUT_IN 60
33 #define S3_MIN_FILTER_NUMBER 0
34 #define S6_MIN_SAMPLES_IN 10
35 #define S6_MAX_SAMPLES_IN 1000
36 
37 #define UNIT_SEL_CNT 3
38 #define S2_MEASURE_OTPS_CNT 2
39 #define S3_FILTER_OPT_CNT 2
40 #define S6_UNIT_SEL_CNT 2
41 #define S6_ROLE_CNT 3
42 #define S6_MONTH_CNT 12
43 
44 #define DAY_MIN 1
45 #define DAY_MAX 31
46 #define YEAR_MIN 1900
47 
48 #define HX_CONT_MAX_CONSEC_FAIL_FI 5
49 #define HX_CONT_MAX_CONSEC_FAIL_FM 5
50 #define HX_CONT_SAMPLES 5000
51 
52 #define HX_TEST_CONST_IT_COUNT 5
53 #define HX_TEST_INC_IT_COUNT 5
54 #define HX_TEST_SAMP_COUNT 160
55 #define HX_TEST_TIME_COUNT 2
56 
57 #define S5_TO_S6_CALIBRATION 0
58 #define S5_TO_S6_USERADD 1
59 #define S5_TO_S6_HX_TEST_SAMPLES 2
60 #define S5_TO_S6_HX_TEST_TIMEOUT 3
61 #define S5_TO_S6_KB_DELAY_TEST 4
62 
63 #define S5_CALIBRATION_APPLIED 0
64 #define S5_USER_CREATED 1
65 #define S5_FINISHED_KB_DELAYS 2
66 
71 namespace app_workspace_ns {
78  INPUT
79  };
80 
86  NONE, // neutral state before correct kb_input_type is determined, should'nt be used otherwise
87  INT,
89  TEXT
90  };
91 
96  typedef struct {
97  kb_input_mode current_mode = NAVIGATION;
98  kb_input_type current_type = NONE;
100 
105  enum font_size {
108  BIG_FONT = 48
109  };
110 
115  typedef struct {
116  std::string dev;
117  std::unique_ptr<spi_config_t> config = std::make_unique<spi_config_t>();
118  } spi_config;
119 
124  typedef struct {
125  int data_pin;
127  int rate;
128  int ref_unit;
129  int offset;
130  } hx711_config;
131 };
132 
137 typedef struct {
138  std::string opt_conf_path;
139  std::string fonts_path;
140  std::string font;
141  std::string lang_path;
142  std::string lang_default;
143  std::string lang_current;
144  std::vector<std::string> lang_options;
146  bool log_debug;
152 } app_config;
153 
160  public:
161  /*************************************************************************************************************/
162  /* Properties */
163  /*************************************************************************************************************/
164 
169  std::unique_ptr<app_workspace_ns::kb_input_state> kb_in_state =
170  std::make_unique<app_workspace_ns::kb_input_state>();
175  std::unique_ptr<custom_hx711> hx711_controller;
180  std::unique_ptr<db_driver> db_conn;
181 
191  bool capslock_flag = false, lshift_flag = false;
192 
197  std::unique_ptr<app_config> main_config = std::make_unique<app_config>();
198 
203  bool user_logged = false;
208  std::unique_ptr<user_workspace> userspace;
209 
211  bool hx_measuring = false;
212  bool hx_continuous = false;
213  bool hx_not_finished_yet = false;
214  std::chrono::time_point<std::chrono::high_resolution_clock> m_start, m_end; // m = measuring (hx - scale)
221  std::unique_ptr<measurement> observed_measurement;
222 
227  std::map<app_workspace_ns::font_size, ImFont*> loaded_fonts;
233  int font_push_cnt = 0;
234 
237 
241 
246  unsigned long s3_filter_number = 0;
247 
249  int selected_unit_option = 1; // by default values are in G
250  // unit_selection MUST have an array with corresponding labels (this is currently in screen_definitions.cpp:)
252  const HX711::Mass::Unit unit_selection[UNIT_SEL_CNT] =
253  {HX711::Mass::Unit::MG, HX711::Mass::Unit::G, HX711::Mass::Unit::KG};
254 
258 
264  std::string s6_ref_mass_str;
265 
266  HX711::Value zero_value = 0, ref_unit = 0;
267 
268  bool s6_rfid_indicator = false;
269  std::unique_ptr<rfid_reader::rfid_tag> s6_rfid_tag;
276  int s6_year_in = 2022, s6_day_in = 1;
277 
278 
279 
281  int int_test = 0;
282  float float_test = 0.0f;
285 
287 
288  bool test_hx_testing = false; // true while testing in progress
293 
294  /*************************************************************************************************************/
295  /* Function prototypes */
296  /*************************************************************************************************************/
297 
302  app_workspace();
308  void init_labels();
314  int init_hx711_controller();
319  void hx711_tare();
324  void hx711_calibrate();
331  int hx711_measure();
343  int open_db_connection();
350  void set_err_screen_and_activate(const char* title, const char* label);
371  int load_font_size(app_workspace_ns::font_size key, float size_px);
379  int log_in_user_rfid(rfid_reader::rfid_tag *tag, bool loading_sub = false);
385  int verify_user_cred();
390  void log_in_user_cred();
396  void login_subuser();
401  void logout_subuser();
406  void logoff_user();
412  int register_new_user();
413 
420  std::vector<double> normalize_raw_values(std::vector<HX711::Value>& raw_vals);
428  std::vector<HX711::Value> hx711_values_samples(int samples);
436  std::vector<HX711::Value> hx711_values_timeout(int seconds);
437 
443  static std::unique_ptr<app_workspace> &get_instance();
444 
456  void set_scr_mgr(screen_manager *scr_mgr);
463  bool is_debug_screens();
464 
472  bool has_user();
474 
475 
476  // debug / tests
478  void print_config();
481  int test_hx_samples();
484  int test_hx_timeout();
485 
487  bool get_kb_testing();
489  void set_kb_testing(bool val);
490 
491  private:
493  std::unique_ptr<std::thread> measuring_watchdog;
495  std::unique_ptr<std::thread> measuring_thread;
497  std::unique_ptr<std::thread> calib_thread;
499  static std::unique_ptr<app_workspace> instance;
501  screen_manager *scr_mgr;
504 
505  void clear_s1_buffers(); // zero buffers used on screen 1
506  void clear_s2_buffers(); // zero buffers used on screen 2
507  void clear_s6_buffers(); // zero buffers used on screen 6
508 
509 };
510 
511 #endif
#define UNIT_SEL_CNT
Definition: app_workspace.h:37
#define S6_ROLE_CNT
Definition: app_workspace.h:41
#define DEF_BUFF_SIZE
Definition: app_workspace.h:23
#define DEF_BUFF_SIZE_EXTRA
Definition: app_workspace.h:27
#define DEF_BUFF_SIZE_SMALL
Definition: app_workspace.h:22
#define S2_MIN_TIMEOUT_IN
Definition: app_workspace.h:31
#define S2_MIN_SAMPLES_IN
Definition: app_workspace.h:29
#define S6_MIN_SAMPLES_IN
Definition: app_workspace.h:34
#define HX_TEST_CONST_IT_COUNT
Definition: app_workspace.h:52
#define HX_TEST_INC_IT_COUNT
Definition: app_workspace.h:53
#define S6_MONTH_CNT
Definition: app_workspace.h:42
#define S2_MEASURE_OTPS_CNT
Definition: app_workspace.h:38
#define S6_UNIT_SEL_CNT
Definition: app_workspace.h:40
#define S3_FILTER_OPT_CNT
Definition: app_workspace.h:39
One of the most importat classes in the whole project. Holds variables that define the state of the a...
int hx711_measure()
Performs hx711 measuring with set values of either samples or timeout. The results is saved to the da...
std::chrono::time_point< std::chrono::high_resolution_clock > m_start
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...
std::unique_ptr< custom_hx711 > hx711_controller
HX711 controller. This is an instance of custom_hx711 which extends library's AdvancedHX711.
std::string s6_ref_mass_str
void logoff_user()
Logs out user and subuser if logged in.
bool capslock_flag
Flags used during text input.
static std::unique_ptr< app_workspace > & get_instance()
Get the instance app_workspace which is a singleton.
HX711::Value zero_value
bool s6_calibration_finished
user_workspace * get_userspace()
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 fon...
std::chrono::time_point< std::chrono::high_resolution_clock > m_end
void set_err_screen_and_activate(const char *title, const char *label)
Set the err screen and activate the screen on next frame.
std::unique_ptr< app_workspace_ns::kb_input_state > kb_in_state
Input state holder.
bool s6_rfid_indicator
const HX711::Mass::Unit unit_selection[UNIT_SEL_CNT]
void init_labels()
This is called after localisation dictionary is loaded. After the dict is loaded arrays defined in ap...
void hx711_tare()
Function that performs taring on the hx711 controller.
int s5_screen_6_indicator
const char * s6_month_labels[S6_MONTH_CNT]
int selected_hx_result_unit
std::unique_ptr< db_driver > db_conn
Database driver holder.
std::unique_ptr< app_config > main_config
Application config loaded from app_config.conf (main config file).
int register_new_user()
This function validets screen 6 buffers and if everything checks out then inserts new user into the D...
const char * s6_role_labels[S6_ROLE_CNT]
int selected_unit_option
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.
std::pair< long, std::string > test_hx_samples_times[HX_TEST_CONST_IT_COUNT+HX_TEST_INC_IT_COUNT]
int open_db_connection()
Tries to open the database connection and init db_driver.
int load_font_size(app_workspace_ns::font_size key, float size_px)
Initializes font into the map.
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 Gram...
char s6_passwd_in[DEF_BUFF_SIZE_SMALL]
char usr_passwd[DEF_BUFF_SIZE_SMALL]
void logout_subuser()
Logs out subuser.
std::unique_ptr< measurement > observed_measurement
This variable is used to observe measuring.
const char * s3_filter_opt_labels[S3_FILTER_OPT_CNT]
app_workspace()
Construct a new app workspace object.
void hx711_calibrate()
Function that performs calibration on the hx711 controller.
char pass_test[DEF_BUFF_SIZE_SMALL]
bool hx_not_finished_yet
int hx711_continuous_measure()
Starts hx711 measuring that will run until its stopped by user.
app_workspace_ns::font_size get_font_size()
Get the font size.
bool is_debug_screens()
Retruns flag is.
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...
std::pair< long, std::string > test_hx_timeout_collected[HX_TEST_CONST_IT_COUNT+HX_TEST_INC_IT_COUNT]
char s6_name_in[DEF_BUFF_SIZE]
char s6_lastname_in[DEF_BUFF_SIZE_SMALL]
const char * unit_sel_labels[UNIT_SEL_CNT]
bool has_user()
Checks if user is logged in. This is determined by user_space being initialized and having the user_l...
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...
int selected_hx_measure_opt
int verify_user_cred()
Tests if user entered correct credentials.
screen_manager * get_scr_mgr()
Get the screen manager instance. This instance is kind of singleton. Is initalized only once on start...
std::map< app_workspace_ns::font_size, ImFont * > loaded_fonts
Map of loaded fonts.
char subusr_name[DEF_BUFF_SIZE]
void set_kb_testing(bool val)
unsigned long s3_filter_number
void set_scr_mgr(screen_manager *scr_mgr)
Set the screen manager instancte. This should only be called once, when the application is starting u...
std::unique_ptr< rfid_reader::rfid_tag > s6_rfid_tag
char usr_name[DEF_BUFF_SIZE]
void login_subuser()
When a user is already logged in and their role is employee or admin, they can log in another user as...
const char * s6_unit_sel_labels[S6_UNIT_SEL_CNT]
bool refresh_current_screen
When this is set to true, elements of current screen will be reinitialized for next frame.
HX711::Value ref_unit
int init_hx711_controller()
Initializes the hx711 controller.
bool user_logged
Flag to indicate if a user is logged in.
int s5_success_indicator
char s6_uname_in[DEF_BUFF_SIZE_SMALL]
std::unique_ptr< user_workspace > userspace
User's workspace. Serves similar function as app_workspace but for user data.
char text_test[DEF_BUFF_SIZE]
void log_in_user_cred()
Logs in user using credentials. Credentials are gained from screen_1 buffers.
int s6_selected_unit_option
char s6_desc_in[DEF_BUFF_SIZE_EXTRA]
const char * hx_measure_opts[S2_MEASURE_OTPS_CNT]
Container that servers for storing users data and manipulating them.
This namespace holds structures and enums used in app_workspace class.
Definition: app_workspace.h:71
font_size
This enum defines sizes of corresponding fonts. E.g.: SMALL_FONT is 12px.
kb_input_type
Enum holding options of keyboard INPUT mode types.
Definition: app_workspace.h:85
kb_input_mode
Enum holding options of keyboard input mode.
Definition: app_workspace.h:76
std::unique_ptr< spi_config_t > spi_config
Definition: keyboard.cpp:34
Structure that holds the values of the application configuration file.
app_workspace_ns::spi_config keyb_conf
std::string lang_default
std::vector< std::string > lang_options
bool init_fail_quit
std::string font
std::string opt_conf_path
std::string lang_path
app_workspace_ns::hx711_config hx_conf
bool debug_screens
std::string fonts_path
app_workspace_ns::spi_config rfid_conf
std::string lang_current
db_driver_conf db_conf
Structure hodling the configuration of hx711 controller.
Structure holding the values of keyboard input mode and input type.
Definition: app_workspace.h:96
Structure holding SPI device and SPI config which is a library structure "spi_config_t".
Container for configuration neccessary to open database connnection.
Definition: db_driver.h:16
RFID tag structure. Stores data read from SPI.
Definition: rfid_reader.h:12