Little Robot Friends
Arduino Library Reference  version 1.1
LRFUtils.cpp
1 //
2 // LRFUtils.cpp
3 // LittleRobotFriends
4 //
5 // Created by Mark Argo on 2014-05-01.
6 // Copyright (c) 2014 Aesthetec Studio Inc. All rights reserved.
7 //
8 
9 /*
10  THIS SOFTWARE IS PROVIDED “AS IS”, WITHOUT ANY REPRESENTATIONS, CONDITIONS,
11  AND/OR WARRANTIES OF ANY KIND. WITHOUT LIMITATION, AESTHETEC STUDIO AND ITS
12  AFFILIATES, LICENSORS, SUPPLIERS, CONTRIBUTORS, SUBCONTRACTORS, DISTRIBUTORS
13  AND ALL CONTRIBUTORS DISCLAIM ANY EXPRESS OR IMPLIED REPRESENTATIONS,
14  CONDITIONS, OR WARRANTIES OF MERCHANTABILITY, MERCHANTABLE QUALITY, SATISFACTORY
15  QUALITY, NON-INFRINGEMENT, TITLE, DURABILITY, OR FITNESS FOR A PARTICULAR
16  PURPOSE, WHETHER ARISING BY STATUTE, COURSE OF DEALING, USAGE OF TRADE, OR
17  OTHERWISE. EXCEPT AS OTHERWISE PROVIDED IN THIS AGREEMENT, YOU SHALL BEAR
18  THE ENTIRE RISK FOR ANY USE OR ANY OTHER EXPLOITATION MADE BY YOU OF ANY
19  RIGHTS IN THE COVERED SOFTWARE.
20 
21  Additional copyright information found at http://littlerobotfriends.com/legal/
22 */
23 
24 #include "LRFUtils.h"
25 #include "LRFHardware.h"
26 #include "LRFMemory.h"
27 
28 #define LRF_DURATION_BASE_UNIT 50
29 
30 #define LRF_DURATIONS_COUNT 8
31 #define LRF_SCALE_COUNT 12
32 #define LRF_COLORS_COUNT 16
33 
34 const unsigned int PROGMEM lrfDurations[LRF_DURATIONS_COUNT] = {
35  0,
36  LRF_DURATION_BASE_UNIT * 1, // short
37  LRF_DURATION_BASE_UNIT * 2, // double short
38  LRF_DURATION_BASE_UNIT * 4, // medium
39  LRF_DURATION_BASE_UNIT * 8, // double medium
40  LRF_DURATION_BASE_UNIT * 16, // long
41  LRF_DURATION_BASE_UNIT * 32, // double long
42  LRF_DURATION_BASE_UNIT * 64, // very long
43 };
44 
45 const unsigned int PROGMEM lrfScale[LRF_SCALE_COUNT] = {
46  55, 58, 62, 65, 69, 73, 78, 82, 87, 92, 98, 104
47 };
48 
49 const unsigned char PROGMEM lrfColors[LRF_COLORS_COUNT * 3] = {
50  0,0,0, // blank
51  LRF_RGB_VALUE_HIGH,0,0, // red
52  0,LRF_RGB_VALUE_HIGH,0, // green
53  0,0,LRF_RGB_VALUE_HIGH, // blue
54  LRF_RGB_VALUE_HIGH,LRF_RGB_VALUE_MED,0, // orange
55  0,LRF_RGB_VALUE_HIGH,LRF_RGB_VALUE_HIGH, // cyan
56  LRF_RGB_VALUE_SOFT,0,LRF_RGB_VALUE_HIGH, // purple
57 
58  LRF_RGB_VALUE_MED,LRF_RGB_VALUE_HIGH,0, // yellow
59  LRF_RGB_VALUE_HIGH,0,LRF_RGB_VALUE_MED, // pink
60 
61  LRF_RGB_VALUE_SOFT,0,0, // soft red
62  0,LRF_RGB_VALUE_SOFT,0, // soft green
63  0,0,LRF_RGB_VALUE_SOFT, // soft blue
64  LRF_RGB_VALUE_SOFT,LRF_RGB_VALUE_SOFT,0, // soft orange
65  0,LRF_RGB_VALUE_SOFT,LRF_RGB_VALUE_SOFT, // soft cyan
66  LRF_RGB_VALUE_SOFT,0,LRF_RGB_VALUE_SOFT, // soft pink
67  LRF_RGB_VALUE_MED,LRF_RGB_VALUE_MED,LRF_RGB_VALUE_MED // white
68 };
69 
70 bool lrf_utils_is_holiday_hardware(void)
71 {
72  if(lrf_memory_read(LRF_MEMORY_HARDWARE_VERSION_MAJOR_ADDR) == 0x01 &&
73  lrf_memory_read(LRF_MEMORY_HARDWARE_VERSION_MINOR_ADDR) == 0x00)
74  return true;
75  else
76  return false;
77 }
78 
80 {
81  unsigned char color = 1 + (rand()%8);
82  lrf_utils_color_lookup((LRFColor)color, value);
83 }
84 
86 {
87  unsigned char idx = color * 3;
88  value->red = pgm_read_byte(&lrfColors[idx]);
89  value->green = pgm_read_byte(&lrfColors[idx+1]);
90  value->blue = pgm_read_byte(&lrfColors[idx+2]);
91 }
92 
93 unsigned int lrf_utils_duration_lookup(LRFDuration duration)
94 {
95  return pgm_read_word(&lrfDurations[(unsigned char)duration]);
96 }
97 
99 {
100  unsigned int freq;
101 
102 //#if LRF_SPEECH_DEBUG
103 // Serial.print("n2f:");
104 // Serial.print(note,DEC);
105 // Serial.print(",");
106 // Serial.print(octave,DEC);
107 //#endif
108 
109  if(note > LRFNote_GS) note = LRFNote_GS;
110  freq = pgm_read_word(&lrfScale[note]);
111 
112 //#if LRF_SPEECH_DEBUG
113 // Serial.print("=");
114 // Serial.print(freq,DEC);
115 //#endif
116 
117  freq *= pow(2,octave);
118 
119 //#if LRF_SPEECH_DEBUG
120 // Serial.print("=");
121 // Serial.print(freq,DEC);
122 //#endif
123 
124  freq = F_CPU / freq / 2 - 1;
125 
126 //#if LRF_SPEECH_DEBUG
127 // Serial.print("=");
128 // Serial.print(freq,DEC);
129 //#endif
130 
131  return freq;
132 }
LRFColor
Constants for looking up color values stored in program memory (PROGMEM)
Definition: LRFUtils.h:46
LRFDuration
Constants for fixed note durations based on musical timingWe found that a short list of fixed duratio...
Definition: LRFUtils.h:234
void lrf_utils_color_lookup(LRFColor color, LRFColorValue *value)
Utility for looking up LRFColorValue data from LRFColor constant.
Definition: LRFUtils.cpp:85
lowest possible octave (some notes are unplayable)
Definition: LRFUtils.h:200
LRFNote
Constants for notes in the musical scale.
Definition: LRFUtils.h:174
unsigned char blue
Blue value (8-bits)
Definition: LRFUtils.h:74
Data structure for holding RGB color values.
Definition: LRFUtils.h:70
void lrf_utils_color_lookup_random(LRFColorValue *value)
Utility for generating a random LRFColorValue.
Definition: LRFUtils.cpp:79
unsigned int lrf_utils_note_to_frequency(LRFNote note, LRFOctave octave)
Utility for looking up frequency values from LRFNote and LRFOctave constants.
Definition: LRFUtils.cpp:98
unsigned char green
Green value (8-bits)
Definition: LRFUtils.h:73
Collection of custom types and handy functions used throughout the LRF codebase.
G# (104hz)
Definition: LRFUtils.h:187
LRFOctave
Constants for octaves in the musical scale.
Definition: LRFUtils.h:198
unsigned int lrf_utils_duration_lookup(LRFDuration duration)
Utility for looking up duration values (in milliseconds) from a LRFDuration constant.
Definition: LRFUtils.cpp:93
unsigned char red
Red value (8-bits)
Definition: LRFUtils.h:72