libraries / Mitov / Mitov_TVOut_RCA.hon commit Added link to project report (97a3ba0)
   1////////////////////////////////////////////////////////////////////////////////
   2//                                                                            //
   3//     This software is supplied under the terms of a license agreement or    //
   4//     nondisclosure agreement with Mitov Software and may not be copied      //
   5//     or disclosed except in accordance with the terms of that agreement.    //
   6//         Copyright(c) 2002-2016 Mitov Software. All Rights Reserved.        //
   7//                                                                            //
   8////////////////////////////////////////////////////////////////////////////////
   9
  10#ifndef _MITOV_TVOUT_RCA_h
  11#define _MITOV_TVOUT_RCA_h
  12
  13#include <Mitov.h>
  14#include <TVout.h>
  15
  16namespace Mitov
  17{
  18//---------------------------------------------------------------------------
  19        class TVOut_RCA_Intf;
  20//---------------------------------------------------------------------------
  21        class TVOutRCAElementBasic : public OpenWire::Component
  22        {
  23                typedef OpenWire::Component inherited;
  24
  25        public:
  26                virtual void Render() = 0;
  27                virtual void TVBegin() = 0;
  28
  29        public: // Public for the print access
  30                Mitov::TVOut_RCA_Intf   &FOwner;
  31
  32        public:
  33                TVOutRCAElementBasic( Mitov::TVOut_RCA_Intf &AOwner );
  34
  35        };
  36//---------------------------------------------------------------------------
  37        class TVOut_RCA_Intf
  38        {
  39        public:
  40                virtual void RegisterRender( TVOutRCAElementBasic *AItem ) {}
  41                virtual bool IsEnabled() = 0;
  42                virtual TVout &GetTV() = 0;
  43
  44                virtual void TV_shift( bool _InVertical, int32_t _Distance ) = 0;
  45                virtual void TV_draw_line(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, char c) = 0;
  46                virtual void TV_bitmap(uint8_t x, uint8_t y, const unsigned char * bmp, uint16_t i = 0, uint8_t width = 0, uint8_t lines = 0) = 0;
  47                virtual void TV_set_cursor(uint8_t, uint8_t) = 0;
  48                virtual void TV_draw_circle(uint8_t x0, uint8_t y0, uint8_t radius, char c, char fc = -1) = 0;
  49                virtual void TV_draw_rect(uint8_t x0, uint8_t y0, uint8_t w, uint8_t h, char c, char fc = -1) = 0;
  50                virtual void TV_set_pixel(uint8_t x, uint8_t y, char c) = 0;
  51                virtual unsigned char TV_get_pixel(uint8_t x, uint8_t y) = 0;
  52
  53        };
  54//---------------------------------------------------------------------------
  55        enum TArduinoTVOutRCAElementColor { tvcBlack, tvcWhite, tvcInvert, tvcNone = -1 };
  56//---------------------------------------------------------------------------
  57        class TVOutRCAElementClocked : public Mitov::TVOutRCAElementBasic, public Mitov::ClockingSupport
  58        {
  59                typedef Mitov::TVOutRCAElementBasic inherited;
  60
  61        public:
  62                virtual void DoClockReceive( void *_Data ) override
  63                {
  64                        Render();
  65                }
  66
  67        public:
  68                virtual void TVBegin() override
  69                {
  70                        if( ! ClockInputPin.IsConnected() )
  71                                Render();
  72                }
  73
  74        public:
  75                using inherited::inherited;
  76
  77        };
  78//---------------------------------------------------------------------------
  79        class TVOutRCAElementColored : public TVOutRCAElementClocked
  80        {
  81                typedef Mitov::TVOutRCAElementClocked inherited;
  82
  83        public:
  84                TArduinoTVOutRCAElementColor    Color = tvcWhite;
  85
  86        public:
  87                using inherited::inherited;
  88
  89        };
  90//---------------------------------------------------------------------------
  91        class TVOutRCAElementFillColored : public TVOutRCAElementClocked
  92        {
  93                typedef Mitov::TVOutRCAElementClocked inherited;
  94
  95        public:
  96                TArduinoTVOutRCAElementColor    BorderColor = tvcWhite;
  97                TArduinoTVOutRCAElementColor    FillColor = tvcBlack;
  98
  99        public:
 100                using inherited::inherited;
 101
 102        };
 103//---------------------------------------------------------------------------
 104        class TVOut_RCA : public OpenWire::Component, public TVOut_RCA_Intf
 105        {
 106                typedef OpenWire::Component inherited;
 107
 108    public:
 109        bool    Enabled : 1;
 110                bool    IsPAL : 1;
 111                int32_t Width = 128;
 112                int32_t Height = 96;
 113
 114        public:
 115                void SetEnabled( bool AVelue )
 116                {
 117                        if( Enabled == AVelue )
 118                                return;
 119
 120                        Enabled = AVelue;
 121
 122                        if( Enabled )
 123                                TVBegin();
 124
 125                        else
 126                                FTV.end();
 127                }
 128
 129        public:
 130                TVout   FTV;
 131
 132        protected:
 133                Mitov::SimpleList<TVOutRCAElementBasic *>       FElements;
 134
 135        public:
 136                virtual void RegisterRender( TVOutRCAElementBasic *AItem ) override
 137                {
 138                        FElements.push_back( AItem );
 139                }
 140
 141                virtual void TV_shift( bool _InVertical, int32_t _Distance ) override
 142                {
 143                        if( ! Enabled )
 144                                return;
 145
 146                        uint8_t ADistance = abs( _Distance );
 147                        uint8_t ADirection;
 148                        if( _InVertical )
 149                        {
 150                                if( _Distance < 0 )
 151                                        ADirection = UP;
 152
 153                                else
 154                                        ADirection = DOWN;
 155
 156                        }
 157                        else
 158                        {
 159                                if( _Distance < 0 )
 160                                        ADirection = LEFT;
 161
 162                                else
 163                                        ADirection = RIGHT;
 164
 165                        }
 166
 167                        FTV.shift( ADistance, ADirection );
 168                }
 169
 170                virtual void TV_draw_line(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, char c) override
 171                {
 172                        if( Enabled )
 173                                FTV.draw_line( x0, y0, x1, y1, c );
 174                }
 175
 176                virtual void TV_draw_circle(uint8_t x0, uint8_t y0, uint8_t radius, char c, char fc = -1) override
 177                {
 178                        if( Enabled )
 179                                FTV.draw_circle( x0, y0, radius, c, fc );
 180                }
 181
 182                virtual void TV_draw_rect(uint8_t x0, uint8_t y0, uint8_t w, uint8_t h, char c, char fc = -1) override
 183                {
 184                        if( Enabled )
 185                                FTV.draw_rect( x0, y0, w, h, c, fc );
 186                }
 187
 188                virtual void TV_set_pixel(uint8_t x, uint8_t y, char c) override
 189                {
 190                        if( Enabled )
 191                                FTV.set_pixel( x, y, c );
 192                }
 193
 194                virtual unsigned char TV_get_pixel(uint8_t x, uint8_t y) override
 195                {
 196                        return FTV.get_pixel( x, y );
 197                }
 198
 199                virtual void TV_bitmap(uint8_t x, uint8_t y, const unsigned char * bmp, uint16_t i = 0, uint8_t width = 0, uint8_t lines = 0) override
 200                {
 201                        if( Enabled )
 202                                FTV.bitmap( x, y, bmp, i, width, lines );
 203                }
 204
 205                virtual void TV_set_cursor( uint8_t x, uint8_t y ) override
 206                {
 207                        if( Enabled )
 208                                FTV.set_cursor( x, y );
 209                }
 210
 211        public:
 212                virtual bool IsEnabled() override
 213                {
 214                        return Enabled;
 215                }
 216
 217                virtual TVout &GetTV() override
 218                {
 219                        return FTV;
 220                }
 221
 222        protected:
 223                void TVBegin()
 224                {
 225                        FTV.begin( IsPAL ? PAL : NTSC, Width, Height );
 226
 227                        for( int i = 0; i < FElements.size(); i ++ )
 228                                FElements[ i ]->TVBegin();
 229
 230//                      FTV.select_font( font4x6 );
 231                }
 232
 233        protected:
 234                virtual void SystemInit() override
 235                {
 236                        if( Enabled )
 237                                FTV.begin( IsPAL ? PAL : NTSC, Width, Height );
 238
 239                        inherited::SystemInit();
 240                }
 241
 242                virtual void SystemStart() override
 243                {
 244                        if( Enabled )
 245                                for( int i = 0; i < FElements.size(); i ++ )
 246                                        FElements[ i ]->TVBegin();
 247
 248                        inherited::SystemStart();
 249                }
 250
 251        public:
 252                TVOut_RCA() :
 253                        IsPAL( false ),
 254                        Enabled( true )
 255                {
 256                }
 257
 258        };
 259//---------------------------------------------------------------------------
 260        class TVOutRCAElementFillScreen : public TVOutRCAElementColored
 261        {
 262                typedef Mitov::TVOutRCAElementColored inherited;
 263
 264        public:
 265                TArduinoTVOutRCAElementColor    Color = tvcBlack;
 266
 267        public:
 268                virtual void Render() override
 269                {
 270                        if( FOwner.IsEnabled() )
 271                                FOwner.GetTV().fill( (int)Color );
 272                }
 273
 274        public:
 275                using inherited::inherited;
 276
 277        };
 278//---------------------------------------------------------------------------
 279        class TVOutRCAElementPlayTone : public TVOutRCAElementClocked
 280        {
 281                typedef Mitov::TVOutRCAElementClocked inherited;
 282
 283        public:
 284                unsigned int    Frequency = 1760;
 285                unsigned long   Duration = 1000;
 286
 287        public:
 288                virtual void Render() override
 289                {
 290                        if( FOwner.IsEnabled() )
 291                        {
 292                                if( Frequency == 0 )
 293                                        FOwner.GetTV().noTone();
 294
 295                                else
 296                                        FOwner.GetTV().tone( Frequency, Duration );
 297                        }
 298                }
 299
 300        public:
 301                using inherited::inherited;
 302
 303        };
 304//---------------------------------------------------------------------------
 305        class TVOutRCAElementDrawCircle : public TVOutRCAElementFillColored
 306        {
 307                typedef Mitov::TVOutRCAElementFillColored inherited;
 308
 309        public:
 310                int32_t X = 10;
 311                int32_t Y = 10;
 312                int32_t Radius = 10;
 313
 314        public:
 315                virtual void Render() override
 316                {
 317                        FOwner.TV_draw_circle( X, Y, Radius, (int)BorderColor, (int)FillColor );
 318                }
 319
 320        public:
 321                using inherited::inherited;
 322
 323        };
 324//---------------------------------------------------------------------------
 325        class TVOutRCAElementDrawRectangle : public TVOutRCAElementFillColored
 326        {
 327                typedef Mitov::TVOutRCAElementFillColored inherited;
 328
 329        public:
 330                int32_t X = 0;
 331                int32_t Y = 0;
 332                int32_t Width = 10;
 333                int32_t Height = 10;
 334
 335        public:
 336                virtual void Render() override
 337                {
 338                        FOwner.TV_draw_rect( X, Y, Width, Height, (int)BorderColor, (int)FillColor );
 339                }
 340
 341        public:
 342                using inherited::inherited;
 343
 344        };
 345//---------------------------------------------------------------------------
 346        class TVOutRCAElementDrawLine : public TVOutRCAElementColored
 347        {
 348                typedef Mitov::TVOutRCAElementColored inherited;
 349
 350        public:
 351                int32_t X1 = 0;
 352                int32_t Y1 = 0;
 353                int32_t X2 = 10;
 354                int32_t Y2 = 10;
 355
 356        public:
 357                virtual void Render() override
 358                {
 359                        FOwner.TV_draw_line( X1, Y1, X2, Y2, (int)Color );
 360                }
 361
 362        public:
 363                using inherited::inherited;
 364
 365        };
 366//---------------------------------------------------------------------------
 367        class TVOutRCAElementDrawPixel : public TVOutRCAElementColored
 368        {
 369                typedef Mitov::TVOutRCAElementColored inherited;
 370
 371        public:
 372                int32_t X = 0;
 373                int32_t Y = 0;
 374
 375        public:
 376                virtual void Render() override
 377                {
 378                        FOwner.TV_set_pixel( X, Y, (int)Color );
 379                }
 380
 381        public:
 382                using inherited::inherited;
 383
 384        };
 385//---------------------------------------------------------------------------
 386        class TVOutRCAElementShiftScreen : public Mitov::TVOutRCAElementClocked
 387        {
 388                typedef Mitov::TVOutRCAElementClocked inherited;
 389
 390        public:
 391                int32_t Distance = 1;
 392                bool    InVertical = false;
 393
 394        public:
 395                virtual void Render() override
 396                {
 397                        FOwner.TV_shift( InVertical, Distance );
 398                }
 399
 400        public:
 401                using inherited::inherited;
 402
 403        };
 404//---------------------------------------------------------------------------
 405        class TVOutRCAElementBasicPosition : public Mitov::TVOutRCAElementClocked
 406        {
 407                typedef Mitov::TVOutRCAElementClocked inherited;
 408
 409    public:
 410                OpenWire::SourcePin     OutputPin;
 411
 412        public:
 413                int32_t X = 0;
 414                int32_t Y = 0;
 415
 416        public:
 417                using inherited::inherited;
 418
 419        };
 420//---------------------------------------------------------------------------
 421        class TVOutRCAElementDrawScene : public Mitov::TVOutRCAElementBasicPosition, public TVOut_RCA_Intf
 422        {
 423                typedef Mitov::TVOutRCAElementBasicPosition inherited;
 424
 425        protected:
 426                Mitov::SimpleList<TVOutRCAElementBasic *>       FElements;
 427
 428        public:
 429                virtual void RegisterRender( TVOutRCAElementBasic *AItem )
 430                {
 431                        FElements.push_back( AItem );
 432                }
 433
 434                virtual bool IsEnabled()
 435                {
 436                        return FOwner.IsEnabled();
 437                }
 438
 439                virtual TVout &GetTV()
 440                {
 441                        return FOwner.GetTV();
 442                }
 443
 444                virtual void TV_shift( bool _InVertical, int32_t _Distance )
 445                {
 446                        FOwner.TV_shift( _InVertical, _Distance );
 447                }
 448
 449                virtual void TV_draw_line(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, char c)
 450                {
 451                        FOwner.TV_draw_line( X + x0, Y + y0, X + x1, Y + y1, c );
 452                }
 453
 454                virtual void TV_bitmap(uint8_t x, uint8_t y, const unsigned char * bmp, uint16_t i = 0, uint8_t width = 0, uint8_t lines = 0)
 455                {
 456                        FOwner.TV_bitmap( X + x , Y + y, bmp, i, width, lines );
 457                }
 458
 459                virtual void TV_set_cursor( uint8_t x, uint8_t y )
 460                {
 461                        FOwner.TV_set_cursor( X + x , Y + y );
 462                }
 463
 464                virtual void TV_draw_circle(uint8_t x0, uint8_t y0, uint8_t radius, char c, char fc = -1)
 465                {
 466                        FOwner.TV_draw_circle( X + x0, Y + y0, radius, c, fc );
 467                }
 468
 469                virtual void TV_draw_rect(uint8_t x0, uint8_t y0, uint8_t w, uint8_t h, char c, char fc = -1)
 470                {
 471                        FOwner.TV_draw_rect( X + x0, Y + y0, w, h, c, fc );
 472                }
 473
 474                virtual void TV_set_pixel(uint8_t x, uint8_t y, char c)
 475                {
 476                        FOwner.TV_set_pixel( X + x , Y + y, c );
 477                }
 478
 479                virtual unsigned char TV_get_pixel(uint8_t x, uint8_t y)
 480                {
 481                        return FOwner.TV_get_pixel( X + x , Y + y );
 482                }
 483
 484        public:
 485                virtual void Render() override
 486                {
 487                        for( int i = 0; i < FElements.size(); i ++ )
 488                                FElements[ i ]->Render();
 489                }
 490
 491        public:
 492                using inherited::inherited;
 493
 494        };
 495//---------------------------------------------------------------------------
 496        class TVOutRCAElementCheckPixel : public Mitov::TVOutRCAElementBasicPosition
 497        {
 498                typedef Mitov::TVOutRCAElementBasicPosition inherited;
 499
 500    public:
 501                OpenWire::SourcePin     OutputPin;
 502
 503        public:
 504                virtual void Render() override
 505                {
 506                        if( FOwner.IsEnabled() )
 507                                OutputPin.SendValue( FOwner.TV_get_pixel( X, Y ) > 0 );
 508                }
 509
 510        public:
 511                using inherited::inherited;
 512
 513        };
 514//---------------------------------------------------------------------------
 515        class TVOutRCAElementDrawBitmap : public Mitov::TVOutRCAElementBasicPosition
 516        {
 517                typedef Mitov::TVOutRCAElementBasicPosition inherited;
 518
 519    public:
 520                uint8_t Width;
 521                uint8_t Height;
 522
 523                const unsigned char *_Bytes;
 524
 525        public:
 526                virtual void Render() override
 527                {
 528                        FOwner.TV_bitmap( X, Y, _Bytes, 0, Width, Height );
 529                }
 530
 531        public:
 532                using inherited::inherited;
 533
 534        };
 535//---------------------------------------------------------------------------
 536        class TVOutRCAElementSetCursor : public Mitov::TVOutRCAElementBasicPosition
 537        {
 538                typedef Mitov::TVOutRCAElementBasicPosition inherited;
 539
 540        public:
 541                virtual void Render() override
 542                {
 543                        FOwner.TV_set_cursor( X, Y );
 544                }
 545
 546        public:
 547                using inherited::inherited;
 548
 549        };
 550//---------------------------------------------------------------------------
 551        class TVOutRCAElementPrintText : public Mitov::TVOutRCAElementBasic
 552        {
 553                typedef Mitov::TVOutRCAElementBasic inherited;
 554
 555        public:
 556                bool NewLine = true;
 557
 558        protected:
 559                const unsigned char *FFont;
 560
 561        public:
 562                template<typename T> void Print( T AValue )
 563                {
 564                        if( FOwner.IsEnabled() )
 565                        {
 566                                FOwner.GetTV().select_font( FFont );
 567                                if( NewLine )
 568                                        FOwner.GetTV().println( AValue );
 569
 570                                else
 571                                        FOwner.GetTV().print( AValue );
 572                        }
 573                }
 574
 575        public:
 576                TVOutRCAElementPrintText( Mitov::TVOut_RCA_Intf &AOwner, const unsigned char * AFont ) :
 577                        inherited( AOwner ),
 578                        FFont( AFont )
 579                {
 580                }
 581
 582        };
 583//---------------------------------------------------------------------------
 584        class TVOutRCAElementTextAt : public Mitov::TVOutRCAElementClocked
 585        {
 586                typedef Mitov::TVOutRCAElementClocked inherited;
 587
 588        public:
 589                int32_t X = 0;
 590                int32_t Y = 0;
 591                String  InitialValue;
 592
 593        protected:
 594                const unsigned char *FFont;
 595                String  FValue;
 596
 597        public:
 598                template<typename T> void Print( T AValue )
 599                {
 600                         FValue = String( AValue );
 601                }
 602
 603        public:
 604                virtual void Render() override
 605                {
 606                        if( FOwner.IsEnabled() )
 607                        {
 608                                FOwner.GetTV().select_font( FFont );
 609                                FOwner.TV_set_cursor( X, Y );
 610                                FOwner.GetTV().print( FValue.c_str() );
 611                        }
 612                }
 613
 614        protected:
 615                virtual void SystemInit()
 616                {
 617                        FValue = InitialValue;
 618                        inherited::SystemInit();
 619                }
 620
 621        public:
 622                TVOutRCAElementTextAt( Mitov::TVOut_RCA_Intf &AOwner, const unsigned char * AFont ) :
 623                        inherited( AOwner ),
 624                        FFont( AFont )
 625                {
 626                }
 627
 628        };
 629//---------------------------------------------------------------------------
 630        TVOutRCAElementBasic::TVOutRCAElementBasic( Mitov::TVOut_RCA_Intf &AOwner ) :
 631                FOwner( AOwner )
 632        {
 633                FOwner.RegisterRender( this );
 634        }
 635//---------------------------------------------------------------------------
 636}
 637
 638#endif