|
It is currently Wed Jul 06, 2011 7:24 pm
|
View unanswered posts | View active topics
 |
|
 |
|
| Author |
Message |
|
number3
|
Post subject: My first app! "Sticks Clock"  Posted: Sat Apr 09, 2011 6:02 pm |
Joined: Wed Apr 06, 2011 8:53 pm Posts: 32
|
My very first app! Yay! This one displays the time in a whimsical way, with sticks! It's surprisingly readable. Thanks to Ryan Young whose code I used as a starting point, and I reused the line routine he put in there. Enjoy! /** * Sticks clock * * Description: * Count the sticks to know the time. The sticks at the top are the hours, * the ones at the bottom are the minutes. A full row represents 20 minutes. * * Thanks to Ryan Young whose code I used as a starting point. * Single pixel thick anti-aliased lines are drawn using Xiaolin Wu's algorithm. * The processor will go to sleep in 15 seconds if there is no activity. * * Copyright (C) 2011, Allerta Inc. * Author: JP Martin & Ryan Young ([email protected]) * * Permission to use, copy, modify, and/or distribute this software for * any purpose with or without fee is hereby granted, provided that the * above copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS * SOFTWARE. **/
#include <pulse_os.h> #include <pulse_types.h>
#include <stdint.h>
#define SLEEP_TIMEOUT 15000
uint8_t current_min;
uint8_t current_hour;
struct pulse_time_tm current_time;
uint32_t sleep_timer = -1;
color24_t hand_color = {113, 217, 226, 0};
color24_t tick_color = {0, 123, 167, 0};
typedef struct point { uint8_t x; uint8_t y; } point;
point center = {47, 63};
void swap(uint8_t *a, uint8_t *b) { uint8_t temp = *a; *a = *b; *b = temp; }
color24_t adjust_intensity(color24_t color, uint8_t intensity) { color24_t temp = {color.red * intensity / 255, color.green * intensity / 255, color.blue * intensity / 255, 0}; return temp; }
void draw_wu_line(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, color24_t color) { //Draw 2 endpoints pulse_set_draw_window(x0, y0, x0, y0); pulse_draw_point24(color); pulse_set_draw_window(x1, y1, x1, y1); pulse_draw_point24(color);
//Negative slope bool negative = (y0 > y1) ? true : false;
if(negative) { swap(&y0, &y1); } if(x0 > x1) { swap(&x0, &x1); negative ^= true; } uint8_t dx = x1 - x0; uint8_t dy = y1 - y0;
if(!dx) //Vertical line { pulse_set_draw_window(x0, y0 + 1, x0, y1 - 1); for(int i = 1; i < dy; i++) { pulse_draw_point24(color); } } else if(!dy) //Horizontal line { pulse_set_draw_window(x0 + 1, y0, x1 - 1, y0); for(int i = 1; i < dx; i++) { pulse_draw_point24(color); } } else if(dx == dy) //45 degree line { unsigned char dy = (negative) ? -1 : 1; unsigned y = (negative) ? y1 : y0; for(int i = 1; i < dx; i++) { x0++; y += dy; pulse_set_draw_window(x0, y, x0, y); pulse_draw_point24(color); } } else //Other line { //Slope is > 1 or not bool steep = (dy > dx) ? true : false; if(steep) { swap(&x0, &y0); swap(&x1, &y1); swap(&dx, &dy); }
//Distance from ideal pixel center uint8_t D = 0;
//Stores last pixel's distance uint8_t lastD = 0;
//Distance increment uint8_t d = dy * 256 / dx;
//Temp coordinates used to plot pixels uint8_t a0, a1, b0, b1;
//Temp coordinate offset uint8_t da, db;
while(x0 < x1) { x0++;; x1--; D += d;
//Check for overflow if(lastD > D) { y0++; y1--; } lastD = D; a0 = x0; b0 = y0; a1 = x1; b1 = y1; da = 0; db = 1; if(negative & ~steep) { swap(&a0, &a1); } else if(negative & steep) { swap(&a0, &b0); swap(&a1, &b1); swap(&b0, &b1); swap(&da, &db); } else if(~negative & steep) { swap(&a0, &b0); swap(&a1, &b1); swap(&da, &db); }
//Draw 4 points pulse_set_draw_window(a0, b0, a0, b0); pulse_draw_point24(adjust_intensity(color, ~D)); pulse_set_draw_window(a0 + da, b0 + db, a0 + da, b0 + db); pulse_draw_point24(adjust_intensity(color, D)); pulse_set_draw_window(a1, b1, a1, b1); pulse_draw_point24(adjust_intensity(color, ~D)); pulse_set_draw_window(a1 - da, b1 - db, a1 - da, b1 - db); pulse_draw_point24(adjust_intensity(color, D)); } } }
void draw_sticks(int y, int height, int spacing, int howmany) { int x,i; x = center.x - spacing*(howmany)/2; for (i=0;i<howmany;i++) { if (i>0 && (i+1)%5==0) { draw_wu_line(x-spacing*5, y+height, x, y, hand_color); } else { draw_wu_line(x, y, x, y+height, hand_color); } x += spacing; } }
void draw_hands() { int i,j,y; //draw_wu_line(center.x, center.y, min_hand[current_min].x, min_hand[current_min].y, hand_color); //draw_wu_line(center.x, center.y, hour_hand[4 * (current_hour % 12) + current_min / 15].x, // hour_hand[4 * (current_hour % 12) + current_min / 15].y, hand_color); y = (center.y*2-60)/2-10; draw_sticks(y, 20, 4, current_hour%12); y = center.y*2-60; for (j = current_min; j>0; ) { i = j; if (i>20) i=20; draw_sticks(y, 15, 3, i); j -= i; y += 20; } }
void erase_hands() { }
void draw_time() { pulse_cancel_timer(&sleep_timer); pulse_get_time_date(¤t_time); if(current_hour != current_time.tm_hour) { current_hour = current_time.tm_hour; } if(current_min != current_time.tm_min) { current_min = current_time.tm_min; } pulse_blank_canvas(); draw_hands(); //for(int i = 0; i < 16; i++) //{ // draw_wu_line(Rtick[i].x, Rtick[i].y, rtick[i].x, rtick[i].y, tick_color); //} sleep_timer = pulse_register_timer(SLEEP_TIMEOUT, &pulse_update_power_down_timer, 0); }
void draw_spinning_time() { erase_hands(); pulse_get_time_date(¤t_time); uint8_t temp_min = current_time.tm_min; uint8_t temp_hour = current_time.tm_hour % 12; current_min = 0; current_hour = 0; while(temp_min != current_min || temp_hour != current_hour) { draw_hands(); erase_hands(); current_min++; if(current_min == 60) { current_min = 0; current_hour++; } } draw_hands(); }
void main_app_init() { float x; x = sin(12.0); current_min = 0; current_hour = 0; draw_time(); pulse_register_callback(ACTION_WOKE_FROM_BUTTON, &draw_time); }
void main_app_handle_button_down() { pulse_cancel_timer(&sleep_timer); draw_time(); sleep_timer = pulse_register_timer(SLEEP_TIMEOUT, &pulse_update_power_down_timer, 0); }
void main_app_handle_button_up() { }
void main_app_loop() {
}
void main_app_handle_doz() {
}
void main_app_handle_hardware_update(enum PulseHardwareEvent event) {
}
| Attachments: |
File comment: Sticks Clock showing 6:47 (slightly blurry)
IMG_4603.jpg [ 20.36 KiB | Viewed 290 times ]
|
|
|
|
|
 |
|
Johan
|
Post subject: Re: My first app! "Sticks Clock"  Posted: Sun Apr 17, 2011 6:14 pm |
Joined: Fri Mar 04, 2011 8:54 pm Posts: 82
|
|
|
|
 |
|
number3
|
Post subject: Re: My first app! "Sticks Clock"  Posted: Mon Apr 18, 2011 6:10 am |
Joined: Wed Apr 06, 2011 8:53 pm Posts: 32
|
|
|
|
 |
|
|
 |
|
 |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum
|
|