Skip to content

Commit

Permalink
Phonebook Applet (#154)
Browse files Browse the repository at this point in the history
* Phonebook library for #144.

* Phonebook applet for #144.

* Phonebook now supports fall-through.  Now + is the next line, 0 is the next word. #144
  • Loading branch information
travisgoodspeed committed Feb 28, 2021
1 parent 1be6672 commit 906f09f
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 5 deletions.
5 changes: 5 additions & 0 deletions firmware/Makefile
Expand Up @@ -3,6 +3,7 @@ ALARM_APP = 1
COUNTDOWN_APP = 0
CALIBRATE_APP = 1
RPN_APP = 1
PHONEBOOK_APP = 1
HEX_APP = 1
STOPWATCH_APP = 1
PHRASE_APP = 0
Expand Down Expand Up @@ -53,6 +54,10 @@ ifeq ($(RPN_APP),1)
APPS_OBJ += apps/rpn.o
APPS_DEFINES += RPN_APP
endif
ifeq ($(PHONEBOOK_APP),1)
APPS_OBJ += apps/phonebook.o libs/phonebook.o
APPS_DEFINES += PHONEBOOK_APP
endif
ifeq ($(HEX_APP),1)
APPS_OBJ += apps/hex.o
APPS_DEFINES += HEX_APP
Expand Down
1 change: 1 addition & 0 deletions firmware/api.h
Expand Up @@ -38,6 +38,7 @@
#include "libs/assembler.h"
#include "libs/morse.h"
#include "libs/pocsag.h"
#include "libs/phonebook.h"

//Standalone functions.

Expand Down
9 changes: 6 additions & 3 deletions firmware/applist.c
Expand Up @@ -82,6 +82,12 @@ const struct app subapps[]={
},
#endif

#ifdef PHONEBOOK_APP
{.name="phonbook", .init=phonebook_init, .draw=phonebook_draw, .exit=phonebook_exit,
.keypress=phonebook_keypress, .fallthrough=phonebook_fallthrough
},
#endif


#ifdef SHABBAT_APP
//Kosher applet for Shabbat that disables all inputs except the SET button.
Expand All @@ -94,9 +100,6 @@ const struct app subapps[]={
#ifdef HEBREW_APP
//Hebrew Calendar applet. Falls through so that it can run from the clock.
{.name="hebrew", .fallthrough=hebrew_keypress
//.init=hebrew_init, .draw=hebrew_draw, .exit=hebrew_exit,
//.keypress=hebrew_keypress,

},
#endif

Expand Down
1 change: 1 addition & 0 deletions firmware/applist.h
Expand Up @@ -22,6 +22,7 @@ extern const struct app clock_applet;
#include "apps/hebrew.h"
#include "apps/dmesg.h"
#include "apps/beats.h"
#include "apps/phonebook.h"

//Then radio apps.
#include "apps/morse.h"
Expand Down
73 changes: 73 additions & 0 deletions firmware/apps/phonebook.c
@@ -0,0 +1,73 @@
/*! \file phonebook.h
\brief Phonebook application.
*/

#include "api.h"

//! Pointer to the current phonebook entry.
static const char* pbptr;


//! Initialize the phonebook.
void phonebook_init(){
pbptr=phonebook;
}

//! Draw the Phonebook
void phonebook_draw(int forced){
//Draw the word.
lcd_string(pbptr);
}


//! Exits the phonebook.
int phonebook_exit(){
//Return 0 so we can move back to the clock.
return 0;
}


//! A button has been pressed for the phonebook.
int phonebook_keypress(char ch){
switch(ch){
//Third (fallthrough) row shows one word each of the current row.
case '1':
pbptr=pb_firstword(pbptr);
break;
case '2':
pbptr=pb_firstword(pbptr);
pbptr=pb_nextword(pbptr);
break;
case '3':
pbptr=pb_firstword(pbptr);
pbptr=pb_nextword(pbptr);
pbptr=pb_nextword(pbptr);
break;
case '-':
pbptr=pb_firstword(pbptr);
pbptr=pb_nextword(pbptr);
pbptr=pb_nextword(pbptr);
pbptr=pb_nextword(pbptr);
break;


//Bottom row moves the line or entry.
case '0':
pbptr=pb_nextword(pbptr);
break;
case '+':
pbptr=pb_nextline(pbptr);
break;
default:
return 0; //no need to redraw
}
return 1; //redraw
}



//! A fallthrough button in the third row for the phonebook.
int phonebook_fallthrough(char ch){
phonebook_draw(phonebook_keypress(ch));
return 0;
}
21 changes: 21 additions & 0 deletions firmware/apps/phonebook.h
@@ -0,0 +1,21 @@
/*! \file phonebook.h
\brief Phonebook application.
*/


#ifndef PHONEBOOK
#error "PHONEBOOK is undefined in config.h. Are you using an old template?"
#endif

//! Initialize the phonebook.
void phonebook_init();
//! Draw the Phonebook
void phonebook_draw(int forced);
//! Exits the phonebook.
int phonebook_exit();

//! A button has been pressed for the phonebook.
int phonebook_keypress(char ch);

//! A fallthrough button in the third row for the phonebook.
int phonebook_fallthrough(char ch);
11 changes: 11 additions & 0 deletions firmware/configtemplate.h
Expand Up @@ -41,3 +41,14 @@
"\x86\xd9" "\x00\x00\xe8\xe8\xee\x88\xe8\x8e\xe8\x88\xee\xe8\x8e\x88\x80\x00", \
"\x86\xd9" "\x00\x00\xe8\xe8\xee\x88\xe8\x8e\xe8\x88\xee\xe8\xe8\x88\x80\x00"

/* This array holds the phonebook. Each line is one record, with the
0 button jumping to the next line and the +/- keys cycling between
the 8-byte words of the line.
*/


#define PHONEBOOK \
"Travis " "555-3811" "\n" \
"Bob 555" "555-0321" "\n" \
"AAA 800" "222-4357" "\n" \
"Last " "555-2358" "\n"
1 change: 1 addition & 0 deletions firmware/libs/.gitignore
Expand Up @@ -3,3 +3,4 @@ pocsag
hebrew
jukebox
beats
phonebook
8 changes: 6 additions & 2 deletions firmware/libs/Makefile
@@ -1,14 +1,15 @@
# This is just for testing the libraries. They are build with
# firmware/Makefile when running in the watch.

EXECS= assembler pocsag jukebox hebrew beats
EXECS= assembler pocsag jukebox hebrew beats phonebook

run: all
./assembler
./pocsag
./jukebox
./hebrew
./beats
./phonebook

clean:
rm -rf *.o $(EXECS)
Expand All @@ -28,4 +29,7 @@ hebrew: hebrew.c hebrew.h
$(CC) -Werror -DSTANDALONE -o hebrew $<

beats: beats.c beats.h
$(CC) -Werror -DSTANDALONE -o beats $<
$(CC) -Werror -DSTANDALONE -o beats $<

phonebook: phonebook.c phonebook.h
$(CC) -Werror -DSTANDALONE -o phonebook $<
102 changes: 102 additions & 0 deletions firmware/libs/phonebook.c
@@ -0,0 +1,102 @@
#include "phonebook.h"

#ifndef STANDALONE
#include "api.h"
#endif

//Fake phonebook only in standalone mode.
#ifdef STANDALONE
#define PHONEBOOK \
"Travis " "555-3811" "\n" \
"bob " "555-0321" "\n" \
"Jim " "555-9379" "\n" \
"Last " "555-2358" "\n"



#include<stdint.h>
#include<stdio.h>
#include<string.h>

int main(){
const char* p=phonebook;
char w[9]=" ";
for(int i=0; i<10; i++){
for(int j=0; j<10; j++){
memcpy(w,p,8);
printf("\t_%s_",w);
p=pb_nextword(p);
}
printf("\n");
p=pb_nextline(p);
}
return 0;
}

#endif



//! This is variable for the phonebook.
const char phonebook[] = PHONEBOOK;



//! Given a pointer, this goes back to the beginning of the line.
const char* pb_firstword(const char* word){
while(word-->phonebook){
//One step forward from a newline.
if(*word=='\n')
return word+1;
}

return phonebook;
}


//! Given a pointer, it finds the very next word while looping to the first of a line.
const char* pb_nextword(const char* word){
//Step forward eight letters, unless we find a terminator.
for(int i=0; i<8; i++){
word++;

//On a null or newline, jump back to the beginning of the line.
if(!*word || *word=='\n')
return pb_firstword(word);
}

//One more to check.
word++;
switch(*word){
case '\n':
case 0:
//If a newline or null, we jump back to the start.
return pb_firstword(--word);
default:
return --word;
}
}


//! Given a pointer, this finds the next line.
const char* pb_nextline(const char* word){
while(phonebook < ++word +sizeof(phonebook)){
//Revert to the first line on a null.
if(!*word)
return phonebook;


//A newline is either preceeded by more text or by a null.
if(*word=='\n'){
word++;

if(*word)//Return the text.
return word;
else//Revert to the first line on a null.
return phonebook;
}
}

//Jump back to the beginning when we go too far.
return phonebook;
}
13 changes: 13 additions & 0 deletions firmware/libs/phonebook.h
@@ -0,0 +1,13 @@
//! Given a pointer, this goes back to the beginning of the line.
const char* pb_firstword(const char* word);


//! Given a pointer, it finds the very next word while looping to the first of a line.
const char* pb_nextword(const char* word);

//! Given a pointer, this finds the next line.
const char* pb_nextline(const char* word);


//! This is variable for the phonebook.
extern const char phonebook[];

0 comments on commit 906f09f

Please sign in to comment.