a691b61822f1dca42d4af403d7bc27aa26216534
[kugel-rb.git] / apps / plugins / alarmclock.c
bloba691b61822f1dca42d4af403d7bc27aa26216534
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Clément Pit-Claudel
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "plugin.h"
23 #include "lib/pluginlib_actions.h"
25 PLUGIN_HEADER
27 const struct button_mapping *plugin_contexts[] = {generic_directions,
28 generic_actions};
30 static int current = 0;
31 static int alarm[2] = {0, 0}, maxval[2] = {24, 60};
32 static bool quit = false, usb = false, waiting = false, done = false;
34 static inline int get_button(void) {
35 return pluginlib_getaction(HZ/2, plugin_contexts, 2);
38 int rem_seconds(void) {
39 return (((alarm[0] - rb->get_time()->tm_hour) * 3600)
40 +((alarm[1] - rb->get_time()->tm_min) * 60)
41 -(rb->get_time()->tm_sec));
44 void draw_centered_string(struct screen * display, char * string) {
45 int w, h;
46 display->getstringsize(string, &w, &h);
48 if (w > display->lcdwidth || h > display->lcdheight) {
49 rb->splash(0, string);
50 } else {
51 display->putsxy((display->lcdwidth - w) / 2,
52 (display->lcdheight - h) / 2,
53 string);
54 display->update();
58 void draw(struct screen * display) {
59 char info[128];
60 display->clear_display();
62 int secs = rem_seconds();
64 if (waiting)
65 rb->snprintf(info, sizeof(info), "Next alarm in %02dh,"
66 " %02dmn, and %02ds.",
67 secs / 3600, (secs / 60) % 60, secs % 60);
68 else {
69 if (current == 0)
70 rb->snprintf(info, sizeof(info), "Set alarm at [%02d]:%02d.",
71 alarm[0],
72 alarm[1]);
73 else
74 rb->snprintf(info, sizeof(info), "Set alarm at %02d:[%02d].",
75 alarm[0],
76 alarm[1]);
78 draw_centered_string(display, info);
81 bool can_play(void) {
82 int audio_status = rb->audio_status();
83 if ((!audio_status && rb->global_status->resume_index != -1)
84 && (rb->playlist_resume() != -1)) {
85 return true;
87 else if (audio_status & AUDIO_STATUS_PAUSE)
88 return true;
90 return false;
93 void play(void) {
94 int audio_status = rb->audio_status();
95 if (!audio_status && rb->global_status->resume_index != -1) {
96 if (rb->playlist_resume() != -1) {
97 rb->playlist_start(rb->global_status->resume_index,
98 rb->global_status->resume_offset);
101 else if (audio_status & AUDIO_STATUS_PAUSE)
102 rb->audio_resume();
105 enum plugin_status plugin_start(const void* parameter)
107 int button;
108 int i;
109 (void)parameter;
111 if (!can_play()) {
112 rb->splash(4*HZ, "No track to resume! This plugin will resume a track "
113 "at a specific time. Therefore, you need to first play"
114 " one, and then pause it and start the plugin again.");
115 quit = true;
118 while(!quit) {
119 button = get_button();
121 if (button == PLA_QUIT)
122 quit = true;
124 FOR_NB_SCREENS(i) {
125 draw(rb->screens[i]);
127 if (waiting) {
128 if (rem_seconds() <= 0) {
129 quit = done = true;
130 play();
133 else {
134 switch (button) {
135 case PLA_UP:
136 case PLA_UP_REPEAT:
137 alarm[current] = (alarm[current] + 1) % maxval[current];
138 break;
140 case PLA_DOWN:
141 case PLA_DOWN_REPEAT:
142 alarm[current] = (alarm[current] + maxval[current] - 1)
143 % maxval[current];
144 break;
146 case PLA_LEFT:
147 case PLA_LEFT_REPEAT:
148 case PLA_RIGHT:
149 case PLA_RIGHT_REPEAT:
150 current = (current + 1) % 2;
151 break;
153 case PLA_FIRE: {
154 if (rem_seconds() < 0)
155 alarm[0] += 24;
157 waiting = true;
158 break;
161 default:
162 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
163 quit = usb = true;
164 break;
169 return (usb) ? PLUGIN_USB_CONNECTED
170 : (done ? PLUGIN_GOTO_WPS : PLUGIN_OK);