Library for the PsiSwarm Robot - Version 0.7

Dependents:   PsiSwarm_V7_Blank

Fork of PsiSwarmLibrary by James Hilder

Committer:
jah128
Date:
Sat Oct 15 13:51:39 2016 +0000
Revision:
6:b340a527add9
Parent:
5:3cdd1a37cdd7
Added Apache license to files

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 6:b340a527add9 3 * Copyright 2016 University of York
jah128 6:b340a527add9 4 *
jah128 6:b340a527add9 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
jah128 6:b340a527add9 6 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
jah128 6:b340a527add9 7 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS
jah128 6:b340a527add9 8 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jah128 6:b340a527add9 9 * See the License for the specific language governing permissions and limitations under the License.
jah128 6:b340a527add9 10 *
jah128 0:d6269d17c8cf 11 * File: led.cpp
jah128 0:d6269d17c8cf 12 *
jah128 0:d6269d17c8cf 13 * (C) Dept. Electronics & Computer Science, University of York
jah128 0:d6269d17c8cf 14 * James Hilder, Alan Millard, Alexander Horsfield, Homero Elizondo, Jon Timmis
jah128 0:d6269d17c8cf 15 *
jah128 5:3cdd1a37cdd7 16 * PsiSwarm Library Version: 0.7
jah128 0:d6269d17c8cf 17 *
jah128 5:3cdd1a37cdd7 18 * October 2016
jah128 0:d6269d17c8cf 19 *
jah128 0:d6269d17c8cf 20 *
jah128 0:d6269d17c8cf 21 */
jah128 0:d6269d17c8cf 22
jah128 0:d6269d17c8cf 23 #include "psiswarm.h"
jah128 0:d6269d17c8cf 24
jah128 0:d6269d17c8cf 25 char green_led_states;
jah128 0:d6269d17c8cf 26 char red_led_states;
jah128 0:d6269d17c8cf 27 char center_led_state;
jah128 0:d6269d17c8cf 28
jah128 0:d6269d17c8cf 29 char held_red_states;
jah128 0:d6269d17c8cf 30 char held_green_states;
jah128 0:d6269d17c8cf 31 Timeout blink_led_timeout;
jah128 0:d6269d17c8cf 32
jah128 0:d6269d17c8cf 33 unsigned short get_led_states(){
jah128 0:d6269d17c8cf 34 return (green_led_states << 8) + red_led_states;
jah128 0:d6269d17c8cf 35 }
jah128 0:d6269d17c8cf 36
jah128 0:d6269d17c8cf 37 void set_leds(char green, char red){
jah128 0:d6269d17c8cf 38 green_led_states = green;
jah128 0:d6269d17c8cf 39 red_led_states = red;
jah128 0:d6269d17c8cf 40 IF_update_leds();
jah128 0:d6269d17c8cf 41 }
jah128 0:d6269d17c8cf 42
jah128 0:d6269d17c8cf 43 void set_base_led(char state){
jah128 0:d6269d17c8cf 44 IF_set_base_LED(state);
jah128 0:d6269d17c8cf 45 }
jah128 0:d6269d17c8cf 46
jah128 0:d6269d17c8cf 47 void set_green_leds(char green){
jah128 0:d6269d17c8cf 48 green_led_states = green;
jah128 0:d6269d17c8cf 49 IF_update_leds();
jah128 0:d6269d17c8cf 50 }
jah128 0:d6269d17c8cf 51
jah128 0:d6269d17c8cf 52 void set_red_leds(char red){
jah128 0:d6269d17c8cf 53 red_led_states = red;
jah128 0:d6269d17c8cf 54 IF_update_leds();
jah128 0:d6269d17c8cf 55 }
jah128 0:d6269d17c8cf 56
jah128 0:d6269d17c8cf 57 void set_led(char led, char state){
jah128 0:d6269d17c8cf 58 if(state % 2 == 1) red_led_states |= 1 << led;
jah128 0:d6269d17c8cf 59 else red_led_states &= ~(1 << led);
jah128 0:d6269d17c8cf 60 if(state / 2) green_led_states |= 1 << led;
jah128 0:d6269d17c8cf 61 else green_led_states &= ~(1 << led);
jah128 0:d6269d17c8cf 62 IF_update_leds();
jah128 0:d6269d17c8cf 63 }
jah128 0:d6269d17c8cf 64
jah128 0:d6269d17c8cf 65 void blink_leds(float timeout){
jah128 0:d6269d17c8cf 66 save_led_states();
jah128 0:d6269d17c8cf 67 set_leds(0xFF,0xFF);
jah128 0:d6269d17c8cf 68 blink_led_timeout.attach(&restore_led_states, timeout);
jah128 0:d6269d17c8cf 69 }
jah128 0:d6269d17c8cf 70
jah128 0:d6269d17c8cf 71 void set_center_led(char state){
jah128 0:d6269d17c8cf 72 set_center_led(state, center_led_brightness);
jah128 0:d6269d17c8cf 73 }
jah128 0:d6269d17c8cf 74
jah128 0:d6269d17c8cf 75 void set_center_led(char state, float brightness){
jah128 0:d6269d17c8cf 76 center_led_brightness = brightness;
jah128 0:d6269d17c8cf 77 center_led_state = state;
jah128 0:d6269d17c8cf 78 switch(center_led_state){
jah128 0:d6269d17c8cf 79 case 0: center_led_red.write(0);
jah128 0:d6269d17c8cf 80 center_led_green.write(0);
jah128 0:d6269d17c8cf 81 break;
jah128 0:d6269d17c8cf 82 case 1: center_led_red.write(center_led_brightness / 4);
jah128 0:d6269d17c8cf 83 center_led_green.write(0);
jah128 0:d6269d17c8cf 84 break;
jah128 0:d6269d17c8cf 85 case 2: center_led_red.write(0);
jah128 0:d6269d17c8cf 86 center_led_green.write(center_led_brightness);
jah128 0:d6269d17c8cf 87 break;
jah128 0:d6269d17c8cf 88 case 3: center_led_red.write(center_led_brightness / 4);
jah128 0:d6269d17c8cf 89 center_led_green.write(center_led_brightness);
jah128 0:d6269d17c8cf 90 break;
jah128 0:d6269d17c8cf 91 }
jah128 0:d6269d17c8cf 92 }
jah128 0:d6269d17c8cf 93
jah128 0:d6269d17c8cf 94 void set_center_led_brightness(float brightness){
jah128 0:d6269d17c8cf 95 set_center_led(center_led_state,brightness);
jah128 0:d6269d17c8cf 96 }
jah128 0:d6269d17c8cf 97
jah128 0:d6269d17c8cf 98 void save_led_states(){
jah128 0:d6269d17c8cf 99 held_red_states = red_led_states;
jah128 0:d6269d17c8cf 100 held_green_states = green_led_states;
jah128 0:d6269d17c8cf 101 }
jah128 0:d6269d17c8cf 102
jah128 0:d6269d17c8cf 103 void restore_led_states(){
jah128 0:d6269d17c8cf 104 set_leds(held_green_states,held_red_states);
jah128 0:d6269d17c8cf 105 }
jah128 0:d6269d17c8cf 106
jah128 0:d6269d17c8cf 107 void IF_init_leds(){
jah128 0:d6269d17c8cf 108 green_led_states = 0;
jah128 0:d6269d17c8cf 109 red_led_states = 0;
jah128 0:d6269d17c8cf 110 center_led_red.period_us(100);
jah128 0:d6269d17c8cf 111 center_led_green.period_us(100);
jah128 0:d6269d17c8cf 112 set_center_led(0,0.2);
jah128 0:d6269d17c8cf 113 }
jah128 0:d6269d17c8cf 114
jah128 0:d6269d17c8cf 115 void IF_update_leds(){
jah128 0:d6269d17c8cf 116 char led_byte_0 = (((green_led_states & (1 << 3)) == 0) << 7) +
jah128 0:d6269d17c8cf 117 (((green_led_states & (1 << 2)) == 0) << 5) +
jah128 0:d6269d17c8cf 118 (((green_led_states & (1 << 1)) == 0) << 3) +
jah128 0:d6269d17c8cf 119 (((green_led_states & (1)) == 0) << 1) +
jah128 0:d6269d17c8cf 120 (((red_led_states & (1 << 3)) == 0) << 6) +
jah128 0:d6269d17c8cf 121 (((red_led_states & (1 << 2)) == 0) << 4) +
jah128 0:d6269d17c8cf 122 (((red_led_states & (1 << 1)) == 0) << 2) +
jah128 0:d6269d17c8cf 123 (((red_led_states & (1)) == 0));
jah128 0:d6269d17c8cf 124 char led_byte_1 = (((green_led_states & (1 << 7)) == 0) << 7) +
jah128 0:d6269d17c8cf 125 (((green_led_states & (1 << 6)) == 0) << 5) +
jah128 0:d6269d17c8cf 126 (((green_led_states & (1 << 5)) == 0) << 3) +
jah128 0:d6269d17c8cf 127 (((green_led_states & (1 << 4)) == 0) << 1) +
jah128 0:d6269d17c8cf 128 (((red_led_states & (1 << 7)) == 0) << 6) +
jah128 0:d6269d17c8cf 129 (((red_led_states & (1 << 6)) == 0) << 4) +
jah128 0:d6269d17c8cf 130 (((red_led_states & (1 << 5)) == 0) << 2) +
jah128 0:d6269d17c8cf 131 (((red_led_states & (1 << 4)) == 0));
jah128 0:d6269d17c8cf 132 IF_write_to_led_ic(led_byte_0,led_byte_1);
jah128 0:d6269d17c8cf 133 }