Keep pebble time watchface backlight on for longer than 3 seconds

The pebble time watchface dims pretty fast and it only lasts 3 seconds, unfortunately there is no option right to increase that. in watchface it is possible to change this behavior programatically here is how i did it.


  1.   static int seconds;
  2.   static void set_backlight(){
  3.   light_enable_interaction();
  4.   if (seconds<=4)return;
  5.   app_timer_register(3000,set_backlight, NULL);
  6.   }
  7.   static void accel_tap_handler(AccelAxisType axis,int32_t direction) {
  8.   light_enable_interaction();
  9.   seconds = 10;
  10.   app_timer_register(3000,set_backlight, NULL);
  11.   }
  12.   static void main_window_load(Window *window) {
  13.   ...
  14.   accel_tap_service_subscribe(&accel_tap_handler);
  15.   ...
  16.   }
  17.   static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
  18.   if (seconds>0)seconds--;
  19.   ...
  20.   }
  21.   static void init() {
  22.   ...
  23.   tick_timer_service_subscribe(SECOND_UNIT, tick_handler);
  24.   ...
  25.   }
  26.  

How to binary serialize an object such as Font and store it in Windows Registry then deserialize it in C#

Smart Notepad App (At least smarter than some unnamed notepads)Say you want to make a notepad like application, and you want to let the user choose the default font for the text and that should be remembered each time the user open the app. The solution is instead of saving each font property, you save the whole class object. But you need to serialize the class using Binary in order to be able to store it in Windows Registry.

Below is the class that create registry keys, and can save and serialize a font object, and deserialize it back again using IFormatter.Serialize, IFormatter.Deserialize and MemoryStream.

Main:

For C# (C-Sharp)

The registry class


  1.  using System;
  2.  using System.Collections.Generic;
  3.  using System.Text;
  4.  using Microsoft.Win32;
  5.  using System.Drawing;
  6.  using System.Runtime.Serialization;
  7.  using System.Runtime.Serialization.Formatters.Binary;
  8.  class winreg
  9.  {
  10.   private RegistryKey prgReg;
  11.   private RegistryKey recentReg;
  12.   public winreg()
  13.   {
  14.   RegistryKey reg = Registry.CurrentUser.OpenSubKey(software”, true);
  15.   RegistryKey regMilky = reg.CreateSubKey(MyBrand);
  16.   prgReg = regMilky.CreateSubKey(MySoftware);
  17.   }
  18.   public void save_font(Font fnt)
  19.   {
  20.   RegistryKey fntReg = prgReg.CreateSubKey(Font);
  21.   IFormatter formatter = new BinaryFormatter();
  22.   Stream stream = new MemoryStream();
  23.   formatter.Serialize(stream,fnt);
  24.   stream.Flush();
  25.   byte[] buffer = new byte[stream.Length];
  26.   stream.Seek(0, SeekOrigin.Begin);
  27.   stream.Read(buffer, 0, (int)stream.Length);
  28.   stream.Close();
  29.   fntReg.SetValue(FontInfo”, buffer,RegistryValueKind.Binary);
  30.   }
  31.   public bool get_font(ref Font fnt)
  32.   {
  33.   RegistryKey fntReg = prgReg.CreateSubKey(Font);
  34.   if (fntReg.GetValue(FontInfo) != null)
  35.   {
  36.   byte[] buffer = (byte[])fntReg.GetValue(FontInfo);
  37.   IFormatter formatter = new BinaryFormatter();
  38.   Stream stream = new MemoryStream(buffer);
  39.   fnt = (Font)formatter.Deserialize(stream);
  40.   stream.Close();
  41.   return true;
  42.   }else return false;
  43.   }
  44.  }

Below is how to use the above class.


  1.   winreg WinRegClass = new winreg();
  2.   Font fnt = MyTextBox.Font;
  3.   if (WinRegClass.get_font(ref fnt)!=false)
  4.   {
  5.   MyTextBox.Font = fnt;
  6.   }

This is not a fine work. I’m still practicing C#, so, don’t judge (; Yesterday I wanted to play a DOS game using DosBox, but somehow ended up practicing more C#. You see: Games=useful.