Optimaze with new mbed os for study

Dependencies:   TS_DISCO_F746NG BSP_DISCO_F746NG Graphics

RadarDemo/RadarDemo.cpp

Committer:
karpent
Date:
2016-11-05
Revision:
3:732f7144ec81
Parent:
2:8db224cc1fcb
Child:
4:66f13188c26b

File content as of revision 3:732f7144ec81:

//
// RadarDemo.cpp - example of graphic commands usage to create 2D graphics.
//

#include "RadarDemo.h"
#include "RK043FN48H.h"

// Size of range control areas on the screen
const int raSize = 50;

RadarDemo::RadarDemo(Display* display) : Radar(display)
{
}


RadarDemo::~RadarDemo()
{
    Radar::~Radar();
}


void RadarDemo::Initialize()
{
    // Set scan perion for 6 seconds
    timer.SetScanPeriod(6);

    // Run forever, to set limited time for the demo
    // use for example: demoTime = 10 * scanPeriod;
    demoTime = 0;

    // Put a number of sample targets on the display
    AddSampleTargets(30);

    // Use medium range, values 0..2 are available now
    SetRange(1);

    // Remark : Data member initializer is not allowed for ARM compiler,
    //          initialize data in class constructor.
    currentBeamAngle = 0;
    lastBeamAngle = currentBeamAngle;

    // Force background redraw
    UnvalidateBackground();

    // Initialize touchscreen
    ts.Init(((RK043FN48H*)GetDisplay())->DisplayWidth(), ((RK043FN48H*)GetDisplay())->DisplayHeight());

    timer.Start();
}


void RadarDemo::Render()
{
    timer.RegisterScan();

    // Calculate actual beam position
    lastBeamAngle = currentBeamAngle;
    currentBeamAngle = timer.GetBeamAngle();

    // TODO: 
    if(DetectTouch(window))
    {
        // Set timeout for the next touch detection
    }

    RK043FN48H* display = (RK043FN48H*)GetDisplay();
    if(NeedsRedraw()) {

        display->SetActiveLayer(Background);
        display->Clear();

        // Set draw color before calling draw method
        display->SetDrawColor(0x80, 0, 0, 0xFF);
        DrawRangeButtons(raSize);

        display->SetDrawColor(0x40, 0x40, 0x40, 0xFF);
        DrawMarkers();

        display->SetDrawColor(0x80, 0x80, 0x80, 0xFF);
        DrawBorder();

        display->SetActiveLayer(Foreground);
        _needsRedraw = false;
    }

    UpdateTargetsLocation(lastBeamAngle, currentBeamAngle, timer.GetRunningTime());

    // Redraw foreground
    display->Clear();
    DrawTracks();

    DrawRadarBeam(currentBeamAngle);
}


bool RadarDemo::IsRunning()
{
    return demoTime > 0 ? timer.GetRunningTime() <= demoTime : true;
}


void RadarDemo::AddSampleTargets(int count)
{
    const float minSpeed = 200; // [km/h]
    const float maxSpeed = 800; // [km/h]
    Target *target;

    srand(timer.GetRunningTime());

    for (int i = 0; i<count; i++) {
        // Generate target parameters
        float angle = 2 * M_PI * rand() / (float)RAND_MAX;
        float range = GetMaxRange() * rand() / (float)RAND_MAX;
        float speed = minSpeed + (maxSpeed - minSpeed) * rand() / (float)RAND_MAX;
        float direction = 2 * M_PI * rand() / (float)RAND_MAX;

        // Create a new target
        target = new Target(i + 100, speed, direction);
        target->SetLocationAngular(range, angle);

        // Put target on the list
        targetsList.push_back(*target);
    }
}


void RadarDemo::UnvalidateBackground()
{
    _needsRedraw = true;
}


bool RadarDemo::NeedsRedraw()
{
    return _needsRedraw;
}


bool RadarDemo::DetectTouch(Window screen)
{
    TS_StateTypeDef tsState;

    ts.GetState(&tsState);

    if (tsState.touchDetected > 0) {

        if(tsState.touchX[0] > (screen.x2-raSize) && tsState.touchY[0] < screen.y1 + raSize) {
            if(ChangeRange(1))
                UnvalidateBackground();
        }

        else if(tsState.touchX[0] > (screen.x2-raSize) && tsState.touchY[0] > (screen.y2-raSize)) {
            if(ChangeRange(-1))
                UnvalidateBackground();
        }

        else {
            SetCenter(tsState.touchX[0], tsState.touchY[0]);
            UnvalidateBackground();
        }
        
        ts.ResetTouchData(&tsState);
        return true;
    }

    return false;
}