RoboCup Base Station

Dependencies:   mbed mbed-rtos Wireless Drivers

main.cpp

Committer:
jjones646
Date:
2014-12-31
Revision:
2:7fd95eae5731
Parent:
1:e5373c63f642
Child:
3:c3114df544e8

File content as of revision 2:7fd95eae5731:

// RoboCup dual-frequency band base station

#include "BaseStation.h"

// Function for writing a number to the 7-segment display
void writeSegment(uint8_t val, DigitalOut& latch)
{
    // Outputs used as input values to the 7-segment binary decoder - uses latching inputs
    DigitalOut signal[4] = { RJ_7_SEG_PINS };

    // write out the new value
    for (int i=0; i<4; i++)
        signal[i] = ((1<<i) & (val)) & 0x0F;

    // latch the value
    for (int i=0; i<2; i++)
        latch = !latch;
}

void seg_task(void const *arg)
{
    // latch pin for 7-seg
    DigitalOut latch( RJ_7_SEG_LATCH_PIN, 0 );
    
    // Decimal point initialized to OFF
    DigitalOut decimal( RJ_7_SEG_DOT_PIN, 1 );

    uint8_t channel = 8;
    writeSegment(channel, latch);
    channel = 0;    // start from 0 once the main task's look begins

    // wait to be signaled before beginning
    osSignalWait(0x01, osWaitForever);
    
    // give a small delay to ensure the startup value stays lit for some time
    Thread::wait(500);
    
    // turn the decimal point off
    decimal = 0;

    while(1) {  // loop forever
        // send numerical value to 7-segment & hold for a while
        writeSegment(channel++, latch);
        channel = (channel > 9) ? 0 : channel; // reset value if too high
        Thread::wait(1000);
    }
}

int main()
{
    // RGB Status LED
    PwmOut rgb_led[3] = { RJ_RGB_LED_PINS };

    // Primary radio status LEDs
    DigitalOut r1_led[3] = { RJ_PRIMARY_RADIO_LEDS };

    // Secondary radio status LEDs
    DigitalOut r2_led[3] = { RJ_SECONDARY_RADIO_LEDS };

    // Used for controlling power to the RGB LED's shared annode lead
    DigitalOut rgb_pwr( RJ_RGB_LED_ANNODE, 0 );

    // Start 7-segment task
    Thread thread_seg_task(seg_task);

    // turn all LEDs off initially - values are inverted since LEDs are sinking the current
    for (int i=0; i<3; i++) {
        rgb_led[i] = 1;
        r1_led[i] = 1;
        r2_led[i] = 1;
    }

    // =========== Cyle primary & secondary radio status LEDs ===========
    // turn on all radio status LEDs
    for (int i=0; i<3; i++) {
        r1_led[i] = 0;
        r2_led[i] = 0;
        Thread::wait(50);
    }
    // turn off all radio status LEDs
    for (int i=0; i<3; i++) {
        r1_led[i] = 1;
        r2_led[i] = 1;
        Thread::wait(50);
    }

    // give power to all colors of the RGB LED and turn off decimal point
    rgb_pwr = 1;
    
    // tell the segment thread to begin its task
    thread_seg_task.signal_set(0x01);

    // fade the RGB LED up to green and half power
    for (float i=1.0; i>0.5; i-=0.01) {
        rgb_led[G] = i;
        Thread::wait(20);
    }

    // at led_intensity[3] = { 0, 0, 0 };
    srand(time(NULL));

    // loop forever ====================
    while(1) {
        uint8_t color = rand()%2;

        // rgb_led[color] = (rand()%1000)/1000;

        // delay
        Thread::wait(300);
    }
}