merge from 2.0-ongoing @ 3581
[ardour.git] / libs / surfaces / tranzport / screen.cc
1 /*
2  *   Copyright (C) 2006 Paul Davis 
3  *   Copyright (C) 2007 Michael Taht
4  *
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *   GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program; if not, write to the Free Software
17  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *  
19  *   */
20
21 #include <cstring>
22 #include <tranzport_control_protocol.h>
23 #include <cstring>
24
25 void
26 TranzportControlProtocol::screen_clear ()
27 {
28         const char *blank = "                    ";
29         print(0,0,blank); 
30         print(1,0,blank);
31 }
32
33 void TranzportControlProtocol::screen_invalidate ()
34 {
35         screen_invalid.set();
36         for(int row = 0; row < ROWS; row++) {
37                 for(int col = 0; col < COLUMNS; col++) {
38                         screen_current[row][col] = 0x7f;
39                         screen_pending[row][col] = ' ';
40                         screen_flash[row][col] = ' ';
41                 }
42         }
43 }
44
45 void TranzportControlProtocol::screen_validate ()
46 {
47 }
48
49 void TranzportControlProtocol::screen_init ()
50 {
51         screen_invalidate();
52 }
53
54 // FIXME: Switch to a column oriented flush to make the redraw of the 
55 // meters look better
56
57 int
58 TranzportControlProtocol::screen_flush ()
59 {
60         int cell = 0, row=0, col_base, pending = 0;
61         const unsigned long CELL_BITS = 0x0F;
62         if ( _device_status == STATUS_OFFLINE) { return (-1); }
63
64         std::bitset<ROWS*COLUMNS> mask(CELL_BITS);
65         std::bitset<ROWS*COLUMNS> imask(CELL_BITS);
66         for(cell = 0; cell < 10 && pending == 0; cell++) {
67                 mask = imask << (cell*4);
68                 if((screen_invalid & mask).any()) {
69                         /* something in this cell is different, so dump the cell to the device. */
70 #if DEBUG_TRANZPORT_SCREEN
71                         printf("MASK   : %s\n", mask.to_string().c_str());
72 #endif
73                         if(cell > 4) { row = 1; } else { row = 0; }
74                         col_base = (cell*4)%COLUMNS;
75         
76                         uint8_t cmd[8]; 
77                         cmd[0] = 0x00; 
78                         cmd[1] = 0x01; 
79                         cmd[2] = cell; 
80                         cmd[3] = screen_pending[row][col_base]; 
81                         cmd[4] = screen_pending[row][col_base+1];
82                         cmd[5] = screen_pending[row][col_base+2]; 
83                         cmd[6] = screen_pending[row][col_base+3];
84                         cmd[7] = 0x00;
85
86                         if((pending = lcd_write(cmd)) == 0) {
87                                 /* successful write: copy to current cached display */
88                                 screen_invalid &= mask.flip(); 
89                                 memcpy (&screen_current[row][col_base], &screen_pending[row][col_base], 4);
90                         }
91                 }
92         }
93         return pending;
94 }
95