Little Robot Friends
Arduino Library Reference  version 1.1
LRF.cpp
1 //
2 // LRF.cpp
3 // LittleRobotFriends
4 //
5 // Created by Mark Argo on 2014-05-20.
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 "LRF.h"
25 #include "LRFHardware.h"
26 #include "LRFSpeech.h"
27 #include "LRFEyes.h"
28 #include "LRFSensors.h"
29 #include "LRFEvents.h"
30 #include "LRFInfrared.h"
31 #include "LRFPower.h"
32 #include "LRFExpressions.h"
33 #include "LRFPersonality.h"
34 #include "LRFMemory.h"
35 #include "LRFDebug.h"
36 #include "LRFGames.h"
37 
38 const char debug_title[] = "lrf";
39 
41 
42 //-------------------------------------------------------------
43 #pragma mark - Move these eventually?
44 //-------------------------------------------------------------
45 
46 void lrf_print_hardware_version(void)
47 {
48  unsigned char major = lrf_memory_read(LRF_MEMORY_HARDWARE_VERSION_MAJOR_ADDR);
49  unsigned char minor = lrf_memory_read(LRF_MEMORY_HARDWARE_VERSION_MINOR_ADDR);
50  lrf_debug_tag(debug_title, "hw:", false);
51  lrf_debug_pair(major, minor);
52 }
53 
54 void lrf_print_firmware_version(void)
55 {
56  unsigned char major = lrf_memory_read(LRF_MEMORY_FIRMWARE_VERSION_MAJOR_ADDR);
57  unsigned char minor = lrf_memory_read(LRF_MEMORY_FIRMWARE_VERSION_MINOR_ADDR);
58  lrf_debug_tag(debug_title, "fw:", false);
59  lrf_debug_pair(major, minor);
60 }
61 
62 void lrf_reset_test_memories(void)
63 {
64  lrf_memory_write(LRF_MEMORY_HARDWARE_TEST_ADDR, 0);
65  lrf_memory_write(LRF_MEMORY_HAS_READ_SHOELACES_ADDR, 0);
66 }
67 
68 void lrf_implant_memories(bool isHoliday)
69 {
70  // write hardware version
71  if(isHoliday)
72  {
73  lrf_memory_write(LRF_MEMORY_HARDWARE_VERSION_MAJOR_ADDR, 0x01);
74  lrf_memory_write(LRF_MEMORY_HARDWARE_VERSION_MINOR_ADDR, 0x00);
75  }
76  else
77  {
78  lrf_memory_write(LRF_MEMORY_HARDWARE_VERSION_MAJOR_ADDR, 0x01);
79  lrf_memory_write(LRF_MEMORY_HARDWARE_VERSION_MINOR_ADDR, 0x01);
80  }
81 
82  // write firmware version
83  lrf_memory_write(LRF_MEMORY_FIRMWARE_VERSION_MAJOR_ADDR, LRF_FIRMWARE_MAJOR);
84  lrf_memory_write(LRF_MEMORY_FIRMWARE_VERSION_MINOR_ADDR, LRF_FIRMWARE_MINOR);
85 
86  // write hardware test
87  lrf_memory_write(LRF_MEMORY_HARDWARE_TEST_ADDR, LRF_MEMORY_HARDWARE_TEST_VALUE);
88 
89  // write shoelaces read
90  lrf_memory_write(LRF_MEMORY_HAS_READ_SHOELACES_ADDR, LRF_MEMORY_HAS_READ_SHOELACES_VALUE);
91 
92  // write personality
93  lrf_memory_write(LRF_MEMORY_PERSONALITY_START_ADDR, LRF_PERSONALITY_LEVEL_NORMAL);
94  lrf_memory_write(LRF_MEMORY_PERSONALITY_START_ADDR+1, LRF_PERSONALITY_LEVEL_NORMAL);
95  lrf_memory_write(LRF_MEMORY_PERSONALITY_START_ADDR+2, LRF_PERSONALITY_LEVEL_NORMAL);
96  lrf_memory_write(LRF_MEMORY_PERSONALITY_START_ADDR+3, LRF_PERSONALITY_LEVEL_NORMAL);
97  lrf_memory_write(LRF_MEMORY_PERSONALITY_START_ADDR+4, LRF_PERSONALITY_LEVEL_NORMAL);
98  lrf_memory_write(LRF_MEMORY_PERSONALITY_START_ADDR+5, LRF_PERSONALITY_LEVEL_NORMAL);
99 
100  // write signature
101  lrf_memory_write(LRF_MEMORY_SIGNATURE_ADDR, 1);
102 }
103 
104 //-------------------------------------------------------------
105 #pragma mark - Setup & Loop
106 //-------------------------------------------------------------
107 
109 {
110  // setup the serial output
111  Serial.begin(9600);
112 
113  // setup all our libraries
114  lrf_hardware_setup();
115 
116  // holiday hardware test
117  unsigned int holiday = (lrf_memory_read(0x08) << 8) | lrf_memory_read(0x09);
118  if(holiday == 0xffff)
119  {
120  lrf_debug_tag(debug_title, "holiday");
121  lrf_implant_memories(true);
122  }
123 
124  // firmware version check
125  unsigned char major = lrf_memory_read(LRF_MEMORY_FIRMWARE_VERSION_MAJOR_ADDR);
126  unsigned char minor = lrf_memory_read(LRF_MEMORY_FIRMWARE_VERSION_MINOR_ADDR);
127  if(major != LRF_FIRMWARE_MAJOR || minor != LRF_FIRMWARE_MINOR)
128  {
129  lrf_memory_write(LRF_MEMORY_FIRMWARE_VERSION_MAJOR_ADDR, LRF_FIRMWARE_MAJOR);
130  lrf_memory_write(LRF_MEMORY_FIRMWARE_VERSION_MINOR_ADDR, LRF_FIRMWARE_MINOR);
131  }
132 
133  // print our hardware & firmware versions
134  lrf_print_hardware_version();
135  lrf_print_firmware_version();
136  delay(50);
137 
138  // test our hardware
139  if(LRF_FORCE_HARDWARE_TEST || (lrf_memory_read(LRF_MEMORY_HARDWARE_TEST_ADDR) != LRF_MEMORY_HARDWARE_TEST_VALUE))
140  {
141 #if LRF_ADVANCED_HARDWARE_TEST
142  lrf_hardware_test_advanced();
143 #else
144  lrf_hardware_test();
145 #endif
146  }
147 
148 
149  // setup the power functionality
150  lrf_power_setup();
151 
152  // Setup the personality & expressions
153  lrf_personality_setup();
154  lrf_expressions_setup();
155 
156  // load saved settings from memory
157  lrf_memory_load();
158 
159  // setup other libraries
160  lrf_sensors_setup();
161  lrf_events_setup();
162  lrf_irda_setup();
163 
164  // global enable of interrupts
165  interrupts();
166 
167 #if LRF_DEBUG_CONSOLE_ENABLED
168  lrf_debug_console();
169 #endif
170 
171  // Let's do our power-up message
172  lrf_events_add(LRFEvent_PowerUp);
173 }
174 
176 {
177  // let's sense the world
178  lrf_sensors_process();
179 
180  // did we recognize anything different?
181  lrf_events_process();
182 
183  // if so, then let's react
184  lrf_expressions_process();
185 
186  // if we need to send some ir data, let's do it here
187  lrf_irda_process();
188 
189  // and finally check our power state
190  lrf_power_process();
191 }
192 
193 
194 //-------------------------------------------------------------
195 #pragma mark - Expressing
196 //-------------------------------------------------------------
197 
199 {
200  lrf_eyes_play_pattern_and_block(pattern,false);
201 }
202 
203 void LittleRobotFriend::blink(LRFPattern pattern, unsigned int duration)
204 {
205  lrf_eyes_play_pattern_duration_and_block(pattern,duration,false);
206 }
207 
209 {
210  lrf_speech_play_sound_and_block(sound,false);
211 }
212 
214 {
215  lrf_expressions_load_pattern_and_sounds_and_block(pattern, &sound, 1);
216 }
217 
218 void LittleRobotFriend::blinkAndSay(LRFPattern pattern, LRFSound *sounds, char soundCount)
219 {
220  lrf_expressions_load_pattern_and_sounds_and_block(pattern, sounds, soundCount);
221 }
222 
223 //-------------------------------------------------------------
224 #pragma mark - Events
225 //-------------------------------------------------------------
226 
228 {
229  lrfEventHandlers[event] = handler;
230 }
231 
232 //-------------------------------------------------------------
233 #pragma mark - Infrared
234 //-------------------------------------------------------------
235 
237 {
238  return lrfIrda.message;
239 }
240 
241 void LittleRobotFriend::sendIRMessage(LRFIRMessage message)
242 {
243  lrf_irda_write_message(message);
244 }
245 
246 //-------------------------------------------------------------
247 #pragma mark - Sensors
248 //-------------------------------------------------------------
249 
251 {
252  unsigned char value;
253  lrf_touch_read(&value);
254  return value;
255 }
256 
258 {
259  unsigned char value;
260  lrf_light_read(&value);
261  return value;
262 }
263 
265 {
266  unsigned char value;
267  lrf_mic_read(&value);
268  return value;
269 }
270 
271 //-------------------------------------------------------------
272 #pragma mark - Outputs
273 //-------------------------------------------------------------
274 
275 void LittleRobotFriend::setLeftLED(unsigned char red, unsigned char green, unsigned char blue, bool update)
276 {
277  lrfEyes.currentLeft.red = (red < LRF_RGB_VALUE_MAX) ? red : LRF_RGB_VALUE_MAX;
278  lrfEyes.currentLeft.green = (green < LRF_RGB_VALUE_MAX) ? green : LRF_RGB_VALUE_MAX;
279  lrfEyes.currentLeft.blue = (blue < LRF_RGB_VALUE_MAX) ? blue : LRF_RGB_VALUE_MAX;
280  if(update) lrf_eyes_update();
281 }
282 
283 void LittleRobotFriend::setRightLED(unsigned char red, unsigned char green, unsigned char blue, bool update)
284 {
285  lrfEyes.currentRight.red = (red < LRF_RGB_VALUE_MAX) ? red : LRF_RGB_VALUE_MAX;
286  lrfEyes.currentRight.green = (green < LRF_RGB_VALUE_MAX) ? green : LRF_RGB_VALUE_MAX;
287  lrfEyes.currentRight.blue = (blue < LRF_RGB_VALUE_MAX) ? blue : LRF_RGB_VALUE_MAX;
288  if(update) lrf_eyes_update();
289 }
290 
291 void LittleRobotFriend::setBothLEDs(unsigned char red, unsigned char green, unsigned char blue, bool update)
292 {
293  setRightLED(red, green, blue, false);
294  setLeftLED(red, green, blue, false);
295  if(update) lrf_eyes_update();
296 }
297 
298 void LittleRobotFriend::setSpeaker(unsigned int frequency, unsigned int duration)
299 {
300  lrf_speaker_on();
301  lrf_speaker_set_frequency(frequency);
302  delay(duration);
303  lrf_speaker_off();
304 }
305 
306 //-------------------------------------------------------------
307 #pragma mark - System
308 //-------------------------------------------------------------
309 
310 void LittleRobotFriend::setBoredom(unsigned int timeout, unsigned char count)
311 {
312  lrfEvents.boredTimeoutReload = timeout * 1000;
313  lrfEvents.boredCountReload = count;
314 }
315 
317 {
318  lrf_power_sleep();
319 }
320 
321 
322 
323 
324 
325 
326 
327 
328 
unsigned char readLight(void)
Read the light sensor.
Definition: LRF.cpp:257
unsigned char readTouch(void)
Read the touch sensor.
Definition: LRF.cpp:250
void setBoredom(unsigned int timeout, unsigned char count)
Set how quickly and how often your robot gets bored.
Definition: LRF.cpp:310
void loop(void)
run the LRF library loop
Definition: LRF.cpp:175
Allows for access of sound data as a structure or raw integer.
Definition: LRFUtils.h:307
void sleep(void)
Put the robot to sleep.
Definition: LRF.cpp:316
void setRightLED(unsigned char red, unsigned char green, unsigned char blue, bool update=true)
Set the RGB value of one or both LEDs.
Definition: LRF.cpp:283
The main class of the Little Robot Friends API.
Definition: LRF.h:62
LRFEvent
Constants for different interaction and system events.
void blink(LRFPattern pattern)
Blink a light pattern on the LED eyes.
Definition: LRF.cpp:198
This is the core API class for access LRF library functions.
void setBothLEDs(unsigned char red, unsigned char green, unsigned char blue, bool update=true)
Set the RGB value of one or both LEDs.
Definition: LRF.cpp:291
void setLeftLED(unsigned char red, unsigned char green, unsigned char blue, bool update=true)
Set the RGB value of one or both LEDs.
Definition: LRF.cpp:275
void sendIRMessage(LRFIRMessage message)
Send an IR message.
Definition: LRF.cpp:241
void say(LRFSound sound)
Speak a single sound.
Definition: LRF.cpp:208
LRFIRMessage readIRMessage(void)
Read the latest received IR message.
Definition: LRF.cpp:236
void(* LRFEventHandler)(void)
Function pointer with no parameters and no return value.
Just powered on (batteries in)
unsigned char readMicrophone(void)
Read the microphone.
Definition: LRF.cpp:264
void setup(void)
setup the LRF library
Definition: LRF.cpp:108
void blinkAndSay(LRFPattern pattern, LRFSound sound)
Blink and speak at the same time.
Definition: LRF.cpp:213
Allows for access of pattern data as a structure or raw integer.
Definition: LRFUtils.h:156
void setEventHandler(LRFEvent event, LRFEventHandler handler)
Customize how your robot responds to particular events.
Definition: LRF.cpp:227