-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat:コードチェック機能の実装 #362
base: main
Are you sure you want to change the base?
feat:コードチェック機能の実装 #362
Changes from all commits
6b34c48
1a0cc5a
0347b4f
e81c177
613e6ff
29f670e
6d77ca4
46b2a5c
85e6a2f
f9f1ef8
ee75b24
ff11289
6553612
52649b2
8529e9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { useMemo } from "react"; | ||
import { crc8Calculator } from "../utils/crc8Calculator"; | ||
|
||
export const useCrc8 = (data?: Uint8Array) => { | ||
return useMemo(() => crc8Calculator(data), [data]); | ||
}; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,5 @@ | ||||||
import { WritableStreamDefaultWriter } from "stream/web"; | ||||||
import { crc8Calculator } from "../utils/crc8Calculator"; | ||||||
import { Failure, Result, Success } from "./result"; | ||||||
|
||||||
export const targets = ["ESP32", "RBoard"] as const; | ||||||
|
@@ -23,7 +24,7 @@ const enterWriteModeKeyword: Record<Target, RegExp> = { | |||||
} as const; | ||||||
|
||||||
const exitWriteModeKeyword: Record<Target, RegExp> = { | ||||||
ESP32: /mrubyc-esp32: End mrbwrite mode/, | ||||||
ESP32: /mruby\/c v\d(.\d+)* start/, | ||||||
RBoard: /\+OK Execute mruby\/c\./, | ||||||
} as const; | ||||||
|
||||||
|
@@ -238,7 +239,7 @@ export class MrubyWriterConnector { | |||||
async writeCode( | ||||||
binary: Uint8Array, | ||||||
option?: Partial<{ execute: boolean }> | ||||||
): Promise<Result<null, Error>> { | ||||||
): Promise<Result<string, Error>> { | ||||||
if (!this.port) { | ||||||
return Failure.error("No port."); | ||||||
} | ||||||
|
@@ -262,7 +263,11 @@ export class MrubyWriterConnector { | |||||
await this.sendCommand("execute"); | ||||||
} | ||||||
|
||||||
return Success.value(null); | ||||||
const crc = crc8Calculator(binary); | ||||||
const crcRes = writeRes.value.split("+OK")[1]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 意図が分かりづらいので、
Suggested change
のような感じでしょうか?(テキトーに書いたのでちゃんと検証していません) |
||||||
if (crc !== undefined && crcRes !== undefined) | ||||||
this.verify(crc, parseInt(crcRes, 16)); | ||||||
return Success.value(writeRes.value); | ||||||
Comment on lines
+268
to
+270
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
} | ||||||
|
||||||
private async sendData( | ||||||
|
@@ -492,4 +497,13 @@ export class MrubyWriterConnector { | |||||
|
||||||
return Success.value(line); | ||||||
} | ||||||
async verify(culcHash: number, retHash: number) { | ||||||
if (culcHash === retHash) { | ||||||
this.handleText("\r\n Verify Success\r\n"); | ||||||
return true; | ||||||
} else { | ||||||
this.handleText("\r\n Verify Failed\r\n"); | ||||||
return false; | ||||||
} | ||||||
} | ||||||
Comment on lines
+500
to
+508
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { | |
CheckCircleRounded as CheckCircleRoundedIcon, | ||
Edit as EditIcon, | ||
Flag as FlagIcon, | ||
Plagiarism, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 流石にPlagiarism(=盗作)はよくない気がします。Document SearchやData Checkはどうでしょうか? |
||
Usb as UsbIcon, | ||
UsbOff as UsbOffIcon, | ||
} from "@mui/icons-material"; | ||
|
@@ -30,6 +31,7 @@ import { isTarget } from "libs/utility"; | |
import { useTranslation } from "react-i18next"; | ||
import ESP32 from "/images/ESP32.png"; | ||
import RBoard from "/images/Rboard.png"; | ||
import { useCrc8 } from "../hooks/useCrc8"; | ||
|
||
const targets = [ | ||
{ | ||
|
@@ -51,6 +53,7 @@ const commands = [ | |
"reset", | ||
"help", | ||
"showprog", | ||
"verify", | ||
] as const; | ||
|
||
export const Home = () => { | ||
|
@@ -98,6 +101,7 @@ export const Home = () => { | |
} | ||
}, [t, connector]); | ||
|
||
const crc8 = useCrc8(code); | ||
//1秒ごとに書き込みモードに入ることを試みる | ||
const tryEntry = useCallback(async () => { | ||
return new Promise<void>((resolve, reject) => { | ||
|
@@ -159,8 +163,13 @@ export const Home = () => { | |
}` | ||
); | ||
} | ||
if (text == "verify" && res.isSuccess() && res.value.includes("+OK")) { | ||
const hash = parseInt(res.value.split("+OK")[1], 16); | ||
if (crc8 !== undefined && hash !== undefined) | ||
connector.verify(crc8, hash); | ||
} | ||
Suzune2741 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
[t, connector] | ||
[t, connector, crc8] | ||
); | ||
|
||
const writeCode = useCallback(async () => { | ||
|
@@ -453,6 +462,14 @@ export const Home = () => { | |
compileStatus.status !== "success" || !connector.isWriteMode | ||
} | ||
/> | ||
<ControlButton | ||
label={t("検証")} | ||
icon={<Plagiarism />} | ||
onClick={() => send("verify")} | ||
Suzune2741 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
disabled={ | ||
compileStatus.status !== "success" || !connector.isWriteMode | ||
} | ||
/> | ||
<ControlButton | ||
label={t("実行")} | ||
icon={<FlagIcon />} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const crc8Calculator = (data?: Uint8Array) => { | ||
Suzune2741 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const hash = data?.reduce((crc, byte) => { | ||
const poly = 0x31; | ||
crc ^= byte; | ||
for (let i = 0; i < 8; i++) { | ||
if (crc & 0x80) { | ||
crc = (crc << 1) ^ poly; | ||
} else { | ||
crc = crc << 1; | ||
} | ||
} | ||
crc &= 0xff; | ||
return crc; | ||
}, 0xff); | ||
return hash; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
意図が分かりづらいので何かしらのコメントが欲しいです。