Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | /* microbit.c - BBC micro:bit specific hooks */
/*
* Copyright (c) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <gpio.h>
#include <board.h>
#include <soc.h>
#include <misc/printk.h>
#include <ctype.h>
#include <gpio.h>
#include <pwm.h>
#include <display/mb_display.h>
#include <bluetooth/mesh.h>
#include "board.h"
#define SCROLL_SPEED K_MSEC(300)
#define BUZZER_PIN EXT_P0_GPIO_PIN
#define BEEP_DURATION K_MSEC(60)
#define SEQ_PER_BIT 976
#define SEQ_PAGE (NRF_FICR->CODEPAGESIZE * (NRF_FICR->CODESIZE - 1))
#define SEQ_MAX (NRF_FICR->CODEPAGESIZE * 8 * SEQ_PER_BIT)
static struct device *gpio;
static struct device *nvm;
static struct device *pwm;
static struct k_work button_work;
static void button_send_pressed(struct k_work *work)
{
printk("button_send_pressed()\n");
board_button_1_pressed();
}
static void button_pressed(struct device *dev, struct gpio_callback *cb,
u32_t pins)
{
struct mb_display *disp = mb_display_get();
if (pins & BIT(SW0_GPIO_PIN)) {
k_work_submit(&button_work);
} else {
u16_t target = board_set_target();
if (target > 0x0009) {
mb_display_print(disp, MB_DISPLAY_MODE_SINGLE,
K_SECONDS(2), "A");
} else {
mb_display_print(disp, MB_DISPLAY_MODE_SINGLE,
K_SECONDS(2), "%X", (target & 0xf));
}
}
}
static const struct {
char note;
u32_t period;
u32_t sharp;
} period_map[] = {
{ 'C', 3822, 3608 },
{ 'D', 3405, 3214 },
{ 'E', 3034, 3034 },
{ 'F', 2863, 2703 },
{ 'G', 2551, 2407 },
{ 'A', 2273, 2145 },
{ 'B', 2025, 2025 },
};
static u32_t get_period(char note, bool sharp)
{
int i;
if (note == ' ') {
return 0;
}
for (i = 0; i < ARRAY_SIZE(period_map); i++) {
if (period_map[i].note != note) {
continue;
}
if (sharp) {
return period_map[i].sharp;
} else {
return period_map[i].period;
}
}
return 1500;
}
void board_play_tune(const char *str)
{
while (*str) {
u32_t period, duration = 0U;
while (*str && !isdigit((unsigned char)*str)) {
str++;
}
while (isdigit((unsigned char)*str)) {
duration *= 10U;
duration += *str - '0';
str++;
}
if (!*str) {
break;
}
if (str[1] == '#') {
period = get_period(*str, true);
str += 2;
} else {
period = get_period(*str, false);
str++;
}
if (period) {
pwm_pin_set_usec(pwm, BUZZER_PIN, period, period / 2U);
}
k_sleep(duration);
/* Disable the PWM */
pwm_pin_set_usec(pwm, BUZZER_PIN, 0, 0);
}
}
void board_heartbeat(u8_t hops, u16_t feat)
{
struct mb_display *disp = mb_display_get();
const struct mb_image hops_img[] = {
MB_IMAGE({ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 }),
MB_IMAGE({ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 0, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 }),
MB_IMAGE({ 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1 }),
MB_IMAGE({ 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1 }),
MB_IMAGE({ 1, 0, 1, 0, 1 },
{ 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 0 },
{ 1, 0, 1, 0, 1 })
};
printk("%u hops\n", hops);
if (hops) {
hops = MIN(hops, ARRAY_SIZE(hops_img));
mb_display_image(disp, MB_DISPLAY_MODE_SINGLE, K_SECONDS(2),
&hops_img[hops - 1], 1);
}
}
void board_other_dev_pressed(u16_t addr)
{
struct mb_display *disp = mb_display_get();
printk("board_other_dev_pressed(0x%04x)\n", addr);
mb_display_print(disp, MB_DISPLAY_MODE_SINGLE, K_SECONDS(2),
"%X", (addr & 0xf));
}
void board_attention(bool attention)
{
struct mb_display *disp = mb_display_get();
static const struct mb_image attn_img[] = {
MB_IMAGE({ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 }),
MB_IMAGE({ 0, 0, 0, 0, 0 },
{ 0, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 0 },
{ 0, 0, 0, 0, 0 }),
MB_IMAGE({ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 0, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 }),
MB_IMAGE({ 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1 }),
};
if (attention) {
mb_display_image(disp,
MB_DISPLAY_MODE_DEFAULT | MB_DISPLAY_FLAG_LOOP,
K_MSEC(150), attn_img, ARRAY_SIZE(attn_img));
} else {
mb_display_stop(disp);
}
}
static void configure_button(void)
{
static struct gpio_callback button_cb;
k_work_init(&button_work, button_send_pressed);
gpio = device_get_binding(SW0_GPIO_CONTROLLER);
gpio_pin_configure(gpio, SW0_GPIO_PIN,
(GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE |
GPIO_INT_ACTIVE_LOW));
gpio_pin_configure(gpio, SW1_GPIO_PIN,
(GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE |
GPIO_INT_ACTIVE_LOW));
gpio_init_callback(&button_cb, button_pressed,
BIT(SW0_GPIO_PIN) | BIT(SW1_GPIO_PIN));
gpio_add_callback(gpio, &button_cb);
gpio_pin_enable_callback(gpio, SW0_GPIO_PIN);
gpio_pin_enable_callback(gpio, SW1_GPIO_PIN);
}
void board_init(u16_t *addr)
{
struct mb_display *disp = mb_display_get();
nvm = device_get_binding(DT_FLASH_DEV_NAME);
pwm = device_get_binding(CONFIG_PWM_NRF5_SW_0_DEV_NAME);
*addr = NRF_UICR->CUSTOMER[0];
if (!*addr || *addr == 0xffff) {
#if defined(NODE_ADDR)
*addr = NODE_ADDR;
#else
*addr = 0x0b0c;
#endif
}
mb_display_print(disp, MB_DISPLAY_MODE_DEFAULT, SCROLL_SPEED,
"0x%04x", *addr);
configure_button();
}
|