11/**
22 * microbit makecode Package: Scroll Text.
3+ * Author: shao ziyang, 2018.Sept
34 * From microbit/micropython Chinese community.
45 * http://www.micropython.org.cn
56 */
67
78enum SCROLL_DIR {
9+ //% block="LEFT"
810 LEFT ,
11+ //% block="UP"
912 UP ,
13+ //% block="RIGHT"
1014 RIGHT ,
15+ //% block="DOWN"
1116 DOWN
1217}
1318
19+ enum SCROLL_ROTATE {
20+ //% block="0"
21+ SR_0 ,
22+ //% block="90"
23+ SR_90 ,
24+ //% block="180"
25+ SR_180 ,
26+ //% block="270"
27+ SR_270
28+ }
29+
1430//% weight=100 color=#Ffbc11 icon="T" block="ScrollText"
1531namespace ScrolText {
1632 let FONTS = [
@@ -66,7 +82,7 @@ namespace ScrolText {
6682 [ 0x04 , 0x06 , 0x04 , 0x04 , 0x0E ] , //49: 1
6783 [ 0x07 , 0x08 , 0x06 , 0x01 , 0x0F ] , //50: 2
6884 [ 0x0F , 0x08 , 0x04 , 0x09 , 0x06 ] , //51: 3
69- [ 0x0C , 0x0A , 0x09 , 0x1F , 0x08 ] , //52: 4
85+ [ 0x0C , 0x0A , 0x09 , 0x0F , 0x08 ] , //52: 4
7086 [ 0x1F , 0x01 , 0x0F , 0x10 , 0x0F ] , //53: 5
7187 [ 0x08 , 0x04 , 0x0E , 0x11 , 0x0E ] , //54: 6
7288 [ 0x1F , 0x08 , 0x04 , 0x02 , 0x01 ] , //55: 7
@@ -152,7 +168,59 @@ namespace ScrolText {
152168 . . . . .
153169 ` )
154170
155- function DatToImg ( dat : number [ ] ) : void {
171+ function _Rotate ( font : number [ ] , rotate : SCROLL_ROTATE ) {
172+ let m : number [ ] [ ] = [
173+ [ 0 , 0 , 0 , 0 , 0 ] ,
174+ [ 0 , 0 , 0 , 0 , 0 ] ,
175+ [ 0 , 0 , 0 , 0 , 0 ] ,
176+ [ 0 , 0 , 0 , 0 , 0 ] ,
177+ [ 0 , 0 , 0 , 0 , 0 ]
178+ ]
179+ let r : number [ ] = [ font [ 0 ] , font [ 1 ] , font [ 2 ] , font [ 3 ] , font [ 4 ] ]
180+ switch ( rotate ) {
181+ case SCROLL_ROTATE . SR_0 :
182+ return r ;
183+ case SCROLL_ROTATE . SR_90 :
184+ for ( let i = 0 ; i < 5 ; i ++ ) {
185+ m [ 4 - i ] [ 0 ] = ( r [ i ] & 0x01 )
186+ m [ 4 - i ] [ 1 ] = ( r [ i ] & 0x02 )
187+ m [ 4 - i ] [ 2 ] = ( r [ i ] & 0x04 )
188+ m [ 4 - i ] [ 3 ] = ( r [ i ] & 0x08 )
189+ m [ 4 - i ] [ 4 ] = ( r [ i ] & 0x10 )
190+ }
191+ break ;
192+ case SCROLL_ROTATE . SR_180 :
193+ for ( let i = 0 ; i < 5 ; i ++ ) {
194+ m [ 4 ] [ 4 - i ] = ( r [ i ] & 0x01 )
195+ m [ 3 ] [ 4 - i ] = ( r [ i ] & 0x02 )
196+ m [ 2 ] [ 4 - i ] = ( r [ i ] & 0x04 )
197+ m [ 1 ] [ 4 - i ] = ( r [ i ] & 0x08 )
198+ m [ 0 ] [ 4 - i ] = ( r [ i ] & 0x10 )
199+ }
200+ break ;
201+ case SCROLL_ROTATE . SR_270 :
202+ for ( let i = 0 ; i < 5 ; i ++ ) {
203+ m [ i ] [ 4 ] = ( r [ i ] & 0x01 )
204+ m [ i ] [ 3 ] = ( r [ i ] & 0x02 )
205+ m [ i ] [ 2 ] = ( r [ i ] & 0x04 )
206+ m [ i ] [ 1 ] = ( r [ i ] & 0x08 )
207+ m [ i ] [ 0 ] = ( r [ i ] & 0x10 )
208+ }
209+ break ;
210+ }
211+ for ( let n = 0 ; n <= 4 ; n ++ ) {
212+ let d = 0
213+ if ( m [ 0 ] [ n ] ) d |= 1
214+ if ( m [ 1 ] [ n ] ) d |= 2
215+ if ( m [ 2 ] [ n ] ) d |= 4
216+ if ( m [ 3 ] [ n ] ) d |= 8
217+ if ( m [ 4 ] [ n ] ) d |= 16
218+ r [ n ] = d
219+ }
220+ return r
221+ }
222+
223+ function _ToImg ( dat : number [ ] ) : void {
156224 for ( let i = 0 ; i < 5 ; i ++ ) {
157225 img . setPixel ( 0 , i , ( dat [ i ] & 0x01 ) == 0x01 )
158226 img . setPixel ( 1 , i , ( dat [ i ] & 0x02 ) == 0x02 )
@@ -166,74 +234,74 @@ namespace ScrolText {
166234 * show a scroll string
167235 * @param s , eg: "Hello"
168236 * @param dir , eg: SCROLL_DIR.LEFT
237+ * @param rotate, eg: SCROLL_ROTATE
169238 * @param delay , eg: 100
170239 */
171- //% blockId="SCROLL_SHOWSTRING" block="scroll string %s|dir %dir|delay %delay"
240+ //% blockId="SCROLL_SHOWSTRING" block="scroll string %s|dir %dir|rotate %rotate| delay %delay"
172241 //% weight=100 blockGap=8
173- export function showString ( s : string , dir : SCROLL_DIR , delay : number ) : void {
242+ export function showString ( s : string , dir : SCROLL_DIR , rotate : SCROLL_ROTATE , delay : number ) : void {
174243 let L = s . length + 1
175- let a = FONTS [ 0 ]
176- let b = FONTS [ 0 ]
244+ let a : number [ ] = [ 0 , 0 , 0 , 0 , 0 ]
245+ let b : number [ ] = [ 0 , 0 , 0 , 0 , 0 ]
177246
178- if ( s == '' )
179- return
247+ if ( s == '' ) return
180248
181249 s = ' ' + s + ' '
182250
183251 switch ( dir ) {
184252 case SCROLL_DIR . LEFT :
185253 for ( let i = 0 ; i < L ; i ++ ) {
186- a = FONTS [ s . charCodeAt ( i ) ]
187- b = FONTS [ s . charCodeAt ( i + 1 ) ]
254+ a = _Rotate ( FONTS [ s . charCodeAt ( i ) ] , rotate )
255+ b = _Rotate ( FONTS [ s . charCodeAt ( i + 1 ) ] , rotate )
188256 let c : number [ ] = [ 0 , 0 , 0 , 0 , 0 ]
189257 for ( let j = 0 ; j < 5 ; j ++ ) {
190258 for ( let k = 0 ; k < 5 ; k ++ )
191259 c [ k ] = ( a [ k ] >> j ) | ( ( b [ k ] << ( 8 - j ) ) >> 3 )
192- DatToImg ( c )
260+ _ToImg ( c )
193261 img . showImage ( 0 , delay )
194262 }
195263 }
196264 break ;
197265 case SCROLL_DIR . RIGHT :
198266 for ( let i = 0 ; i < L ; i ++ ) {
199- a = FONTS [ s . charCodeAt ( i ) ]
200- b = FONTS [ s . charCodeAt ( i + 1 ) ]
267+ a = _Rotate ( FONTS [ s . charCodeAt ( i ) ] , rotate )
268+ b = _Rotate ( FONTS [ s . charCodeAt ( i + 1 ) ] , rotate )
201269 let c : number [ ] = [ 0 , 0 , 0 , 0 , 0 ]
202270 for ( let j = 0 ; j < 5 ; j ++ ) {
203271 for ( let k = 0 ; k < 5 ; k ++ )
204272 c [ k ] = ( a [ k ] << j ) | ( ( b [ k ] >> ( 5 - j ) ) )
205- DatToImg ( c )
273+ _ToImg ( c )
206274 img . showImage ( 0 , delay )
207275 }
208276 }
209277 break ;
210278 case SCROLL_DIR . UP :
211279 for ( let i = 0 ; i < L ; i ++ ) {
212- a = FONTS [ s . charCodeAt ( i ) ]
213- b = FONTS [ s . charCodeAt ( i + 1 ) ]
280+ a = _Rotate ( FONTS [ s . charCodeAt ( i ) ] , rotate )
281+ b = _Rotate ( FONTS [ s . charCodeAt ( i + 1 ) ] , rotate )
214282 let c : number [ ] = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
215283 for ( let j = 0 ; j < 5 ; j ++ ) {
216284 c [ j ] = a [ j ]
217285 c [ j + 6 ] = b [ j ]
218286 }
219287 for ( let j = 0 ; j < 6 ; j ++ ) {
220- DatToImg ( c )
288+ _ToImg ( c )
221289 img . showImage ( 0 , delay )
222290 c . removeAt ( 0 )
223291 }
224292 }
225293 break ;
226294 case SCROLL_DIR . DOWN :
227295 for ( let i = 0 ; i < L ; i ++ ) {
228- a = FONTS [ s . charCodeAt ( i ) ]
229- b = FONTS [ s . charCodeAt ( i + 1 ) ]
296+ a = _Rotate ( FONTS [ s . charCodeAt ( i ) ] , rotate )
297+ b = _Rotate ( FONTS [ s . charCodeAt ( i + 1 ) ] , rotate )
230298 let c : number [ ] = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
231299 for ( let j = 0 ; j < 5 ; j ++ ) {
232300 c [ j ] = a [ j ]
233301 c [ j + 6 ] = b [ j ]
234302 }
235303 for ( let j = 0 ; j < 6 ; j ++ ) {
236- DatToImg ( c )
304+ _ToImg ( c )
237305 img . showImage ( 0 , delay )
238306 for ( let k = 5 ; k > 0 ; k -- ) {
239307 c [ k ] = c [ k - 1 ]
@@ -249,11 +317,12 @@ namespace ScrolText {
249317 * show a scroll number
250318 * @param n , eg: 123
251319 * @param dir , eg: SCROLL_DIR.LEFT
320+ * @param rotate, eg: SCROLL_ROTATE
252321 * @param delay , eg: 100
253322 */
254- //% blockId="SCROLL_SHOWNUMBER" block="scroll number %n|dir %dir|delay %delay"
323+ //% blockId="SCROLL_SHOWNUMBER" block="scroll number %n|dir %dir|rotate %rotate| delay %delay"
255324 //% weight=100 blockGap=8
256- export function showNumber ( n : number , dir : SCROLL_DIR , delay : number ) : void {
257- showString ( n . toString ( ) , dir , delay )
325+ export function showNumber ( n : number , dir : SCROLL_DIR , rotate : SCROLL_ROTATE , delay : number ) : void {
326+ showString ( n . toString ( ) , dir , rotate , delay )
258327 }
259328}
0 commit comments