Ejemplo n.º 1
0
        // generate EEPROM to write to device
        public static bool GenerateEEPROMConfig(config cfg, out eep_config eep)
        {
            eep = new eep_config();

            eep.config_id = CONFIG_ID;
            eep.config_size = (UInt16)Marshal.SizeOf(typeof(eep_config));

            eep.button_mode = (byte)cfg.buttons_mode;
            eep.af_mode = (byte)cfg.af_mode;
            eep.rotary_mode = (byte)cfg.rotary_mode;

            eep.af_high_05hz = (byte)(cfg.af_high_hz * 2);
            eep.af_low_05hz = (byte)(cfg.af_low_hz * 2);
            //eep.af_high_hz_x10 = (UInt16)(cfg.af_high_hz * 10);
            //if (!calc_timer(cfg.af_high_hz, out eep.af_high_div, out eep.af_high_per))
            //	return false;
            //eep.af_low_hz_x10 = (UInt16)(cfg.af_low_hz * 10);
            //if (!calc_timer(cfg.af_low_hz, out eep.af_low_div, out eep.af_low_per))
            //	return false;

            eep.af_mask = 0;
            for (int i = 0; i < 16; i++)
            {
                if (cfg.af_mask[i])
                    eep.af_mask |= (UInt16)(1 << i);
            }

            if (cfg.start_af_toggle_mode)
                eep.start_af_toggle_mode = 0xFF;
            else
                eep.start_af_toggle_mode = 0x00;

            if (cfg.af_save_state)
                eep.af_save_state = 0xFF;
            else
                eep.af_save_state = 0;

            // calculate CRC32
            int size = Marshal.SizeOf(eep);
            byte[] buffer = new byte[size];
            IntPtr ptr = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(eep, ptr, true);
            Marshal.Copy(ptr, buffer, 0, size);
            Marshal.FreeHGlobal(ptr);
            eep.crc32 = crc32(buffer, size - 4);	// don't include the 4 byte CRC

            return true;
        }
Ejemplo n.º 2
0
        // decode EEPROM read from device
        public static config DecodeEEPROMConfig(eep_config eep, out string failure_message)
        {
            // check CRC32
            int size = Marshal.SizeOf(eep);
            byte[] buffer = new byte[size];
            IntPtr ptr = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(eep, ptr, true);
            Marshal.Copy(ptr, buffer, 0, size);
            Marshal.FreeHGlobal(ptr);
            if (eep.crc32 != crc32(buffer, size - 4))
            {
                failure_message = "Bad CRC.";
                return null;
            }

            // decode config
            var cfg = new config();

            cfg.buttons_mode = eep.button_mode;
            cfg.af_mode = eep.af_mode;
            cfg.rotary_mode = eep.rotary_mode;

            cfg.af_high_hz = eep.af_high_05hz / 2;
            cfg.af_low_hz = eep.af_low_05hz / 2;

            for (int i = 0; i < 16; i++)
            {
                if ((eep.af_mask & (1 << i)) != 0)
                    cfg.af_mask[i] = true;
                else
                    cfg.af_mask[i] = false;
            }

            if (eep.start_af_toggle_mode != 0)
                cfg.start_af_toggle_mode = true;
            else
                cfg.start_af_toggle_mode = false;

            if (eep.af_save_state != 0)
                cfg.af_save_state = true;
            else
                cfg.af_save_state = false;

            if (!config.ValidateConfig(cfg, out failure_message))
                return null;

            return cfg;
        }