private static int BytesInHPC(Config.VisualConfig hpc) { int bytes_in_hpc_ = 0; int intlen = sizeof(Int16); int floatlen = sizeof(float); FieldInfo[] fields = hpc.GetType().GetFields(); foreach (FieldInfo field in fields) { if (field != null) { object val = field.GetValue(hpc); if (val is float) { bytes_in_hpc_ += floatlen; } else if (val is Config.ConfigValue) { Config.ConfigValue val_ = (Config.ConfigValue)val; if (val_.isInt) { bytes_in_hpc_ += intlen; } else { bytes_in_hpc_ += floatlen; } } } } return(bytes_in_hpc_); }
/********************************************/ /*private static void Test() * { * debugprints = true; * int ret = 0; * * // test read * print("Reading config..."); * Config.VisualConfig hpc0 = new Config.VisualConfig(); * ret = LoadConfigFromEEPROM(ref hpc0); * if (ret == 0) PrintConfig(hpc0); * * // test write * Config.VisualConfig hpc = new Config.VisualConfig(); * hpc.center.Value = 0f; * hpc.pitch.Value = 6.96969696f; * hpc.DPI.Value = 1.23456789f; * hpc.viewCone.Value = 12.3456789f; * hpc.verticalAngle.Value = 123.456789f; * hpc.numViews.Value = 1234.56789f; * hpc.screenH.Value = 500.69f; * hpc.configVersion = 0.1f; * print("Writing config..."); * PrintConfig(hpc); * ret = WriteConfigToEEPROM(hpc); * * // test read * Config.VisualConfig hpc2 = new Config.VisualConfig(); * print("Rereading config..."); * ret = LoadConfigFromEEPROM(ref hpc2); * if (ret == 0) PrintConfig(hpc2); * }*/ public static void PrintConfig(Config.VisualConfig h) { String s = ""; FieldInfo[] fields = h.GetType().GetFields(); foreach (FieldInfo field in fields) { if (field != null) { object val = field.GetValue(h); if (val is float) { s += String.Format("{0:G}f ", (float)val); } else if (val is Config.ConfigValue) { Config.ConfigValue val_ = (Config.ConfigValue)val; if (val_.isInt) { s += String.Format("{0:D}i ", (Int16)val_); } else { s += String.Format("{0:G}f ", (float)val_); } } } } print(s); }
public static int WriteConfigToEEPROM(Config.VisualConfig hpc) { print("Writing config to EEPROM, please wait..."); PrintConfig(hpc); byte[] JSONString = Encoding.ASCII.GetBytes(JsonUtility.ToJson(hpc, false)); UInt32 jsonlength = (UInt32)JSONString.Length; byte[] out_buf = new byte[jsonlength + 4]; out_buf[0] = (byte)(jsonlength >> 24); out_buf[1] = (byte)(jsonlength >> 16); out_buf[2] = (byte)(jsonlength >> 8); out_buf[3] = (byte)(jsonlength); print(string.Format("JSON length: {0:D}", jsonlength)); for (int i = 0; i < jsonlength; ++i) { out_buf[i + 4] = JSONString[i]; } int err = rw(EEPROM_START_PAGE, out_buf.Length, out_buf, false); if (err != 0) { printerr(String.Format("Error {0:D}: HoloPlay Config could not be written to EEPROM! Using default HoloPlay Config.", -1 * err)); return(err); } PrintArray(out_buf, (uint)out_buf.Length, 0, format: "out: {0:S}"); print("Config successfully written to EEPROM!"); return(err); }
// todo: let the user load config 2 or 3 for second displays public void LoadConfig() { Config.VisualConfig loadedConfig = new Config.VisualConfig(); if (!Config.LoadVisualFromFile(out loadedConfig, Config.visualFileName)) { // todo: print an on-screen warning about the config not being available } config = loadedConfig; }
// todo: let the user load config 2 or 3 for second displays public void LoadConfig() { Config.VisualConfig loadedConfig = new Config.VisualConfig(); int index = 0; #if MULTI_LKG index = (int)visualName; #endif if (!Config.LoadVisualFromFile(out loadedConfig, Config.visualFileNames[index])) { // todo: print an on-screen warning about the config not being available } config = loadedConfig; }
public static int LoadConfigFromEEPROM(ref Config.VisualConfig hpc) { float ver = hpc.configVersion; byte[] bytes_read = new byte[BytesInHPC(hpc)]; for (int i = 0; i < bytes_read.Length; ++i) { bytes_read[i] = 0; } byte[] in_buf = new byte[bytes_read.Length + 2]; int err = rw((byte)EEPROM_START_PAGE, in_buf.Length, in_buf, true); if (err == 0) { if (in_buf[0] == EEPROM_START_BYTE && in_buf[in_buf.Length - 1] == EEPROM_END_BYTE) { for (int i = 0; i < bytes_read.Length; ++i) { bytes_read[i] = in_buf[i + 1]; } } else { err = -5; } } if (err != 0) { printerr(String.Format("Error {0:D}: HoloPlay Config could not be loaded from EEPROM! Using default HoloPlay Config.", -1 * err)); return(err); } hpc = DeserializeHoloPlayConfig(bytes_read); if (hpc == null) { return(-7); } if (hpc.configVersion != ver) { printerr(String.Format("HoloPlay Config version mismatch. Version expected: {0:G}; version found: {1:G}. Using default HoloPlay Config. Please recalibrate.", ver, hpc.configVersion)); return(-6); } return(0); }
private static Config.VisualConfig DeserializeHoloPlayConfig(byte[] byte_in) { Config.VisualConfig hpc = new Config.VisualConfig(); if (byte_in.Length != BytesInHPC(hpc)) { printerr("HoloPlayConfig length mismatch! Aborting..."); return(null); } int intlen = sizeof(Int16); int floatlen = sizeof(float); int ind = 0; FieldInfo[] fields = hpc.GetType().GetFields(); foreach (FieldInfo field in fields) { if (field != null) { object val = field.GetValue(hpc); if (val is float) { field.SetValue(hpc, BitConverter.ToSingle(byte_in, ind)); ind += floatlen; } else if (val is Config.ConfigValue) { Config.ConfigValue val_ = (Config.ConfigValue)val; if (val_.isInt) { val_.Value = BitConverter.ToInt16(byte_in, ind); ind += intlen; } else { val_.Value = BitConverter.ToSingle(byte_in, ind); ind += floatlen; } } } } return(hpc); }
private static byte[] SerializeHoloPlayConfig(Config.VisualConfig hpc) { List <byte> byte_out = new List <byte>(); FieldInfo[] fields = hpc.GetType().GetFields(); foreach (FieldInfo field in fields) { if (field != null) { object val = field.GetValue(hpc); if (val is float) { byte[] bytes = BitConverter.GetBytes((float)val); foreach (byte b in bytes) { byte_out.Add(b); } } else if (val is Config.ConfigValue) { Config.ConfigValue val_ = (Config.ConfigValue)val; byte[] bytes; if (val_.isInt) { bytes = BitConverter.GetBytes((Int16)val_); } else { bytes = BitConverter.GetBytes((float)val_); } foreach (byte b in bytes) { byte_out.Add(b); } } } } return(byte_out.ToArray()); }
public static int WriteConfigToEEPROM(Config.VisualConfig hpc) { print("Writing config to EEPROM, please wait..."); PrintConfig(hpc); byte[] in_buf = SerializeHoloPlayConfig(hpc); byte[] bytes_out = new byte[in_buf.Length + 2]; bytes_out[0] = EEPROM_START_BYTE; bytes_out[bytes_out.Length - 1] = EEPROM_END_BYTE; for (int i = 0; i < in_buf.Length; ++i) { bytes_out[i + 1] = in_buf[i]; } int err = rw((byte)EEPROM_START_PAGE, bytes_out.Length, bytes_out, false); if (err != 0) { printerr(String.Format("Error {0:D}: HoloPlay Config could not be written to EEPROM! Using default HoloPlay Config.", -1 * err)); return(err); } print("Config successfully written to EEPROM!"); return(err); }