C++ Library for the PsiSwarm Robot - Version 0.7

Fork of PsiSwarmV7 by Psi Swarm Robot

Committer:
jah128
Date:
Thu Feb 04 21:48:54 2016 +0000
Revision:
0:d6269d17c8cf
Child:
2:c6986ee3c7c5
PsiSwarm Library Version 0.4 - Initial Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jah128 0:d6269d17c8cf 1 /* University of York Robotics Laboratory PsiSwarm Library: LED Functions Source File
jah128 0:d6269d17c8cf 2 *
jah128 0:d6269d17c8cf 3 * File: led.cpp
jah128 0:d6269d17c8cf 4 *
jah128 0:d6269d17c8cf 5 * (C) Dept. Electronics & Computer Science, University of York
jah128 0:d6269d17c8cf 6 * James Hilder, Alan Millard, Alexander Horsfield, Homero Elizondo, Jon Timmis
jah128 0:d6269d17c8cf 7 *
jah128 0:d6269d17c8cf 8 * PsiSwarm Library Version: 0.4
jah128 0:d6269d17c8cf 9 *
jah128 0:d6269d17c8cf 10 * February 2016
jah128 0:d6269d17c8cf 11 *
jah128 0:d6269d17c8cf 12 *
jah128 0:d6269d17c8cf 13 */
jah128 0:d6269d17c8cf 14
jah128 0:d6269d17c8cf 15 #include "psiswarm.h"
jah128 0:d6269d17c8cf 16
jah128 0:d6269d17c8cf 17 char green_led_states;
jah128 0:d6269d17c8cf 18 char red_led_states;
jah128 0:d6269d17c8cf 19 char center_led_state;
jah128 0:d6269d17c8cf 20
jah128 0:d6269d17c8cf 21 char held_red_states;
jah128 0:d6269d17c8cf 22 char held_green_states;
jah128 0:d6269d17c8cf 23 Timeout blink_led_timeout;
jah128 0:d6269d17c8cf 24
jah128 0:d6269d17c8cf 25 unsigned short get_led_states(){
jah128 0:d6269d17c8cf 26 return (green_led_states << 8) + red_led_states;
jah128 0:d6269d17c8cf 27 }
jah128 0:d6269d17c8cf 28
jah128 0:d6269d17c8cf 29 void set_leds(char green, char red){
jah128 0:d6269d17c8cf 30 green_led_states = green;
jah128 0:d6269d17c8cf 31 red_led_states = red;
jah128 0:d6269d17c8cf 32 IF_update_leds();
jah128 0:d6269d17c8cf 33 }
jah128 0:d6269d17c8cf 34
jah128 0:d6269d17c8cf 35 void set_base_led(char state){
jah128 0:d6269d17c8cf 36 IF_set_base_LED(state);
jah128 0:d6269d17c8cf 37 }
jah128 0:d6269d17c8cf 38
jah128 0:d6269d17c8cf 39 void set_green_leds(char green){
jah128 0:d6269d17c8cf 40 green_led_states = green;
jah128 0:d6269d17c8cf 41 IF_update_leds();
jah128 0:d6269d17c8cf 42 }
jah128 0:d6269d17c8cf 43
jah128 0:d6269d17c8cf 44 void set_red_leds(char red){
jah128 0:d6269d17c8cf 45 red_led_states = red;
jah128 0:d6269d17c8cf 46 IF_update_leds();
jah128 0:d6269d17c8cf 47 }
jah128 0:d6269d17c8cf 48
jah128 0:d6269d17c8cf 49 void set_led(char led, char state){
jah128 0:d6269d17c8cf 50 if(state % 2 == 1) red_led_states |= 1 << led;
jah128 0:d6269d17c8cf 51 else red_led_states &= ~(1 << led);
jah128 0:d6269d17c8cf 52 if(state / 2) green_led_states |= 1 << led;
jah128 0:d6269d17c8cf 53 else green_led_states &= ~(1 << led);
jah128 0:d6269d17c8cf 54 IF_update_leds();
jah128 0:d6269d17c8cf 55 }
jah128 0:d6269d17c8cf 56
jah128 0:d6269d17c8cf 57 void blink_leds(float timeout){
jah128 0:d6269d17c8cf 58 save_led_states();
jah128 0:d6269d17c8cf 59 set_leds(0xFF,0xFF);
jah128 0:d6269d17c8cf 60 blink_led_timeout.attach(&restore_led_states, timeout);
jah128 0:d6269d17c8cf 61 }
jah128 0:d6269d17c8cf 62
jah128 0:d6269d17c8cf 63 void set_center_led(char state){
jah128 0:d6269d17c8cf 64 set_center_led(state, center_led_brightness);
jah128 0:d6269d17c8cf 65 }
jah128 0:d6269d17c8cf 66
jah128 0:d6269d17c8cf 67 void set_center_led(char state, float brightness){
jah128 0:d6269d17c8cf 68 center_led_brightness = brightness;
jah128 0:d6269d17c8cf 69 center_led_state = state;
jah128 0:d6269d17c8cf 70 switch(center_led_state){
jah128 0:d6269d17c8cf 71 case 0: center_led_red.write(0);
jah128 0:d6269d17c8cf 72 center_led_green.write(0);
jah128 0:d6269d17c8cf 73 break;
jah128 0:d6269d17c8cf 74 case 1: center_led_red.write(center_led_brightness / 4);
jah128 0:d6269d17c8cf 75 center_led_green.write(0);
jah128 0:d6269d17c8cf 76 break;
jah128 0:d6269d17c8cf 77 case 2: center_led_red.write(0);
jah128 0:d6269d17c8cf 78 center_led_green.write(center_led_brightness);
jah128 0:d6269d17c8cf 79 break;
jah128 0:d6269d17c8cf 80 case 3: center_led_red.write(center_led_brightness / 4);
jah128 0:d6269d17c8cf 81 center_led_green.write(center_led_brightness);
jah128 0:d6269d17c8cf 82 break;
jah128 0:d6269d17c8cf 83 }
jah128 0:d6269d17c8cf 84 }
jah128 0:d6269d17c8cf 85
jah128 0:d6269d17c8cf 86 void set_center_led_brightness(float brightness){
jah128 0:d6269d17c8cf 87 set_center_led(center_led_state,brightness);
jah128 0:d6269d17c8cf 88 }
jah128 0:d6269d17c8cf 89
jah128 0:d6269d17c8cf 90 void save_led_states(){
jah128 0:d6269d17c8cf 91 held_red_states = red_led_states;
jah128 0:d6269d17c8cf 92 held_green_states = green_led_states;
jah128 0:d6269d17c8cf 93 }
jah128 0:d6269d17c8cf 94
jah128 0:d6269d17c8cf 95 void restore_led_states(){
jah128 0:d6269d17c8cf 96 set_leds(held_green_states,held_red_states);
jah128 0:d6269d17c8cf 97 }
jah128 0:d6269d17c8cf 98
jah128 0:d6269d17c8cf 99 void IF_init_leds(){
jah128 0:d6269d17c8cf 100 green_led_states = 0;
jah128 0:d6269d17c8cf 101 red_led_states = 0;
jah128 0:d6269d17c8cf 102 center_led_red.period_us(100);
jah128 0:d6269d17c8cf 103 center_led_green.period_us(100);
jah128 0:d6269d17c8cf 104 set_center_led(0,0.2);
jah128 0:d6269d17c8cf 105 }
jah128 0:d6269d17c8cf 106
jah128 0:d6269d17c8cf 107 void IF_update_leds(){
jah128 0:d6269d17c8cf 108 char led_byte_0 = (((green_led_states & (1 << 3)) == 0) << 7) +
jah128 0:d6269d17c8cf 109 (((green_led_states & (1 << 2)) == 0) << 5) +
jah128 0:d6269d17c8cf 110 (((green_led_states & (1 << 1)) == 0) << 3) +
jah128 0:d6269d17c8cf 111 (((green_led_states & (1)) == 0) << 1) +
jah128 0:d6269d17c8cf 112 (((red_led_states & (1 << 3)) == 0) << 6) +
jah128 0:d6269d17c8cf 113 (((red_led_states & (1 << 2)) == 0) << 4) +
jah128 0:d6269d17c8cf 114 (((red_led_states & (1 << 1)) == 0) << 2) +
jah128 0:d6269d17c8cf 115 (((red_led_states & (1)) == 0));
jah128 0:d6269d17c8cf 116 char led_byte_1 = (((green_led_states & (1 << 7)) == 0) << 7) +
jah128 0:d6269d17c8cf 117 (((green_led_states & (1 << 6)) == 0) << 5) +
jah128 0:d6269d17c8cf 118 (((green_led_states & (1 << 5)) == 0) << 3) +
jah128 0:d6269d17c8cf 119 (((green_led_states & (1 << 4)) == 0) << 1) +
jah128 0:d6269d17c8cf 120 (((red_led_states & (1 << 7)) == 0) << 6) +
jah128 0:d6269d17c8cf 121 (((red_led_states & (1 << 6)) == 0) << 4) +
jah128 0:d6269d17c8cf 122 (((red_led_states & (1 << 5)) == 0) << 2) +
jah128 0:d6269d17c8cf 123 (((red_led_states & (1 << 4)) == 0));
jah128 0:d6269d17c8cf 124 IF_write_to_led_ic(led_byte_0,led_byte_1);
jah128 0:d6269d17c8cf 125 }