/*
 * user_config.h
 *
 *  Created on: Jun 12, 2019
 *      Author: true
 */

#ifndef USER_CONFIG_H_
#define USER_CONFIG_H_



#include "misc/checksum.h"



// some global options
#define FLASH_RSVD_PAGES                8           // how many FLASH_UCONF_BYTES to reserve/check
#define FLASH_UCONF_BYTES               256         // basically assumed everywhere

#define MISC_WIGGLE_RATE                (7 - 4);
#define MISC_WIGGLE_SHIFT               4

// system state stuff
#define SYS_OLED_ROTATE_X               (1 << 0)
#define SYS_OLED_ROTATE_Y               (1 << 1)
#define SYS_OLED_REVERSE_CHARS          (1 << 2)    // todo: figure out wtf this is
#define SYS_OLED_ROTATE_CHANGED         (1 << 7)

// user configuration stuff
#define UCONF_VER                       0x04

#define UCONF_FLAGS_AUTOROTATE_ENA      (1 << 0)
#define UCONF_FLAGS_AD_BANNER           (1 << 1)    // show conference banner text
#define UCONF_FLAGS_LEDS_DISABLE        (1 << 3)    // used by programs to temporarily disable LEDs
#define UCONF_FLAGS_FAVOR_SPEED         (1 << 4)    // todo: increase clock rather than reduce framerate
#define UCONF_FLAGS_LEDS_ENABLE         (1 << 5)    // used by user in options menu to enable / disable RGBLEDs
#define UCONF_FLAGS_SHOW_CPU_USAGE      (1 << 6)
#define UCONF_FLAGS_SHOW_ACCEL_ANGLE    (1 << 7)

#define UCONF_FRAMERATE_FULL            40          // this is set/used at runtime, makes no difference in saved config
#define UCONF_FRAMERATE_HALF            40          // take the tickrate divided by this for the framerate.

#define UCONF_NAME_MAXLEN               15

#define UCONF_NAME_MODE_ROTATE180       (1 << 0)    // force flipping the display over
#define UCONF_NAME_MODE_AUTOROTATE      (1 << 1)    // automatically flip the display
#define UCONF_NAME_MODE_HALFWIDTH       (1 << 2)    // render only every other line of fonts
#define UCONF_NAME_MODE_COLOR_INVERT    (1 << 3)    // use pixel invert instead of set

#define UCONF_NAME_DISP_MASK            0x70
#define UCONF_NAME_DISP_COUNT           8
#define UCONF_NAME_DISP_OFFSET          4
#define UCONF_NAME_DISP_STATIC_HORIZ    0x00        // unchanging name
#define UCONF_NAME_DISP_STATIC_VERT     0x10        // unchanging name, each char rotated 90deg
#define UCONF_NAME_DISP_WIGGLE          0x20        // sine-wiggling name
#define UCONF_NAME_DISP_CHAR_ROTATE     0x30        // auto-rotating characters
#define UCONF_NAME_DISP_CHAR_ROTATE_WIG 0x40        // auto-rotating characters plus wiggle (not implemented)
#define UCONF_NAME_DISP_DEMOWAVE1       0x50        // waving letters like in demoscene stuff
#define UCONF_NAME_DISP_DEMOWAVE2       0x60



// this must be FLASH_UCONF_BYTES long for assumptions in code to work out
typedef struct UserConf {
    uint8_t  ver;
    uint8_t  flags;
    uint8_t  framemod;
    uint8_t  nameconf;                          // 4
    uint32_t iter;                              // 8
    uint8_t  font_idx;
    int8_t   char_spacing;                      // 10
    int8_t   y_offset;                          // 11
    uint8_t  pad12;                             // 12
    char     name[UCONF_NAME_MAXLEN + 1];       // 28
    char     name2[UCONF_NAME_MAXLEN + 1];      // 44
    uint8_t  favcolor_hue;
    uint8_t  favcolor_sat;
    uint8_t  favcolor_val;                      // 47
    uint8_t  altcolor_hue;
    uint8_t  altcolor_sat;
    uint8_t  altcolor_val;                      // 50
    uint8_t  ledprog_rgb_idx;
    uint8_t  padding0;                          // 52
    uint8_t  ledprog_rgb[16];                   // 68
    uint8_t  ledprog_rgb_data[16][8];           // 196
    uint8_t  padding1[48];                      // 244
    uint16_t lsens_dark_thresh;                 // 246
    uint16_t menu_timeout;                      // 248
    uint16_t dim_timeout;                       // 250
    uint16_t sleep_timeout;                     // 252
    uint16_t tempcx10_offset;                   // 253-254
    uint16_t checksum;                          // 255-256
} UserConf;



extern UserConf uconf;
extern uint8_t sysflags;

extern uint8_t batt_volt;
extern uint8_t batt_mv;

extern uint8_t temp_degc;
extern uint8_t temp_degc_decimal;



// non-volatile functions
void uconf_load();
void uconf_write();
void uconf_clear();

// volatile functions
void uconf_defaults();



#endif /* USER_CONFIG_H_ */
