finalcut/examples/7segment.cpp

238 lines
6.9 KiB
C++
Raw Normal View History

2019-08-06 23:45:28 +02:00
/***********************************************************************
* 7segment.cpp - Seven-segment display *
* *
* This file is part of the FINAL CUT widget toolkit *
2019-08-06 23:45:28 +02:00
* *
* Copyright 2012-2020 Markus Gans *
2019-08-06 23:45:28 +02:00
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of *
2019-08-06 23:45:28 +02:00
* the License, or (at your option) any later version. *
* *
* FINAL CUT is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
2019-08-06 23:45:28 +02:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
#include <array>
2019-08-06 23:45:28 +02:00
#include <map>
#include <vector>
2019-02-24 20:21:12 +01:00
#include <final/final.h>
namespace fc = finalcut::fc;
2019-02-24 20:21:12 +01:00
using finalcut::FColorPair;
2019-08-11 18:15:57 +02:00
using finalcut::FRect;
2019-02-24 20:21:12 +01:00
using finalcut::FPoint;
using finalcut::FSize;
//----------------------------------------------------------------------
// class SegmentView
2019-02-24 20:21:12 +01:00
//----------------------------------------------------------------------
2020-04-14 23:46:42 +02:00
class SegmentView final : public finalcut::FDialog
2019-02-24 20:21:12 +01:00
{
public:
// Constructor
2019-02-24 20:21:12 +01:00
explicit SegmentView (finalcut::FWidget* = nullptr);
private:
2020-11-24 21:06:39 +01:00
struct sevenSegment
2019-02-24 20:21:12 +01:00
{
unsigned char a : 1;
unsigned char b : 1;
unsigned char c : 1;
unsigned char d : 1;
unsigned char e : 1;
unsigned char f : 1;
unsigned char g : 1;
unsigned char : 1; // padding bit
2020-11-24 21:06:39 +01:00
};
2019-02-24 20:21:12 +01:00
// Methods
void hexEncoding();
void get7Segment (const wchar_t);
2019-08-06 23:45:28 +02:00
void draw() override;
2019-02-24 20:21:12 +01:00
// Data members
std::map<wchar_t, sevenSegment> code{};
std::array<finalcut::FString, 3> line{};
finalcut::FLineEdit input{"0123", this};
finalcut::FButton exit{"E&xit", this};
2019-02-24 20:21:12 +01:00
};
//----------------------------------------------------------------------
SegmentView::SegmentView (finalcut::FWidget* parent)
: FDialog{parent}
2019-02-24 20:21:12 +01:00
{
2020-04-19 20:38:52 +02:00
// Dialog settings
// Avoids calling a virtual function from the constructor
// (CERT, OOP50-CPP)
FDialog::setText ("Seven-segment display");
FDialog::setGeometry (FPoint{25, 5}, FSize{42, 15});
2020-04-19 20:38:52 +02:00
2019-02-24 20:21:12 +01:00
// Set encoding
hexEncoding();
// Input field
input.setGeometry (FPoint(2, 2), FSize{12, 1});
input.setLabelText (L"&Hex value");
input.setLabelText (L"&Hex-digits or (.) (:) (H) (L) (P) (U)");
input.setLabelOrientation(finalcut::FLineEdit::label_above);
input.setMaxLength(9);
input.setInputFilter("[:.hHlLpPuU[:xdigit:]]");
2019-02-24 20:21:12 +01:00
// Exit button
exit.setGeometry(FPoint{28, 11}, FSize{10, 1});
2019-02-24 20:21:12 +01:00
// Add some function callbacks
input.addCallback
2019-02-24 20:21:12 +01:00
(
"changed",
2020-08-11 23:04:46 +02:00
[] (SegmentView& dialog)
2019-02-24 20:21:12 +01:00
{
2020-08-11 23:04:46 +02:00
dialog.redraw();
2019-02-24 20:21:12 +01:00
},
2020-08-11 23:04:46 +02:00
std::ref(*this)
2019-02-24 20:21:12 +01:00
);
exit.addCallback
2019-02-24 20:21:12 +01:00
(
"clicked",
2020-08-11 23:04:46 +02:00
finalcut::getFApplication(),
&finalcut::FApplication::cb_exitApp,
this
2019-02-24 20:21:12 +01:00
);
}
//----------------------------------------------------------------------
void SegmentView::hexEncoding()
{
code['0'] = sevenSegment{1, 1, 1, 1, 1, 1, 0};
code['1'] = sevenSegment{0, 1, 1, 0, 0, 0, 0};
code['2'] = sevenSegment{1, 1, 0, 1, 1, 0, 1};
code['3'] = sevenSegment{1, 1, 1, 1, 0, 0, 1};
code['4'] = sevenSegment{0, 1, 1, 0, 0, 1, 1};
code['5'] = sevenSegment{1, 0, 1, 1, 0, 1, 1};
code['6'] = sevenSegment{1, 0, 1, 1, 1, 1, 1};
code['7'] = sevenSegment{1, 1, 1, 0, 0, 0, 0};
code['8'] = sevenSegment{1, 1, 1, 1, 1, 1, 1};
code['9'] = sevenSegment{1, 1, 1, 1, 0, 1, 1};
code['A'] = sevenSegment{1, 1, 1, 0, 1, 1, 1};
code['B'] = sevenSegment{0, 0, 1, 1, 1, 1, 1};
code['C'] = sevenSegment{1, 0, 0, 1, 1, 1, 0};
code['D'] = sevenSegment{0, 1, 1, 1, 1, 0, 1};
code['E'] = sevenSegment{1, 0, 0, 1, 1, 1, 1};
code['F'] = sevenSegment{1, 0, 0, 0, 1, 1, 1};
}
//----------------------------------------------------------------------
void SegmentView::get7Segment (const wchar_t c)
{
for (std::size_t i{0}; i < 3; i++)
2019-02-24 20:21:12 +01:00
line[i].clear();
switch ( c )
{
case ':':
line[0] = ' ';
line[1] = '.';
line[2] = '.';
break;
case '.':
line[0] = ' ';
line[1] = ' ';
line[2] = '.';
break;
case 'H':
line[0] = " ";
line[1] = "|_|";
line[2] = "| |";
break;
case 'L':
line[0] = " ";
line[1] = "| ";
line[2] = "|_ ";
break;
case 'P':
line[0] = " _ ";
line[1] = "|_|";
line[2] = "| ";
break;
case 'U':
line[0] = " ";
line[1] = "| |";
line[2] = "|_|";
break;
default:
// Hexadecimal digit from 0 up to f
if ( code.find(c) != code.end() )
{
2020-04-13 12:40:11 +02:00
const sevenSegment& s = code[c];
constexpr std::array<char, 2> h{{' ', '_'}};
constexpr std::array<char, 2> v{{' ', '|'}};
line[0] << ' ' << h[s.a] << ' ';
line[1] << v[s.f] << h[s.g] << v[s.b];
line[2] << v[s.e] << h[s.d] << v[s.c];
}
2019-02-24 20:21:12 +01:00
}
}
//----------------------------------------------------------------------
void SegmentView::draw()
{
std::vector<finalcut::FTermBuffer> tbuffer{3};
finalcut::FTermBuffer left_space{};
FDialog::draw();
setColor(fc::LightGray, fc::Black);
finalcut::drawBorder(this, FRect(FPoint{3, 6}, FPoint{40, 11}));
2019-02-24 20:21:12 +01:00
for (auto&& ch : input.getText().toUpper())
2019-02-24 20:21:12 +01:00
{
const FColorPair color{fc::LightRed, fc::Black};
2019-02-24 20:21:12 +01:00
get7Segment(ch);
2019-08-25 22:16:00 +02:00
for (std::size_t i{0}; i < 3; i++)
2019-02-24 20:21:12 +01:00
tbuffer[i] << color << line[i] << " ";
}
const std::size_t length = tbuffer[0].getLength();
2019-02-24 20:21:12 +01:00
if ( length < 36 )
left_space << finalcut::FString(36 - length, ' ');
print() << FPoint {4, 7} << left_space << tbuffer[0]
<< FPoint {4, 8} << left_space << tbuffer[1]
<< FPoint {4, 9} << left_space << tbuffer[2]
<< FPoint {4, 10} << finalcut::FString{36, ' '};
2019-02-24 20:21:12 +01:00
}
//----------------------------------------------------------------------
// main part
//----------------------------------------------------------------------
int main (int argc, char* argv[])
{
finalcut::FApplication app(argc, argv);
SegmentView dialog(&app);
2020-04-13 12:40:11 +02:00
finalcut::FWidget::setMainWidget(&dialog);
2019-02-24 20:21:12 +01:00
dialog.show();
return app.exec();
}