Landfall

Annotated pkg/landfall-examples/qi_lb60_backlight/qi_lb60_backlight.cc

229:810ca800993b
11 months ago Paul Boddie Employ a picture representation compatible with 16-bit SPI transfers over DMA. cpm-library-improvements
paul@0 1
/*
paul@0 2
 * Access the keypad to modify the backlight on the Ben NanoNote.
paul@0 3
 *
paul@104 4
 * Fn+Volume Down/Up adjusts the brightness.
paul@104 5
 * Red Arrow+Volume Down/Up disables/enables the backlight.
paul@104 6
 *
paul@153 7
 * Copyright (C) 2018, 2020, 2023 Paul Boddie <paul@boddie.org.uk>
paul@0 8
 *
paul@0 9
 * This program is free software; you can redistribute it and/or
paul@0 10
 * modify it under the terms of the GNU General Public License as
paul@0 11
 * published by the Free Software Foundation; either version 2 of
paul@0 12
 * the License, or (at your option) any later version.
paul@0 13
 *
paul@0 14
 * This program is distributed in the hope that it will be useful,
paul@0 15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@0 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@0 17
 * GNU General Public License for more details.
paul@0 18
 *
paul@0 19
 * You should have received a copy of the GNU General Public License
paul@0 20
 * along with this program; if not, write to the Free Software
paul@0 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
paul@0 22
 * Boston, MA  02110-1301, USA
paul@0 23
 */
paul@0 24
paul@0 25
#include <l4/devices/input-keypad-client.h>
paul@38 26
#include <l4/devices/keypad-loader.h>
paul@0 27
paul@104 28
#include <l4/re/env.h>
paul@0 29
paul@0 30
#include <l4/re/event_enums.h>
paul@0 31
#include <l4/util/util.h>
paul@0 32
paul@104 33
#include "activation_client.h"
paul@104 34
#include "backlight_client.h"
paul@104 35
paul@153 36
paul@153 37
paul@0 38
/* Backlight level. */
paul@0 39
paul@0 40
enum Qi_lb60_backlight_levels
paul@0 41
{
paul@0 42
  Qi_lb60_backlight_min_level = 0,
paul@0 43
  Qi_lb60_backlight_max_level = 255,
paul@0 44
  Qi_lb60_backlight_step = 10,
paul@0 45
};
paul@0 46
paul@0 47
static int backlight_level = 64;
paul@0 48
paul@0 49
/* Key state. */
paul@0 50
paul@0 51
static int modifier_set = 0;
paul@20 52
static int enable_modifier_set = 0;
paul@0 53
paul@0 54
/* Backlight device abstractions. */
paul@0 55
paul@104 56
static Activation *activation_device;
paul@104 57
static Backlight *backlight_device;
paul@0 58
paul@0 59
paul@0 60
paul@0 61
/* Input event handler. */
paul@0 62
paul@0 63
static void handler(Input_event event, void *priv)
paul@0 64
{
paul@0 65
  (void) priv;
paul@0 66
paul@20 67
  /* Track the state of the modifier keys. */
paul@0 68
paul@0 69
  if (event.code == L4RE_KEY_FN)
paul@0 70
    modifier_set = event.value;
paul@20 71
  else if (event.code == L4RE_KEY_RIGHTALT)
paul@20 72
    enable_modifier_set = event.value;
paul@0 73
paul@20 74
  if (!event.value || !(modifier_set || enable_modifier_set))
paul@0 75
    return;
paul@0 76
paul@0 77
  /* Upon keypress events, test controls and update the backlight. */
paul@0 78
paul@0 79
  switch (event.code)
paul@0 80
  {
paul@0 81
    case L4RE_KEY_VOLUMEDOWN:
paul@20 82
      if (enable_modifier_set)
paul@20 83
      {
paul@104 84
        activation_device->disable();
paul@20 85
        return;
paul@20 86
      }
paul@20 87
      else if (backlight_level < Qi_lb60_backlight_min_level + Qi_lb60_backlight_step)
paul@0 88
        backlight_level = Qi_lb60_backlight_min_level;
paul@0 89
      else
paul@0 90
        backlight_level -= Qi_lb60_backlight_step;
paul@0 91
      break;
paul@0 92
paul@0 93
    case L4RE_KEY_VOLUMEUP:
paul@20 94
      if (enable_modifier_set)
paul@20 95
      {
paul@104 96
        activation_device->enable();
paul@20 97
        return;
paul@20 98
      }
paul@20 99
      else if (backlight_level > Qi_lb60_backlight_max_level - Qi_lb60_backlight_step)
paul@0 100
        backlight_level = Qi_lb60_backlight_max_level;
paul@0 101
      else
paul@0 102
        backlight_level += Qi_lb60_backlight_step;
paul@0 103
      break;
paul@0 104
paul@0 105
    default: return;
paul@0 106
  }
paul@0 107
paul@0 108
  /* Use the backlight device to update the backlight level. */
paul@0 109
paul@0 110
  backlight_device->set_brightness(backlight_level);
paul@0 111
}
paul@0 112
paul@0 113
paul@0 114
paul@0 115
int main(void)
paul@0 116
{
paul@38 117
  // Load the keypad details from the configured library.
paul@38 118
paul@153 119
  Keypad_generic *keypad = (Keypad_generic *) load_keypad();
paul@0 120
  Input_keypad_client client(keypad);
paul@0 121
paul@0 122
  /* Obtain a reference to the backlight device. */
paul@0 123
paul@104 124
  l4_cap_idx_t backlight = l4re_env_get_cap("backlight");
paul@153 125
paul@153 126
  if (!l4_is_valid_cap(backlight))
paul@153 127
    return 1;
paul@104 128
paul@104 129
  /* NOTE: The component framework should permit the use of a compound client
paul@104 130
           object. */
paul@104 131
paul@104 132
  client_Activation activation_obj(backlight);
paul@104 133
  activation_device = &activation_obj;
paul@104 134
paul@104 135
  client_Backlight backlight_obj(backlight);
paul@104 136
  backlight_device = &backlight_obj;
paul@0 137
paul@0 138
  client.attach(handler, 0);
paul@0 139
paul@0 140
  l4_sleep_forever();
paul@0 141
paul@0 142
  return 0;
paul@0 143
}