Arch Cookbook

This is a part of Arch Cookbook.

Recipe 7: Working with RTC

Ingredients

In addition to things listed in Recipe 1, we require

  • Grove - Serial LCD
  • Grove - RTC

Procedure

  • Connect Grove - Serial LCD to on-board grove connector marked UART.
  • Connect Grove - RTC to on board grove connector markded I2C.
  • Build and upload the program to Arch platform. follow procedure listed in Recipe 1.

The following program demonstrates a watch.

Import program

00001 #include "mbed.h"
00002 #include "SerialLCD.h"
00003 #include "ds1307.h"
00004  
00005 SerialLCD lcd(P1_13, P1_14);  // Grove Serial LCD is connected to UART Tx and Rx pins
00006 DS1307 rtc(P0_5, P0_4);    // Grove RTC is connected to I2C SDA(P0_5) and SCL(P0_4)
00007  
00008 int main() {
00009     const char *week[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
00010     int sec, min, hour, day, date, month, year;
00011     char strBuffer[16];
00012     
00013     lcd.begin();
00014     lcd.print("Clock");
00015     rtc.start_clock();
00016     rtc.gettime(&sec, &min, &hour, &day, &date, &month, &year);
00017     if (0 == year) {
00018         rtc.settime(0, 0, 0, 4, 1, 1, 2014 % 100); // Jan 1st, 2014, Wed, 00:00:00
00019     }
00020 
00021     while(1) {
00022         rtc.gettime(&sec, &min, &hour, &day, &date, &month, &year);
00023         snprintf(strBuffer, sizeof(strBuffer), "%d-%d-%d  %s", 2000 + year, month, date, week[day]);
00024         lcd.setCursor(0, 0);
00025         lcd.print(strBuffer);
00026         snprintf(strBuffer, sizeof(strBuffer), "%d:%d:%d", hour, min, sec);
00027         lcd.setCursor(0, 1);
00028         lcd.print(strBuffer);
00029         wait(0.5);
00030     }
00031 }
00032 
00033 

Recipe 8: Working with USB Keyboard

Ingredients

In addition to things listed in Recipe 1, we require

  • Grove - Button

Procedure

  • Connect Grove - Button to on-board grove connector marked UART.
  • Build and upload the program to Arch platform. follow procedure listed in Recipe 1.

The following program demonstrates a USB keyboard with one button. Press the button to mute your computer and press CapsLock, NumLock or ScrollLock of your keyboard to turn on/off Arch's LEDs.

Import program

00001 #include "mbed.h"
00002 #include "USBKeyboard.h"
00003 
00004 //LED1: NUM_LOCK, LED2: CAPS_LOCK, LED3: SCROLL_LOCK
00005 BusOut leds(LED1, LED2, LED3);
00006 DigitalOut button(P1_14);               // Configure P1_14 pin as input
00007 USBKeyboard keyboard;
00008 
00009 int main() {
00010     int buttonPressedCount = 0; 
00011     
00012     while (!keyboard.configured()) {    // wait until keyboard is configured
00013     }
00014     
00015     while (1) {
00016         leds = keyboard.lockStatus();
00017         
00018         if (button.read()) {
00019             buttonPressedCount++;
00020             if (2 == buttonPressedCount) { // when button is pressed about 0.02s
00021                 keyboard.mediaControl(KEY_MUTE); // send mute key
00022             }
00023         } else {
00024             buttonPressedCount = 0;
00025         }
00026         wait(0.01);
00027     }
00028 }

The following program implements an automatic input keyboard triggered by CapsLock key (for Windows). When the CapsLock key is pressed, a website is opened by IE.

Import program

00001 #include "mbed.h"
00002 #include "USBKeyboard.h"
00003  
00004 //LED1: NUM_LOCK, LED2: CAPS_LOCK, LED3: SCROLL_LOCK
00005 BusOut leds(LED1, LED2, LED3);
00006 USBKeyboard keyboard;
00007  
00008 int main(void) {
00009     uint8_t caps;                       // status of CapsLock
00010     
00011     while (!keyboard.configured()) {    // wait until keyboard is configured
00012     }
00013  
00014     while (1) {
00015         leds = keyboard.lockStatus();
00016         caps = keyboard.lockStatus() & 0x2;
00017         
00018         // wait until CapsLock is pressed
00019         while ((keyboard.lockStatus() & 0x2) == caps) {
00020             leds = keyboard.lockStatus();
00021         }
00022         
00023         if (!caps) {
00024             keyboard.keyCode(KEY_CAPS_LOCK);    // lowercase input
00025         }
00026         
00027         // Automatic input
00028         keyboard.keyCode('r', 0x08);            // win + r
00029         wait(0.1);
00030         keyboard.puts("iexplore  http://seeedstudio.com\n\n");
00031     }
00032 }


Report

Please log in to post comments.